Jump to content
  • Advertisement
Sign in to follow this  
Sobe118

Rotational Matrix confusion

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

If you intended to correct an error in the post then please contact us.

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

Share this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!