Created a Projectile and allowed it to be operated on by wind and gravity

This commit is contained in:
Jon Janzen
2021-02-07 14:49:14 -07:00
parent c863cf5a8b
commit bf1afc092e
3 changed files with 105 additions and 2 deletions

View File

@@ -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;
}
}