# Combining rotations?

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

## Recommended Posts

I'm kind of stuck here, basically I have: D3DXVECTOR3 dir = end - origin; D3DXVector3Normalize(&dir, &dir); float xrot = atan2(dir.x, 0); float yrot = atan2(dir.y, 0); float zrot = atan2(dir.z,0); D3DXMATRIX rot, rotx,roty,rotz; D3DXMatrixRotationX(&rotx, xrot ); D3DXMatrixRotationY(&roty, yrot ); D3DXMatrixRotationZ(&rotz, zrot ); rot = rotx*roty*rotz; ->set world transform as rot, then render and it doesn't work, it seems to only take in rotx's rotation while ignoring the rest, if I reorder it to be roty*rotx*rotz it instead takes roty's rotation and ignores the rest. All I'm trying to do is rotate around the x axis, then the y axis then the z axis and combine all those rotations to make one rotation around all those axis's. What am I doing wrong?

##### Share on other sites
Did you get this:
float xrot = atan2(dir.x, 0);float yrot = atan2(dir.y, 0);float zrot = atan2(dir.z,0);
From a tutorial, or perhaps from someone else's code? Or was it just something you thought might work?

Either way, I'm having a hard time seeing how the above code could return anything useful. Consider for example a vector pointing directly along the positive y axis. Using the above code, you'd get the following angles (converted to degrees, and assuming atan2() will return '0' when the input is the zero vector):
xrot = 0yrot = 90zrot = 0
Which I doubt is what you're looking for.

I'm going to guess that what you're after here is some sort of billboarding effect, or perhaps a way to align an NPC toward a target. There are a few different methods that can be used here, each with different advantages and disadvantages.

Could you describe what it is exactly that you're trying to do? That would make it easier to offer more specific advice.

##### Share on other sites
I'm trying to create a rotation matrix from a directional vector.

##### Share on other sites
Maybe you want to construct a lookat matrix?

http://stackoverflow.com/questions/349050/calculating-a-lookat-matrix

##### Share on other sites
Well basically I'm trying to draw a set of pipes where I have a start point and an end point. I can scale them and set their positions just fine but I can't figure out how to do their rotations, right now what I'md oing is breaking the rotations off into two bits: one bit I rotate around the y axis, the other I rotate around the z axis, however when I try to multiply them they don't work..

##### Share on other sites
Do I understand what you're trying to do? Take an object with an initial axis of (x,y,z) and rotate it to an orientation of (dx,dy,dz).

If so (this is off the top of my head and I probably have the cross product reversed or something):
Normalize both the initial axis (axis) and the new orientation vector (dir).
D3DXVECTOR3 ortho;D3DXVec3Cross(&ortho, &axis, &dir); // get the axis of rotationfloat rotAngle = acos(D3DXVec3Dot(&axis,&dir));D3DXMATRIX rot;D3DXMatrixRotationAxis(&rot,&ortho,rotAngle);world = scale * rot * translationMatrix;dev->SetTransform(D3DTS_WORLD,&world);// draw the pipe

You'll probably have to check the sign of the angle of rotation for angles > pi or something. I can never remember that stuff without playing with it.

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

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

• Total Topics
633717
• Total Posts
3013511
×