From 0bcc2c31352a47e12a776c221730ba0249b38bad Mon Sep 17 00:00:00 2001 From: Allanis Date: Wed, 7 Aug 2013 12:14:47 +0100 Subject: [PATCH] [Add] vect_reflect() - Mirror a vector off another. --- src/physics.c | 11 +++++++++++ src/physics.h | 1 + 2 files changed, 12 insertions(+) diff --git a/src/physics.c b/src/physics.c index 8cd3035..99dc5ec 100644 --- a/src/physics.c +++ b/src/physics.c @@ -74,6 +74,17 @@ void vect_cadd(Vec2* v, const double x, const double y) { v->angle = ANGLE(v->x, v->y); } +/* Mirrors a vector off another, stores results in vector. */ +void vect_reflect(Vec2* r, Vec2* v, Vec2* n) { + double dot; + + dot = (v->x*n->x) + (v->y*n->y); + r->x = v->x - ((2. * dot) * n->x); + r->y = v->y - ((2. * dot) * n->y); + r->mod = MOD(r->x, r->y); + r->angle = MOD(r->x, r->y); + +} /* ================ */ /* SOLID! */ diff --git a/src/physics.h b/src/physics.h index ebf7caf..b00056d 100644 --- a/src/physics.h +++ b/src/physics.h @@ -33,6 +33,7 @@ void vectcpy(Vec2* dest, const Vec2* src); void vectnull(Vec2* v); double vect_angle(const Vec2* ref, const Vec2* v); void vect_cadd(Vec2* v, const double x, const double y); +void vect_reflect(Vec2* r, Vec2* v, Vec2* n); /* Describe any solid in 2D space. */ typedef struct Solid_ {