diff options
author | tom barrett <spalf0@gmail.com> | 2018-02-27 06:59:58 -0600 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2018-02-27 06:59:58 -0600 |
commit | b5896e2d7597e42818a47710da22098d178bf8f6 (patch) | |
tree | d48107628b0c10fc41493e35f8ba8f3039c3b9bc /src/astroid.rs | |
parent | 87346ea9b91d441e92c476785c2cb979f9a23bf5 (diff) |
-now use velocity and position vector, astroids are now randomly populated
Diffstat (limited to 'src/astroid.rs')
-rw-r--r-- | src/astroid.rs | 47 |
1 files changed, 35 insertions, 12 deletions
diff --git a/src/astroid.rs b/src/astroid.rs index 6aa1807..fb14751 100644 --- a/src/astroid.rs +++ b/src/astroid.rs @@ -1,35 +1,58 @@ use mass::{Mass, Type}; +extern crate rand; +use self::rand::distributions::Range; +use astroid::rand::distributions::Sample; extern crate serde_json; +use astroid::rand::Rng; #[derive(Serialize, Deserialize, Debug)] pub struct Astroid { name : String, t : Type, - location : (f64, f64, f64), + position : (f64, f64, f64), + velocity : (f64, f64, f64), } -impl Mass for Astroid { - fn new(name : &str, location : (f64, f64, f64)) -> Astroid { +impl Astroid { + pub fn new() -> Astroid { + let name : String = rand::thread_rng() + .gen_ascii_chars() + .take(8) + .collect(); + let mut r = Range::new(-50.0, 50.0); + let mut rng = rand::thread_rng(); + let position = (r.sample(&mut rng), r.sample(&mut rng), r.sample(&mut rng)); Astroid { - name : String::from(name), - t : Type::Astroid, - location : location, + name : name, + t : Type::Astroid, + position : position, + velocity : (0.0, 0.0, 0.0), } } +} +impl Mass for Astroid { fn name(&self) -> &String { &self.name } - fn location(&self) -> (f64, f64, f64) { - self.location - } - - fn set_location(&mut self, location : (f64, f64, f64)) { - self.location = location; + fn position(&self) -> (f64, f64, f64) { + self.position } fn serialize(&self) ->String { serde_json::to_string(self).unwrap() } + + fn process(&mut self) { + self.position.0 += self.velocity.0; + self.position.1 += self.velocity.1; + self.position.2 += self.velocity.2; + } + + fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) { + self.velocity.0 += acceleration.0; + self.velocity.1 += acceleration.1; + self.velocity.2 += acceleration.2; + } } |