Skip to content

Instantly share code, notes, and snippets.

@mooman219
Created June 5, 2019 18:41
Show Gist options
  • Select an option

  • Save mooman219/afa12348e6e3d924faabab761c52655f to your computer and use it in GitHub Desktop.

Select an option

Save mooman219/afa12348e6e3d924faabab761c52655f to your computer and use it in GitHub Desktop.
pub fn tick(sprite: &mut SpriteDescription, particle: &mut Particle, delta: f32) {
let length_squared = particle.pos.x * particle.pos.x + particle.pos.y * particle.pos.y;
let length = f32::sqrt(length_squared);
let norm = particle.pos / length;
let norm_squared = particle.pos / length_squared;
particle.acceleration = -(norm * (Self::G * Self::MASS)).div_element_wise(norm_squared);
particle.velocity += particle.acceleration;
particle.pos += particle.velocity;
let velocity = particle.velocity + particle.acceleration * delta;
let position = particle.pos + velocity * delta;
sprite.set_pos(position.extend(0.0));
}
@mooman219

Copy link
Copy Markdown
Author

Fix norm_squared

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment