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()),
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 ////////////////////////////////////////////////////////////////
}
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
}
}
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);
}
}
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
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 {
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 {