Advertisement

Rotational Matrix confusion

Started by May 17, 2016 06:51 AM
1 comment, last by Sobe118 8 years, 4 months ago

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(
	cosf(rotationRadians),  0.0f, sinf(rotationRadians), 0.0f,
	0.0f,			1.0f, 0.0f,		     0.0f,
	-sinf(rotationRadians), 0.0f, cosf(rotationRadians), 0.0f,
	0.0f,			0.0f, 0.0f,		     1.0f);

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.

Advertisement

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);

This topic is closed to new replies.

Advertisement