feat: added reflect

main
Sara 2025-01-26 21:13:25 +01:00
parent 110e72e241
commit 0c7cbeaa6c
2 changed files with 7 additions and 0 deletions

View File

@ -39,6 +39,11 @@ Vecf Vecf::move_towards(Vecf const &from, Vecf const &to, float delta) {
return Vecf::lerp(from, to, delta / Vecf::distance(from, to));
}
Vecf Vecf::reflect(Vecf const &in, Vecf const &normal) {
Vecf const midpoint{normal * Vecf::dot(normal, in)};
return (midpoint - in) + midpoint;
}
float Vecf::magnitude() const {
return std::sqrt(this->x * this->x + this->y * this->y);
}

View File

@ -22,6 +22,8 @@ struct Vecf {
static Vecf lerp(Vecf const &from, Vecf const &to, float t);
//! move towards a point by a set unit distance
static Vecf move_towards(Vecf const &from, Vecf const &to, float delta);
//! calculate the outgoing velocity of an object reflecting against a surface
static Vecf reflect(Vecf const &in, Vecf const &normal);
//! magnitude (a.k.a length or absolute) of this vector
float magnitude() const;
//! square of the magnitude, use for comparing lengths of vectors efficiently