Archived

This topic is now archived and is closed to further replies.

Rotation problem

This topic is 5125 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 am trying to connect two points, both of which will be moving, with a spiral. I''ve put the spiral in a vertex buffer and it goes down the z axis, beginning at z=0 and ending at z=1. I am trying to figure out how to make the matrices to make it draw it correctly between the two points. Here''s what my code is - CPoint3D is a class for a 3D point and uses floats (there are reasons I''m not using D3DXVECTOR3 but it isn''t important for this):
D3DXMATRIX matCurrent, matTransform;
CPoint3D ptDif = ptPos1 - ptPos2;

D3DXMatrixIdentity(&matCurrent);

// Make it the appropriate length

D3DXMatrixScaling(&matTransform, 1, 1, ptDif.GetMagnitude());
D3DXMatrixMultiply(&matCurrent, &matCurrent, &matTransform);
		
// Rotate around the x axis 

D3DXMatrixRotationX(&matTransform, atan2f(-ptDif.y, ptDif.z));
D3DXMatrixMultiply(&matCurrent, &matCurrent, &matTransform);

// Rotate around the y axis 

D3DXMatrixRotationY(&matTransform, atan2f(ptDif.x, ptDif.z));
D3DXMatrixMultiply(&matCurrent, &matCurrent, &matTransform);
		
// Translate it to the first object

D3DXMatrixTranslation(&matTransform, ptPos1.x, ptPos1.y, ptPos1.z);
D3DXMatrixMultiply(&matCurrent, &matCurrent, &matTransform);

// Render the stuff

If I keep the both of the points in the xz plane this works fine (because the first rotation has no effect). It also works fine if it stays in the yz plane (the second rotation has no effect). The problem is, if it moves in x, y, and z, the spiral gradually strays from the point. Apparently the rotations don''t work correctly when done together. What am I doing wrong? Do I need to use quaternions or something? If so, tell me how I need to use them, I''ve never used them before and MSDN confuses me.

Share this post


Link to post
Share on other sites
You could use quaternions...though it may not help.

D3DXQUATERNION quatRotation;
D3DXMATRIX matRotation;

D3DXQuaternionRotationYawPitchRoll(&quatRotation, atan2f(ptDif.x, ptDif.z), atan2f(-ptDif.y, ptDif.z), 0);
D3DXQuaternionRotationMatrix(&matRotation, &quatRotation);

Now just multiply your scaling matrix by your rotation matrix, then your translation matrix.

matWorldMatrix = matScale*matRotation*matTranslation;

Good luck,
Chris

Share this post


Link to post
Share on other sites
Okay, here''s exactly what I''m doing. I''m trying to draw two spheres with a spring (the spiral) connecting them. It is good enough for the spring to go from the center of one sphere to the center of the other - it doesn''t have to start and end on the actual outside of the sphere. My spheres render correctly, but when I render the spring, it doesn''t connect them correctly most of the time. As I said, I have a vertex buffer for my spring - it is centered around the z axis with length 1, so I''m trying to scale it, then rotate it and translate it to get it to connect the two objects. Hope this helps.

Share this post


Link to post
Share on other sites
I think if you look at the geometry you should realize you should have something like the following:

// Rotate around the x axis

D3DXMatrixRotationX(&matTransform, atan2f(-ptDif.y, sqrt(ptDif.z^2+ptDif.x^2)));
D3DXMatrixMultiply(&matCurrent, &matCurrent, &matTransform);

// Rotate around the y axis

D3DXMatrixRotationY(&matTransform, atan2f(ptDif.x, ptDif.z));
D3DXMatrixMultiply(&matCurrent, &matCurrent, &matTransform);


in place of:

// Rotate around the x axis

D3DXMatrixRotationX(&matTransform, atan2f(-ptDif.y, ptDif.z));
D3DXMatrixMultiply(&matCurrent, &matCurrent, &matTransform);

// Rotate around the y axis

D3DXMatrixRotationY(&matTransform, atan2f(ptDif.x, ptDif.z));
D3DXMatrixMultiply(&matCurrent, &matCurrent, &matTransform);

Share this post


Link to post
Share on other sites