Created a Projectile and allowed it to be operated on by wind and gravity
This commit is contained in:
34
Cargo.lock
generated
34
Cargo.lock
generated
@@ -1,5 +1,39 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "approx"
|
||||||
|
version = "0.4.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3f2a05fd1bd10b2527e20a2cd32d8873d115b8b39fe219ee25f42a8aca6ba278"
|
||||||
|
dependencies = [
|
||||||
|
"num-traits",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "autocfg"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num-traits"
|
||||||
|
version = "0.2.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
|
||||||
|
dependencies = [
|
||||||
|
"autocfg",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "structs"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"approx",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tuples"
|
name = "tuples"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"structs",
|
||||||
|
]
|
||||||
|
|||||||
@@ -7,3 +7,4 @@ edition = "2018"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
structs = { path = "structs" }
|
||||||
|
|||||||
72
src/main.rs
72
src/main.rs
@@ -1,3 +1,71 @@
|
|||||||
fn main() {
|
use structs::Tuple;
|
||||||
println!("Hello, world!");
|
|
||||||
|
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