Game Dev Advanced

21  Vector math

Every engine leans on a small vector type. Here you give a Vec2 the two operations everything else builds on, then use them to advance a body one physics step.

Step 1 / 2

Give Vec2 a dot product and a length (its magnitude).

Hints
  • dot: self.x * o.x + self.y * o.y.
  • length is self.dot(self).sqrt().
21_vec_math.rs
Reset to starterCompiled in a sandbox; hidden checks must pass.
Reveal solution
#[derive(Clone, Copy)]
struct Vec2 { x: f32, y: f32 }

impl Vec2 {
    fn dot(self, o: Vec2) -> f32 {
        self.x * o.x + self.y * o.y
    }
    fn length(self) -> f32 {
        self.dot(self).sqrt()
    }
}