Rotation problem

Started by
5 comments, last by timmay314 20 years, 4 months ago
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.
Advertisement
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
Chris ByersMicrosoft DirectX MVP - 2005
OK, I tried that and it looks the same . This doesn''t make any sense...anyone else have an idea?
Could you describe what you''re trying to do a little more detailed, while being simple still? I didn''t quite comprehend what you are attempting in the first post.

Chris
Chris ByersMicrosoft DirectX MVP - 2005
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.
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);
It''s a good thing stupidity isn''t a capital offense. If it were I''d be six feet under.

Thank you very much .

This topic is closed to new replies.

Advertisement