Skip to content

Commit

Permalink
Rename velocity to speed (#19)
Browse files Browse the repository at this point in the history
Velocity includes direction as well as magnitude, while these values
do not.
  • Loading branch information
johanhelsing authored Nov 20, 2022
1 parent 228b22f commit 47d13a9
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ fn spawn_particle_system(mut commands: Commands, asset_server: Res<AssetServer>)
max_particles: 10_000,
default_sprite: asset_server.load("my_particle.png"),
spawn_rate_per_second: 25.0.into(),
initial_velocity: JitteredValue::jittered(3.0, -1.0..1.0),
initial_speed: JitteredValue::jittered(3.0, -1.0..1.0),
lifetime: JitteredValue::jittered(8.0, -2.0..2.0),
color: ColorOverTime::Gradient(Gradient::new(vec![
ColorPoint::new(Color::WHITE, 0.0),
Expand Down
2 changes: 1 addition & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn startup_system(mut commands: Commands, asset_server: Res<AssetServer>) {
max_particles: 50_000,
default_sprite: asset_server.load("px.png"),
spawn_rate_per_second: 1000.0.into(),
initial_velocity: JitteredValue::jittered(3.0, -1.0..1.0),
initial_speed: JitteredValue::jittered(3.0, -1.0..1.0),
acceleration: ValueOverTime::Sin(SinWave {
amplitude: 150.0,
period: 5.0,
Expand Down
4 changes: 2 additions & 2 deletions examples/local_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn startup_system(mut commands: Commands, asset_server: Res<AssetServer>) {
emitter_angle: 0.0,
default_sprite: asset_server.load("px.png"),
spawn_rate_per_second: 35.0.into(),
initial_velocity: JitteredValue::jittered(25.0, 0.0..5.0),
initial_speed: JitteredValue::jittered(25.0, 0.0..5.0),
acceleration: 0.0.into(),
lifetime: JitteredValue::jittered(3.0, -2.0..2.0),
color: ColorOverTime::Gradient(Gradient::new(vec![
Expand All @@ -69,7 +69,7 @@ fn startup_system(mut commands: Commands, asset_server: Res<AssetServer>) {
emitter_angle: std::f32::consts::PI,
default_sprite: asset_server.load("px.png"),
spawn_rate_per_second: 35.0.into(),
initial_velocity: JitteredValue::jittered(25.0, 0.0..5.0),
initial_speed: JitteredValue::jittered(25.0, 0.0..5.0),
acceleration: 0.0.into(),
lifetime: JitteredValue::jittered(3.0, -2.0..2.0),
color: ColorOverTime::Gradient(Gradient::new(vec![
Expand Down
4 changes: 2 additions & 2 deletions examples/time_scaling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn startup_system(mut commands: Commands, asset_server: Res<AssetServer>) {
emitter_angle: 0.0,
default_sprite: asset_server.load("px.png"),
spawn_rate_per_second: 35.0.into(),
initial_velocity: JitteredValue::jittered(25.0, 0.0..5.0),
initial_speed: JitteredValue::jittered(25.0, 0.0..5.0),
acceleration: 0.0.into(),
lifetime: JitteredValue::jittered(3.0, -2.0..2.0),
color: ColorOverTime::Gradient(Gradient::new(vec![
Expand All @@ -61,7 +61,7 @@ fn startup_system(mut commands: Commands, asset_server: Res<AssetServer>) {
emitter_angle: std::f32::consts::PI,
default_sprite: asset_server.load("px.png"),
spawn_rate_per_second: 35.0.into(),
initial_velocity: JitteredValue::jittered(25.0, 0.0..5.0),
initial_speed: JitteredValue::jittered(25.0, 0.0..5.0),
acceleration: 0.0.into(),
lifetime: JitteredValue::jittered(3.0, -2.0..2.0),
color: ColorOverTime::Gradient(Gradient::new(vec![
Expand Down
14 changes: 7 additions & 7 deletions src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ pub struct ParticleSystem {
/// Zero indicates straight to the right in the X direction. [`std::f32::consts::PI`] indicates straight left in the X direction.
pub emitter_angle: f32,

/// The initial movement velocity of a particle.
/// The initial movement speed of a particle.
///
/// This value can be constant, or have added jitter to have particles move at varying speeds.
pub initial_velocity: JitteredValue,
pub initial_speed: JitteredValue,

/// The acceleration of each particle.
///
/// This value can change over time. Zero makes the particle move at its ``initial_velocity`` for its lifetime.
/// This value can change over time. Zero makes the particle move at its ``initial_speed`` for its lifetime.
pub acceleration: ValueOverTime,

/// The lifetime of each particle, in seconds.
Expand Down Expand Up @@ -146,7 +146,7 @@ impl Default for ParticleSystem {
spawn_radius: 0.0.into(),
emitter_shape: std::f32::consts::TAU,
emitter_angle: 0.0,
initial_velocity: 1.0.into(),
initial_speed: 1.0.into(),
acceleration: 0.0.into(),
lifetime: 5.0.into(),
color: ColorOverTime::default(),
Expand Down Expand Up @@ -205,9 +205,9 @@ pub struct Lifetime(pub f32);
#[derive(Debug, Component, Default)]
pub struct DistanceTraveled(pub f32);

/// Defines the current velocity of an individual particle entity.
/// Defines the current speed of an individual particle entity.
#[derive(Debug, Component, Default)]
pub struct Velocity(pub f32);
pub struct Speed(pub f32);

/// Defines the direction a particle is currently traveling.
#[derive(Debug, Component, Default)]
Expand Down Expand Up @@ -298,7 +298,7 @@ pub struct ParticleSystemBundle {
pub(crate) struct ParticleBundle {
pub particle: Particle,
pub lifetime: Lifetime,
pub velocity: Velocity,
pub speed: Speed,
pub direction: Direction,
pub distance: DistanceTraveled,
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//! max_particles: 10_000,
//! default_sprite: asset_server.load("my_particle.png"),
//! spawn_rate_per_second: 25.0.into(),
//! initial_velocity: JitteredValue::jittered(3.0, -1.0..1.0),
//! initial_speed: JitteredValue::jittered(3.0, -1.0..1.0),
//! lifetime: JitteredValue::jittered(8.0, -2.0..2.0),
//! color: ColorOverTime::Gradient(Gradient::new(vec![
//! ColorPoint::new(Color::WHITE, 0.0),
Expand Down
22 changes: 9 additions & 13 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rand::prelude::*;
use crate::{
components::{
BurstIndex, Direction, Lifetime, Particle, ParticleBundle, ParticleCount, ParticleSpace,
ParticleSystem, Playing, RunningState, Velocity,
ParticleSystem, Playing, RunningState, Speed,
},
values::ColorOverTime,
DistanceTraveled,
Expand Down Expand Up @@ -137,9 +137,7 @@ pub fn partcle_spawner(
max_lifetime: particle_system.lifetime.get_value(&mut rng),
max_distance: particle_system.max_distance,
},
velocity: Velocity(
particle_system.initial_velocity.get_value(&mut rng),
),
speed: Speed(particle_system.initial_speed.get_value(&mut rng)),
direction: Direction::new(
direction,
particle_system.z_value_override.is_some(),
Expand All @@ -165,9 +163,7 @@ pub fn partcle_spawner(
max_lifetime: particle_system.lifetime.get_value(&mut rng),
max_distance: particle_system.max_distance,
},
velocity: Velocity(
particle_system.initial_velocity.get_value(&mut rng),
),
speed: Speed(particle_system.initial_speed.get_value(&mut rng)),
direction: Direction::new(
direction,
particle_system.z_value_override.is_some(),
Expand Down Expand Up @@ -232,26 +228,26 @@ pub(crate) fn particle_transform(
&Lifetime,
&Direction,
&mut DistanceTraveled,
&mut Velocity,
&mut Speed,
&mut Transform,
)>,
particle_system_query: Query<&ParticleSystem>,
time: Res<Time>,
) {
particle_query.par_for_each_mut(
512,
|(particle, lifetime, direction, mut distance, mut velocity, mut transform)| {
|(particle, lifetime, direction, mut distance, mut speed, mut transform)| {
if let Ok(particle_system) = particle_system_query.get(particle.parent_system) {
let lifetime_pct = lifetime.0 / particle.max_lifetime;
let initial_position = transform.translation;
if particle_system.use_scaled_time {
velocity.0 += particle_system.acceleration.at_lifetime_pct(lifetime_pct)
speed.0 += particle_system.acceleration.at_lifetime_pct(lifetime_pct)
* time.delta_seconds();
transform.translation += direction.0 * velocity.0 * time.delta_seconds();
transform.translation += direction.0 * speed.0 * time.delta_seconds();
} else {
velocity.0 += particle_system.acceleration.at_lifetime_pct(lifetime_pct)
speed.0 += particle_system.acceleration.at_lifetime_pct(lifetime_pct)
* time.raw_delta_seconds();
transform.translation += direction.0 * velocity.0 * time.raw_delta_seconds();
transform.translation += direction.0 * speed.0 * time.raw_delta_seconds();
}

transform.scale = Vec3::splat(particle_system.scale.at_lifetime_pct(lifetime_pct));
Expand Down

0 comments on commit 47d13a9

Please sign in to comment.