# Rotating one object to track another in worldspace

This should be pretty straightforward, but its been awhile and my brain has officially turned off for the holidays :-) To keep things simple, lets assume everything is in 2D space.

I've got a large circle, who's centre will be the origin (0,0). Now lets say I've got Object A, who is sitting inside this circle at the origin, and is facing a direction (0,1), the positive y direction. Circumference of the circle shouldn't matter for what I'm trying to do. Now let's say I have two incoming parameters (which I have no control over). One of these parameters is a rotation (in Radians), and the other is a translation. If I apply this rotation to Object A while it's in the centre of the circle, followed by the translation, the object will now be on the outside of the circle but its direction will be facing the origin. In other words, the incoming parameters will translate the object to be outside but facing the centre of the circle, where it was originally inside and facing the positive y direction.

Here's where I'm stuck: If I also have a random point inside the same circle, and I want Object A to be translated to the exact same position it would normally be translated to (outside the circle) from those two incoming parameters, but facing the direction of this point instead (which could be anywhere inside the circle), what's the most efficient combination of operations to apply to it to accomplish this, assuming object A always starts in the origin, and this is the only information I have (the translation/rotation parameters, and the position of the point I wish to track). Assume the point can move around anywhere inside the circle, and the object will remain outside in the exact same position but its direction will "track" the point regardless of where it is.

UPDATE for clarification: Sometimes a picture is worth a thousand words ;-) See attached image... #1 is where the object starts at. #2 is what would happen if I apply the incoming translation and rotation parameters. #3 is what I'm trying to achieve. I do know that the "random" point is located at (0, 0.5) in this example. How do I get from #1 to #3 in the least amount of steps.

• Read it twice, understood nothing. Maybe you can rephrase the whole question in a simpler manner. Maybe draw some diagrams. – RandyGaul Dec 20 '13 at 4:23
• So... basically you are asking "how do I rotate an object to face a point?" – Josh Dec 20 '13 at 4:43
• It's not straightforward at all, because it's too abstract. So A is initially facing (0, 1), and then you want it to face (a, b) (say, unit vector), right? Then my answer would be to calculate the unit vector pointing from A to whatever point it needs to points to. But I bet that doesn't help you. Because it's abstract. – muhuk Dec 20 '13 at 6:18
• Maybe it's as simple as Josh Petrie is suggesting? If I have an object I know is facing (0,1) initially, can I do the following 1) translate using supplied parameter which puts me outside the circle into the desired location 2) ignore supplied rotation, it doesn't matter, 3) build a unit vector from new location in the (0,1) positive y direction. 4) build a vector between new position and the random point in the circle. 5) take dot product of these two vectors, use results to figure out angle between them which is what I want to rotate? – Lucky Mike Dec 21 '13 at 4:46
• Can you provide some extra information, on how the angle and translation are calculated before being passed in to the function? Is the translation vector the radius of the circle? – Syntac_ Dec 21 '13 at 12:13

After re-positioning the object based on the incoming parameters, I think you can do something like this:

lookDirection = targetPoint.position - objectA.position;
objectA.rotation = atan2(lookDirection.y, lookDirection.x);


or, if you can only set incremental roations, then the second line becomes something like...

objectA.rotate(atan2(lookDirection.y, lookDirection.x) - objectACurrentRotation);


You might need to flip a sign or adjust the angle you get from atan2 to work with your rotation notation (since you don't specify what libraries or coordinate systems you're using), but I think it can be basically as simple as this.

Just remember to do those two steps anytime objectA or its targetPoint moves after this.

• So you think the original "rotation" is still applicable to solve the problem? – Lucky Mike Dec 21 '13 at 23:22
• In the first version, I'm just overwriting that rotation, so it doesn't matter what rotation we were given as input. The second version uses that input, but it's actually extra work to compensate for it, so it's better to overwrite if you can. – DMGregory Dec 21 '13 at 23:53
• I ended up going with this solution. I'm actually working in 3D but used a 2D example for simplification; my coordinate system required me to change what I passed into the atan2 function, but the first two lines worked perfectly otherwise. – Lucky Mike Dec 23 '13 at 22:42

I believe this is what you're after.

Note that it makes more sense to pass in the distance from the center of the circle you wish object A to be placed instead of this translation vector, as it appears the rotation is determining how far around the circle objectA should be placed.

function PositionAndRotate( float distFromCenter, float angle, Vector2 objectBPos )
{
// Calculate position using polar coordinate to cartesian and set it
this.setPosition(Vector2( distFromCenter * cos(angle), distFromCenter * sin(angle) ));

// Calculate look at vector
Vector2 lookAt = objectBPos - this.getPosition();

// Get angle needed to rotate object A to face object B
float angleToRotateBy = atan2(lookAt.x, lookAt.y);

// Set rotation for object A
this.rotate(angleToRotateBy)
}


This function assumes it is a method of object A.

This is similar to DMGregory's answer but I wanted to show a more complete answer to resolve any ambiguity.

• would upvote this, but apparently my StackExchange rep doesn't carry over to the gamedev forums, and I don't have enough points yet – Lucky Mike Dec 23 '13 at 22:58