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.
Give Vec2 a dot product and a length (its magnitude).
Hints
dot:self.x * o.x + self.y * o.y.lengthisself.dot(self).sqrt().
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()
}
}