From 15cda3333ba8d5600b1afa9426a112dea99d4941 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Sun, 7 Mar 2021 19:11:22 +0100 Subject: [PATCH] Calculations from the world's 2D to the wall's 1D --- src/core/level/mod.rs | 39 +++++++++++++++++++++++++++++++++++++-- src/core/object/character.rs | 23 +++++++++++++++++++---- src/geometry.rs | 10 ++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/core/level/mod.rs b/src/core/level/mod.rs index bdc6cb0..e422042 100644 --- a/src/core/level/mod.rs +++ b/src/core/level/mod.rs @@ -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, vel: &Point) -> (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, Point) { + 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 { + 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 } } diff --git a/src/core/object/character.rs b/src/core/object/character.rs index 3d789f2..b2ec5cf 100644 --- a/src/core/object/character.rs +++ b/src/core/object/character.rs @@ -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> { - 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 diff --git a/src/geometry.rs b/src/geometry.rs index 7ddc3ec..f5e2b0e 100644 --- a/src/geometry.rs +++ b/src/geometry.rs @@ -42,6 +42,12 @@ impl Point { 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 { + 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 { -- 2.11.0