pub struct Projectile { /* private fields */ }Expand description
A projectile with position, velocity, and acceleration.
Projectiles simulate simple physics-based motion, updating position based on velocity and velocity based on acceleration each frame.
§Example
use harmonica::{fps, Point, Vector, Projectile, GRAVITY};
// Create a ball thrown upward
let mut ball = Projectile::new(
fps(60),
Point::new(0.0, 0.0, 0.0),
Vector::new(5.0, 20.0, 0.0), // Initial velocity
GRAVITY,
);
// Simulate for 1 second
for _ in 0..60 {
let pos = ball.update();
println!("Ball at y={}", pos.y);
}Implementations§
Source§impl Projectile
impl Projectile
Sourcepub const fn new(
delta_time: f64,
position: Point,
velocity: Vector,
acceleration: Vector,
) -> Self
pub const fn new( delta_time: f64, position: Point, velocity: Vector, acceleration: Vector, ) -> Self
Creates a new projectile with the given parameters.
§Arguments
delta_time- Time step per update (usefpsto compute)position- Initial positionvelocity- Initial velocityacceleration- Constant acceleration (e.g., gravity)
§Example
use harmonica::{fps, Point, Vector, Projectile, TERMINAL_GRAVITY};
let projectile = Projectile::new(
fps(60),
Point::new(10.0, 0.0, 0.0),
Vector::new(5.0, 2.0, 0.0),
TERMINAL_GRAVITY,
);Sourcepub fn update(&mut self) -> Point
pub fn update(&mut self) -> Point
Updates the projectile’s position and velocity for one frame.
Returns the new position after the update.
§Example
use harmonica::{fps, Point, Vector, Projectile, GRAVITY};
let mut p = Projectile::new(
fps(60),
Point::origin(),
Vector::new(10.0, 0.0, 0.0),
GRAVITY,
);
// Update returns the new position
let new_pos = p.update();Sourcepub const fn acceleration(&self) -> Vector
pub const fn acceleration(&self) -> Vector
Returns the acceleration of the projectile.
Sourcepub fn set_position(&mut self, pos: Point)
pub fn set_position(&mut self, pos: Point)
Sets the position of the projectile.
Sourcepub fn set_velocity(&mut self, vel: Vector)
pub fn set_velocity(&mut self, vel: Vector)
Sets the velocity of the projectile.
Sourcepub fn set_acceleration(&mut self, acc: Vector)
pub fn set_acceleration(&mut self, acc: Vector)
Sets the acceleration of the projectile.
Trait Implementations§
Source§impl Clone for Projectile
impl Clone for Projectile
Source§fn clone(&self) -> Projectile
fn clone(&self) -> Projectile
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for Projectile
impl Debug for Projectile
Source§impl PartialEq for Projectile
impl PartialEq for Projectile
impl Copy for Projectile
impl StructuralPartialEq for Projectile
Auto Trait Implementations§
impl Freeze for Projectile
impl RefUnwindSafe for Projectile
impl Send for Projectile
impl Sync for Projectile
impl Unpin for Projectile
impl UnwindSafe for Projectile
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more