Limit stick point to unit vector
authorTomas Wenström <tomas.wenstrom@gmail.com>
Tue, 19 Jan 2021 18:42:34 +0000 (19:42 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Tue, 19 Jan 2021 18:42:34 +0000 (19:42 +0100)
src/common.rs
src/core/controller.rs
src/core/game.rs

index 01c0238..0d82903 100644 (file)
@@ -14,10 +14,26 @@ pub struct Point2D<T> {
 }
 
 impl Point2D<f64> {
-    pub fn length(self) -> f64 {
+    pub fn length(&self) -> f64 {
         ((self.x * self.x) + (self.y * self.y)).sqrt()
     }
 
+    pub fn normalize(&self) -> Self {
+       let l = self.length();
+       Self {
+           x: self.x / l,
+           y: self.y / l,
+       }
+    }
+
+    pub fn radians(&self) -> Radians {
+       Radians(self.y.atan2(self.x))
+    }
+
+    pub fn degrees(&self) -> Degrees {
+       self.radians().to_degrees()
+    }
+
     pub fn to_i32(self) -> Point2D<i32> {
        Point2D {
            x: self.x as i32,
index 832480d..9d23257 100644 (file)
@@ -1,5 +1,5 @@
 use common::Point2D;
-use hashmap;
+use {hashmap, point};
 use common::Radians;
 use sdl2::HapticSubsystem;
 use sdl2::JoystickSubsystem;
@@ -68,7 +68,6 @@ pub struct Stick {
     pub x: f32,
     pub y: f32,
     pub a: Radians,
-    pub len: f32,
 }
 
 impl Stick {
@@ -82,11 +81,6 @@ impl Stick {
            Err(_) => panic!("invalid y axis {}", self.idy),
        };
        self.a = Radians(self.y.atan2(self.x) as f64);
-       self.len = {
-           let x = (self.x / self.y).abs().min(1.0);
-           let y = (self.y / self.x).abs().min(1.0);
-           (self.x.powi(2) + self.y.powi(2)).sqrt() / (x.powi(2) + y.powi(2)).sqrt()
-       }
     }
 
     #[inline(always)] #[allow(dead_code)] fn up(&self) -> bool { self.y > 0.99 }
@@ -94,15 +88,17 @@ impl Stick {
     #[inline(always)] #[allow(dead_code)] fn left(&self) -> bool { self.x < -0.99 }
     #[inline(always)] #[allow(dead_code)] fn right(&self) -> bool { self.x > 0.99 }
 
-    pub fn to_point(&self) -> Point2D<f64> {
-       Point2D {
-           x: self.x as f64,
-           y: self.y as f64,
-       }
+    pub fn to_axis_point(&self) -> Point2D<f64> {
+       point!(self.x as f64, self.y as f64)
     }
 
-    pub fn to_adjusted_point(&self) -> Point2D<f64> {
-       Point2D::from(self.a) * self.len as f64
+    pub fn to_point(&self) -> Point2D<f64> {
+       let p = point!(self.x as f64, self.y as f64);
+       if p.length() > 1.0 {
+           p.normalize()
+       } else {
+           p
+       }
     }
 }
 
index 8474fab..f48d9b3 100644 (file)
@@ -170,7 +170,7 @@ impl Object for Character {
            for _i in 0..100 {
                objects.push(Box::new(Boll {
                    pos: self.pos,
-                   vel: ctrl.aim.to_adjusted_point() * (3.0 + rand::random::<f64>()) + point!(normal.sample(&mut rand::thread_rng()), normal.sample(&mut rand::thread_rng())) + self.vel,
+                   vel: ctrl.aim.to_point() * (3.0 + rand::random::<f64>()) + point!(normal.sample(&mut rand::thread_rng()), normal.sample(&mut rand::thread_rng())) + self.vel,
                    bounces: 2,
                }));
            }
@@ -207,12 +207,12 @@ impl Object for Character {
        let l = 300.0;
        let pos = (self.pos.x as i32, self.pos.y as i32);
        // axis values
-       let p = (self.pos + ctrl.aim.to_point() * l).to_i32().into();
+       let p = (self.pos + ctrl.aim.to_axis_point() * l).to_i32().into();
        canvas.set_draw_color((0, 255, 0));
        canvas.draw_line(pos, p).unwrap();
        draw_cross(canvas, p);
-       // adjusted values
-       let p = (self.pos + ctrl.aim.to_adjusted_point() * l).to_i32().into();
+       // values limited to unit vector
+       let p = (self.pos + ctrl.aim.to_point() * l).to_i32().into();
        canvas.set_draw_color((255, 0, 0));
        canvas.draw_line(pos, p).unwrap();
        draw_cross(canvas, p);