# Rotational Matrix confusion

This topic is 940 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm having issues with rotating 3D objects towards a location.

As a test I tried to rotate every object towards the origin.

The main part of the code is below along with a resulting image.

(fyi my unit system is not standard, x-horazantal, z-depth(vertical from this images perspective), y-up(towards camera)

When the objects are positioned towards a point the flat surface points towards the target, looks perpendicular. The red lines drawn show when the objects point at the center correctly. If I add PI/2 to the rotation then the diagonals point at the center.

The resulting image should form a layered ring like design of  aligned objects.

But it seems to create a series of 4 hyperbolas.

I'm guessing I made a simple error.

float3 origin = {0.0f, 0.0f, 0.0f};
float3 unitVector = normalize(make_float3(origin.x-object.x,
origin.y-object.y,
origin.z-object.z));

//x,z are the surface plain : y is vertical : atan2f returns -PI to PI
float rotationRadians = atan2f(unitVector.x, unitVector.z);

XMMATRIX rotateMat = XMMATRIX(
0.0f,			1.0f, 0.0f,		     0.0f,
0.0f,			0.0f, 0.0f,		     1.0f);


##### Share on other sites

Well, atan2f takes (vertical, horizontal) as coordinates and assumes a right handed coordinate system. If y is towards, x goes right and z goes down.

Try calling atan2f(-z, x) instead. The signs of course depend on your coordinate system axis, so you might need to switch some signs around.

##### Share on other sites

Thanks, that was the right part to look at for the issue.

For my coordinate system the code below created the correct visual appearance (objects are symmetrical on one plane though)

rotationRadians = atan2f(unitVector.z, unitVector.x);
rotationRadians = atan2f(unitVector.x, -unitVector.z);
rotationRadians = atan2f(-unitVector.x, unitVector.z);


1. 1
2. 2
3. 3
Rutin
13
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633697
• Total Posts
3013407
×