How can I make an object rotate around its axis? i.e. having the moon rotating BOTH around point 0, 0, 0 and its own axis? So far I have only been able to do the point 0, 0, 0 point by using the gametime component and creating a rotation matrix.
2 Answers
Let's say we have the following:
class 2DMoon
{
Texture2D texture;
Vector2 axis;
Vector2 origin;
}
The origin point could be (0,0), but let's say it's something more complex--something like (29,43). Now let's say the texture's width is 50 and the height is 90.
To get the axis for the texture for it to rotate around, assuming you want the center, you would do the following (assuming the origin (ie. current position) and texture are loaded):
axis.X = (.5 * texture.Width); axis.Y = (.5 * texture.Height);
As you know, that would take make the axis a vector of (25,45).
As BlueRaja states above, you could then make a method that looks like this:
Rotate()
{
origin.X -= axis.X;
origin.Y -= axis.Y;
// rotation goes here
origin.X += axis.X;
origin.Y += axis.y;
}
This should work for any sort of standard texture. (And of course, you don't HAVE to have the Vector2 I made up called "axis"--it's just for easy reference.
Now, take the same logic and apply it for the 3D.
A word of advice: if you are trying to work through logic in 3D, look at the logic in 2D first. 9 times out of 10, you'll find the answer you're looking for!
(If I made any mistake during the transposition in my Rotate() method, please let me know--I'm sort of tired where I'm at, and I'm not testing it, but the rotation should work like that, no?)