Created a Projectile and allowed it to be operated on by wind and gravity
This commit is contained in:
72
src/main.rs
72
src/main.rs
@@ -1,3 +1,71 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use structs::Tuple;
|
||||
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Environment {
|
||||
gravity: Tuple,
|
||||
wind: Tuple,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct Projectile {
|
||||
position: Tuple,
|
||||
velocity: Tuple,
|
||||
}
|
||||
|
||||
impl Projectile {
|
||||
|
||||
fn new(position: Tuple, velocity: Tuple) -> Projectile {
|
||||
if !position.is_point() {
|
||||
panic!("position can not be a vector");
|
||||
}
|
||||
|
||||
if !velocity.is_vector() {
|
||||
panic!("velocity can not be point");
|
||||
}
|
||||
|
||||
Projectile {
|
||||
position: position,
|
||||
velocity: velocity,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Projectile {
|
||||
fn tick(&mut self, env: &Environment) {
|
||||
self.position = self.position + self.velocity;
|
||||
self.velocity = env.wind + env.gravity;
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Projectile {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "pos: {}, vel {}", self.position, self.velocity)
|
||||
}
|
||||
}
|
||||
|
||||
fn init_env() -> Environment {
|
||||
Environment {
|
||||
gravity: Tuple::vector(0.0, 0.0, -0.98),
|
||||
wind: Tuple::vector(0.0, 0.0, 0.0),
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let env = init_env();
|
||||
|
||||
let mut ball = Projectile::new(
|
||||
Tuple::point(0.0, 0.0, 1.0),
|
||||
Tuple::vector(1.0, 0.0, 0.0),
|
||||
);
|
||||
|
||||
let mut tick = 0;
|
||||
println!("tick {} the ball {}", tick, ball);
|
||||
while ball.position.z() > 0.0 {
|
||||
ball.tick(&env);
|
||||
|
||||
println!("tick {} the ball {}", tick, ball);
|
||||
tick += 1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user