Reputation: 673
I'm trying to create a simple game, and I have an image (An arrow like this:--->
) that I want to point at the mouse. If I know the mouse position and the image's position, surely I can do some math to rotate the image so it points at the mouse pointer. So what would I need to do in order to rotate the object (about the centre) in such a way?
And Would I also be able to do this if I wanted to point the object at another set of coordinates?
Upvotes: 0
Views: 180
Reputation: 1858
For the rotation, you can use either a rotation matrices, euler angles or quaternions. I would use the latter, since they don't have the problem with the gimbal lock. All of them are interchangable, but there are a lot of conventions, be aware of that!
The problem with Euler angles is that, there are a lot of them. This sounds a little bit odd, I know. What I mean is that there are various conventions, and you have to know which one you are currently using, otherwise you introduce errors. The figure below rotates around the z
(D) axis, then around the new x'
(C) axis and then z'
(B) axis.
Using quaternions, this problem can be avoided alltogether. Eventhough it introduces some new ones, such as more sophisticated math, but this is well explained in the links below and also implemented in various libraries in python.
The rotational axis around which the rotation is performed can be oriented arbitrarily, which is a great benefit. This comes in quite handy later on.
If you would like to see an algorithmic example of it, I suggest you have a look at this post. For the math, wikipedia might help.
Upvotes: 1