Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

i was wondering if anyone could give me some advice on how to rotate one object around another moving object. After a bit of googling i found some code on another website that worked perfectly but it would only work on staitionary objects. The code that i used:

Vector3 temp = objectPosition - objectToRotateAboutPosition;
temp = Vector3.Transform(temp, Matrix.CreateRotationX(angle));
objectPosition = objectToRotateAboutPosition + temp;

It works perfectly on the staitionary object but on a moving object it seems to object that is orbiting the main object seems to distance itself a great distance before it returns to the original position. I was wondering if anyone knew a way that i could tweak this code blocka and make it work, or maybe a different way to rotate one object around a moving object.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

That is good code for doing the orbit. It can be shortened to the following line but probably wont have a perf increase:

objectPosition = Vector3.Transform(ObjectPosition - objectToRotateAboutPosition, Matrix.CreateRotationX(angle)) + objectToRotateAboutPosition;

Now about the dynamic of the orbit center being in motion. After completing your code snippet there, simply move the orbiting object the same as the orbit center is has moved.

//after your snippet
orbitCenterChange = thisFrameObjectToRotateAboutPosition - lastFrameObjectToRotateAboutPosition;
objectPosition += orbitCenterChange;

Or, if you don't keep track of last frame's position but you have a velocity vector for the object you are orbiting around:

//after your snippet
objectPosition += orbitCenterVelocity * dt;
share|improve this answer
    
Hi, I wasn't keeping track of the frames position but I think I will try to implement it since I don't have a velocity vector. I was wondering if by frame, you just mean hold the previous and current position of the object per update. –  Habeeb Adisa Mar 26 '13 at 12:11
    
And also could you tell me what the variable dt is in the last code block I might try both ways. –  Habeeb Adisa Mar 26 '13 at 12:17
    
yes, store last frame's position of the orbited object and subtract the current frame's orbited object's position. Ultimately, you have to invoke the same motion on the orbiting object that you are invoking on the orbited object. So the difference in position of the orbited object from last frame to the current frame represents it's motion since last frame. add that difference to the orbiting object and they are moving together. –  Steve H Mar 26 '13 at 14:06
    
dt means 'delta time'. It's the amount of time that's elapsed since the last update. In Xna it is float dt = (float)gameTime.ElapsedGameTime.TotalSeconds; –  Steve H Mar 26 '13 at 14:09
    
Unfortunately, i have tried both ways but they both yield the same result. The orbiting object makes a large circle far away from the original object before it meets back with the object at the same point from which it started. Since both ways did the same thing I'm thinking that i'm doing it right but i might have wrong values somewhere. I'll keep trying, and thanks for your help. I also decided to use your smaller line of code since it will be easier to remember if I ever need to use it again. Thanks alot. –  Habeeb Adisa Mar 26 '13 at 17:40

Translate the "other" object to the origin. Do the rotation. then Translate back.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.