0

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?

George Willcox
  • 677
  • 12
  • 30
  • 2
    What point do you want to rotate it around? The tip of the arrow, the end of the tail, somewhere in the middle? How is the arrow represented in your code? – jscs Feb 28 '16 at 20:44
  • The arrow is an Image, and I'd like to rotate about the centre of it. I've updated the question to show this. – George Willcox Feb 28 '16 at 20:45

1 Answers1

1

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.

Euler Angles from Wolfram Mathworld

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.

Community
  • 1
  • 1
jhoepken
  • 1,842
  • 3
  • 17
  • 24
  • I'm trying to rotate on a 2D surface so I'm not sure if this will work, also I don't understand it at all so could you please explain. – George Willcox Feb 28 '16 at 21:03
  • Without knowing the details of your application, it will work for a *planar* surface, which in turn is 2D. You just have to use the surface normal vector as the rotational axis for rotational axis. This is just under the assumption that you gov for the quaternion approach. – jhoepken Feb 28 '16 at 21:07