Calculations from the world's 2D to the wall's 1D
authorTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 7 Mar 2021 18:11:22 +0000 (19:11 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Sun, 7 Mar 2021 18:11:22 +0000 (19:11 +0100)
src/core/level/mod.rs
src/core/object/character.rs
src/geometry.rs

index bdc6cb0..e422042 100644 (file)
@@ -106,7 +106,7 @@ impl Level {
            for e in &wall.edges {
                if !debug_mode {
                    let c = (e.p1 + e.p2) / 2.0;
-                   let a = (e.p2 - e.p1).to_angle() - std::f64::consts::FRAC_PI_2.radians();
+                   let a = e.normal().reverse();
 
                    renderer.draw_line(
                        <(i32, i32)>::from(c.to_i32()),
@@ -250,6 +250,15 @@ impl WallEdge {
        let cross = (self.p2 - self.p1).cross_product(p - self.p1);
        cross > 0.0
     }
+
+    fn normal(&self) -> Angle {
+       self.angle() + std::f64::consts::FRAC_PI_2.radians()
+    }
+
+    /// Angle from the right to the left point if the normal is up.
+    fn angle(&self) -> Angle {
+       (self.p2 - self.p1).to_angle()
+    }
 }
 
 ////////// WALL ////////////////////////////////////////////////////////////////
@@ -277,6 +286,32 @@ impl Wall {
     }
 
     pub fn normal(&self) -> Angle {
-       (self.edge.p2 - self.edge.p1).to_angle() + std::f64::consts::FRAC_PI_2.radians()
+       self.edge.normal()
+    }
+
+    pub fn angle(&self) -> Angle {
+       self.edge.angle()
+    }
+
+    pub fn from_2d(&self, pos: &Point<f64>, vel: &Point<f64>) -> (f64, f64) {
+       let pos = self.projection_of(*pos - self.edge.p1);
+       let vel = self.projection_of(*vel);
+       (pos, vel)
+    }
+
+    pub fn to_2d(&self, pos: f64, vel: f64) -> (Point<f64>, Point<f64>) {
+       let a = Point::from(self.edge.angle());
+       let pos = self.edge.p1 + a * pos;
+       let vel = a * vel;
+       (pos, vel)
+    }
+
+    /// Returns the 1D position of a point projected onto this wall.
+    /// This is done by rotating the point using a rotation matrix and then taking the resulting x value.
+    /// x'=xcos−ysin <- only this is used
+    /// y'=xsin+ycos
+    fn projection_of(&self, p: Point<f64>) -> f64 {
+       let r = Point::from(self.edge.angle());
+       p.x * r.x + p.y * r.y // r.y is inverted here instead of inverting the angle
     }
 }
index 3d789f2..b2ec5cf 100644 (file)
@@ -72,10 +72,9 @@ impl Object for Character {
                if let Intersection(wall, pos) = lvl.intersect_walls(self.body.pos - self.body.vel, self.body.pos) {
                    self.body.standing_on = Some(wall);
                    self.body.pos = pos;
-                   self.body.vel = point!(0.0, 0.0);
 
                    self.state.exit();
-                   self.state = Box::new(StandState);
+                   self.state = Box::new(StandState); // eller loopa igenom alla triggers eller states för att hitta rätt state?
                    self.state.enter(&mut self.body, &ctrl, objects);
                }
            }
@@ -162,9 +161,25 @@ impl State for FallState {
 struct StandState;
 
 impl State for StandState {
+    fn enter(&mut self, body: &mut Body, _ctrl: &Controller, _objects: &mut Objects) {
+       if let Some(wall) = &body.standing_on {
+           body.vel = body.vel.project_onto(wall.angle());
+       }
+    }
+
     fn update(&mut self, body: &mut Body, _ctrl: &Controller, _objects: &mut Objects, _lvl: &Level, _dt: Duration) -> Option<Box<dyn State>> {
-       if let Some(_wall) = &body.standing_on {
-           body.vel *= 0.9;
+       if let Some(wall) = &body.standing_on {
+           let (mut pos, mut vel) = wall.from_2d(&body.pos, &body.vel);
+           vel *= 0.9;
+           pos += vel;
+           let (p, v) = wall.to_2d(pos, vel);
+           body.pos = p;
+           body.vel = v;
+       }
+
+       None
+    }
+}
        }
 
        None
index 7ddc3ec..f5e2b0e 100644 (file)
@@ -42,6 +42,12 @@ impl Point<f64> {
     pub fn cross_product(&self, p: Self) -> f64 {
         return self.x * p.y - self.y * p.x;
     }
+
+    /// Returns the perpendicular projection of this vector on a line with the specified angle.
+    pub fn project_onto(&self, angle: Angle) -> Point<f64> {
+       let dot_product = self.length() * (self.to_angle() - angle).to_radians().cos();
+       Point::from(angle) * dot_product
+    }
 }
 
 macro_rules! impl_point_op {
@@ -225,6 +231,10 @@ impl Angle {
     pub fn mirror(&self, incidence: Angle) -> Angle {
        Angle((std::f64::consts::PI + self.0 * 2.0 - incidence.0) % std::f64::consts::TAU)
     }
+
+    pub fn reverse(&self) -> Angle {
+       Angle((self.0 + std::f64::consts::PI) % std::f64::consts::TAU)
+    }
 }
 
 impl PartialEq for Angle {