# DirectionToRotation doesnt work

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

## Recommended Posts

hi i'm using following code block for Directions to Rotation (x,y,z) but it doesnt work correctly, whats the problem?
procedure DirectionToRotation(Direction,Up,Right:TD3DVector; var Rotation:TD3DVector);
var
s,c:single;
begin
s:=-Right.z;
c:=sqrt(Right.x*Right.x+Right.y*Right.y);
if c>UBEpsilon then
begin
end
else
begin
Rotation.x:=0;
end;
end;



##### Share on other sites
It looks like you're trying to perform a matrix-to-Euler-angle conversion. Unfortunately, 'it doesn't work' doesn't give us much to go on (I suppose we could just proof your code, but that's usually not a very effective way of helping someone).

There are a number of ways in which such a conversion can 'not work' (or appear not to work):

1. Difference in input and output due to floating-point imprecision

2. Difference in input and output due to aliasing of Euler-angle triples

3. Improper handling of corner cases

4. Wrong axis order (e.g. xyz instead of xzy)

5. Confusion regarding conventions (such as matrix 'majorness' or vector orientation)

The math library linked in my signature includes code for matrix-Euler conversions that supports all possible orders (xyz, xyx, zxy, etc.). Similar code is available elsewhere online as well (check the Wikipedia article on Euler angles).

##### Share on other sites
i'm filling my matrix like this
(in xyz order)
var  m:TD3DMatrix;begin  D3DXMatrixRotationX(matris,-DegToRad(Rotation.X));  D3DXMatrixRotationY(m,-DegToRad(Rotation.Y));  D3DXMatrixMultiply(matris,matris,m);  D3DXMatrixRotationZ(m,-DegToRad(Rotation.Z));  D3DXMatrixMultiply(matris,matris,m);end;

i'm using left hand coordinate system

and all examples which i found with google "matrix to euler" doesnt work correctly they go nut when angles over 90 degre or 270 or 180, when rotation be x=42 y=67 z=85 its working just like i want.

these are some methods which i found
(you dont have to read all of these many of them look like eachother)
sy:=-Direction.x;	cy:=sqrt(1-(sy*sy));	yr:=ArcTan2(sy,cy);  Rotation.y:=RadToDeg(yr);	if (sy<>1)and(sy<>-1) then	begin		Rotation.x:=RadToDeg(arctan2(Direction.y/cy,Direction.z/cy));		Rotation.z:=RadToDeg(arctan2(Up.x/cy,Right.x/cy));	end	else	begin		Rotation.x:=RadToDeg(arctan2(-Up.z,Up.y));		Rotation.z:=RadToDeg(arctan2(0,1));	end;

 x:=arcsin(Direction.y);  cosX:=Cos(x);  if cosX < 0.005 then  begin    z:=0;    y:=Sign(-Up.x)*ArcCos(Right.x);  end  else  begin    z:=Sign(Right.y)*ArcCos(Up.y/cosX);    y:=Sign(Direction.x)*ArcCos(Direction.z/cosX);    if (Sign(Up.y)=-1) and (z=0) then    begin      z:=z+pi;      y:=y+pi;    end;  end;  Rotation:=D3DXVector3(RadToDeg(x),RadToDeg(y),RadToDeg(z));

 y:=-ArcSin(Direction.x);  C:=cos(Y);  if (abs(C)>0.0005) then  begin    X:=ArcTan2( Direction.y  / C, Direction.z / C );    Z:=ArcTan2( Up.x / C, Right.x / C );  end  else  begin    X:=0;    Z:=ArcTan2(-Right.y,Up.y );  end;  Rotation:=D3DXVector3(RadToDeg(x),RadToDeg(y),RadToDeg(z));

angle_y:=ArcSin(Right.z);  c:=cos(angle_y);  if abs(C)>0.005 then  begin    angle_x:=ArcTan2(-Up.z/c,Direction.z/c);    angle_z:=ArcTan2(-Right.y/c,Right.x/c);  end  else  begin    angle_x:=0;      angle_z:=ArcTan2(Up.y,Up.x);  end;  angle_x:=RadToDeg(angle_x);  angle_y:=RadToDeg(angle_y);  angle_z:=RadToDeg(angle_z);  Rotation:=D3DXVector3(angle_x,angle_y,angle_z);

 if Up.x>0.998 then  begin    heading:=ArcTan2(Right.z,Direction.z);    attitude:=Pi/2;    bank:=0;  end  else  if (Up.x<-0.998) then  begin    heading:=arctan2(Right.z,Direction.z);    attitude:=-PI/2;    bank:=0;  end  else  begin    heading:=arctan2(-Direction.x,Right.x);    bank:=arctan2(-Up.z,Up.y);    attitude:=arcsin(Up.x);  end;  Rotation:=D3DXVector3(RadToDeg(bank),RadToDeg(heading),RadToDeg(attitude));

also these codes send by happy coders :) i mean all of these codes are working just fine for message owners, maybe they are not filling matrix like me or they are using different systems, i dont know

thanks everyone

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19

• 11
• 15
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633004
• Total Posts
3009840
• ### Who's Online (See full list)

There are no registered users currently online

×