iDevGames Forums
Reflect a vector in relation to another vector - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: Reflect a vector in relation to another vector (/thread-9561.html)



Reflect a vector in relation to another vector - ipeku - Nov 22, 2011 11:37 AM

Hey,

I'm stuck with a geometrical problem. I have vector A and vector V. I need to find vector A' that is the reflection of A in relation to V (see pic).

[Image: reflect.jpg]


RE: Reflect a vector in relation to another vector - skyhawk - Nov 22, 2011 01:30 PM

in 2D or 3D space?


RE: Reflect a vector in relation to another vector - ipeku - Nov 22, 2011 01:35 PM

2D space.


RE: Reflect a vector in relation to another vector - ThemsAllTook - Nov 22, 2011 02:03 PM

This one's pretty simple:

Code:
float Vector2_dot(Vector2 vector1, Vector2 vector2) {
    return vector1.x * vector2.x + vector1.y * vector2.y;
}

Vector2 Vector2_reflect(Vector2 vector, Vector2 normal) {
    Vector2 result;
    float dot;
    
    dot = Vector2_dot(vector, normal);
    result.x = 2 * dot * normal.x - vector.x;
    result.y = 2 * dot * normal.y - vector.y;
    return result;
}



RE: Reflect a vector in relation to another vector - ipeku - Dec 12, 2011 07:17 AM

Awesome. Just trying it out (too many side quests).

...

Hey it's working !!! Thanks.

[Image: l.jpg]