Electric Froth

______ _ ______ ( / // _/_ o ( / _/_ / /-- // _ _, / _ , _, -/--_ __ / /_ (/____/(/_(/_(__(__/ (_(_(__ _/ / (_(_)(__/ /_


Game Physics - Reflection and Reversing Vectors

by Joel Kleier on 2014-12-19

Developing hobby games I usually just stick to libraries that already handle things like physics. Basically I follow examples, copy/paste code, and plugin numbers. This is good for getting a hobby game built but not so good to learn the math and gain an intuition of what’s actually going on, which is what I want to do now. part 0 part 1 part 2 part 3.

There are only a couple of operations that I’ve read about that seem generally useful to my purpose: reflection of a vector about an arbitrary axis, and reversing the direction of a vector!


This article was written as I attempt to learn the subject matter at hand, so there is a strong possibility of incorrect information based on my own ignorance.

Please, if you see an error of any kind, feel free to send me an email!

Reflection About an Arbitrary Axis

Say you have a vector V and a vector T…​ how do you reflect vector V about vector T?

The key here is that the intent is not to rotate around just the x and y axis, no, the rotation should occur about an arbitrary axis! Not as straight forward, and, like rotation, the math is a little much for me to delve into right now, but here’s a great resource, which provides the following formula:

w' = w - 2(w . v)v

With the V and T values defined earlier, this turns into:

V' = V - 2(V . T)T

(the period is supposed to represent the dot product operation)

And the code ends up looking like:

proc reflect*(self, axis: Vector): Vector =
    var V = axis * (self.dot(axis) * 2)
    return self - V

Admittedly, this is untested at the time of this writing, but that should be the gist of the operation as code.

Reversing the Direction of A Vector

This is almost too simple! If you think about it, to reverse a vector is simply to subtract it’s x and y distances instead of add them, so reversing a vector really just boils down to returning the opposite of each of it’s components, like so:

proc reverse*(self: Vector): Vector =
    result.x = -self.x
    result.y = -self.y


At this point, my grasp of some of the math requried for 2D game physics is a little less shaky, and has already helped me out! The next step is to dive in to some more advanced concepts, like the Separating-Axis Theorem for detection and (possibly) resolution of collisions. But, I’m not sure if I’ll continue into that subject next week or not. There are some other languages, tools, and libraries that I’d like to take a deeper look into.

Whatever comes next time, thanks for reading!