Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Fix particle systems + more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
darthdeus committed Sep 21, 2023
1 parent dc09e6d commit 249a7ec
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 24 deletions.
51 changes: 28 additions & 23 deletions comfy/examples/particle_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn setup(c: &mut EngineContext) {
Particle {
texture: texture_id("comfy"),
position: random_circle(5.0),
size: splat(1.0),
size: splat(0.5),
// Other than the builtin easing curves, any f32 -> f32 curve
// will work.
size_curve: quad_in_out,
Expand All @@ -33,26 +33,7 @@ fn setup(c: &mut EngineContext) {
}
})
.with_size(vec2(3.0, 8.0)),
Transform::position(vec2(-10.0, 0.0)),
));

c.commands().spawn((
ParticleSystem::with_spawn_on_death(300, || {
Particle {
texture: texture_id("comfy"),
position: random_circle(5.0),
size: splat(0.5),
size_curve: expo_out,

angular_velocity: random() * 5.0,
// Both size and color can be faded.
fade_type: FadeType::Both,
color_start: RED,
color_end: RED,
..Default::default()
}
}),
Transform::position(vec2(10.0, 0.0)),
Transform::position(vec2(-8.0, 0.0)),
));

c.commands().spawn((
Expand All @@ -74,6 +55,8 @@ fn setup(c: &mut EngineContext) {
color_start: GREEN,
color_end: LIME,

// Particles can have trails. These aren't currently
// very nice to configure, but they do work!
trail: TrailRef::Local(Trail::new(
0.1,
1.0,
Expand All @@ -88,6 +71,9 @@ fn setup(c: &mut EngineContext) {
BlendMode::Additive,
)),

// If the builtin particle system logic isn't enough, the particles can also use a
// custom `update` function with arbitrary logic inside that gets called on each
// frame.
update: Some(|p| {
// Calculate distance from origin.
let current_distance = p.position.length();
Expand All @@ -109,9 +95,9 @@ fn setup(c: &mut EngineContext) {

let abs_diff = difference.abs();

// Rescale the radius from 0..radius to 0..1 for interpolation
// Rescale the radius from 0..radius for interpolation
let abs_diff_scaled =
abs_diff.clamp_scale(0.0..DESIRED_RADIUS, 0.0..1.0);
abs_diff.clamp_scale(0.0..DESIRED_RADIUS, 0.2..0.8);

let t = if abs_diff < 1.0 {
1.0 - abs_diff_scaled
Expand All @@ -129,6 +115,25 @@ fn setup(c: &mut EngineContext) {
}),
Transform::position(Vec2::ZERO),
));

c.commands().spawn((
ParticleSystem::with_spawn_on_death(300, || {
Particle {
texture: texture_id("comfy"),
position: random_circle(5.0),
size: splat(0.7),
size_curve: expo_out,

angular_velocity: random() * 10.0,
// Both size and color can be faded.
fade_type: FadeType::Both,
color_start: RED,
color_end: RED,
..Default::default()
}
}), // .with_size(vec2(0.2, 5.0))
Transform::position(vec2(10.0, 0.0)),
));
}

fn update(_c: &mut EngineContext) {}
19 changes: 18 additions & 1 deletion comfy/src/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,26 @@ pub struct Particle {
pub trail: TrailRef,
}

// fn map_circle_point_to_rectangle(
// point_in_circle: Vec2,
// circle_radius: f32,
// rectangle_size: Vec2,
// ) -> Vec2 {
// let normalized = point_in_circle / circle_radius;
//
// let half_rect = rectangle_size * 0.5;
// (normalized * half_rect) + half_rect
// }

impl Particle {
pub fn initialize(&mut self, position: Vec2, size: Option<Vec2>) {
self.position += random_box(position, size.unwrap_or(Vec2::ZERO));
// self.position = random_box(position, size.unwrap_or(Vec2::ZERO));
if let Some(size) = size {
self.position = random_box(position, size);
} else {
self.position += position;
}

self.lifetime_current = self.lifetime_max * gen_range(0.9, 1.1);
self.frame = 0;
self.animation_timer = 0.0;
Expand Down

0 comments on commit 249a7ec

Please sign in to comment.