X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;f=src%2Fcore%2Flevel%2Fmod.rs;h=e422042f57241ae0ea787be34f6104e89584eb1f;hb=15cda3333ba8d5600b1afa9426a112dea99d4941;hp=bdc6cb0e292bad415ad694d05fcc8eaf3c61391b;hpb=b1075e661c2ce2f107350b8d4f5f9a92e047b93e;p=kaka%2Frust-sdl-test.git 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 } }