Archived

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

how do I re-align orientation?

This topic is 6361 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

normally, we have 3 axis, X=(1,0,0); Y=(0,1,0); Z=(0,0,1); Now, if I know that Z has changed.. say to (0.3, 0.3 0.5) or something, how can I figure out the other axis'' orientations? I know that I can find X by Y cross Z, but how do I find the correct Y in the first place?

Share this post


Link to post
Share on other sites
bishop: that would never work

basically, if you have z and you want to figure out x and y, you''re gonan have infinite solutions
think of it this way:
imagine the z axis as a line and draw a plane perpendicular to that axis
now x and y can be anywhere on that plane, as long as they make a 90 degree angle (x dot y = 0) and they both pass thru the point that z axis crosses the plane (better say origin, which they all pass through it anyways, so...)

----------------
- Pouya / FOO!!!
***CENSORED***

Share this post


Link to post
Share on other sites
To go from an axis aligned cooridnate system (identity) to one defined by the 3 axes X, Y and Z use this matrix:
Xx Xy Xz 0
Yx Yy Yz 0
Zx Zy Zz 0
0 0 0 1
I''m pretty sure about the above matrix and positive about the below matrix.

To go from a cooridnate system aligned with the three axes X, Y, and Z to the axis aligned cooridnate system (identity) use this matrix:
Xx Yx Zx 0
Xy Yy Zy 0
Xz Yz Zz 0
0 0 0 1



Share this post


Link to post
Share on other sites
You''re right, I''ve figured it out.. however I have another problem now.

at first my camera''s 3 axis is aligned with the regular xyz axis,
position at (0,0,someZ)..

next time I move the camera, I get a newZ, I figure out NewX and NewY with..

NewX = NewZ x LastY
NewY = NewX x NewZ;

So at each frame, my camera is moving, and I do this every frame based on the lastY.. After a while, if I go back to (0,0,someZ), my camera is skewed.. I assume this is because of floating point error.
At this point, my newZ is (0,0,someZ) but my lastY could be (0.2138,0.8550505,0.11202) you get the point.. Distorted..
How can I avoid this?

Altho in my final application, I plan to specify in an OpenGL kind of way.. I.e. I''m specify it like glLookAt(Eyepos, PointofInterest, ViewUpVector).. Where I specify my own ViewUpVector each frame, that could help right?

Any insight is appreciated... (Quaternions???)


Share this post


Link to post
Share on other sites
quote:
Original post by jho

next time I move the camera, I get a newZ, I figure out NewX and NewY with..

NewX = NewZ x LastY
NewY = NewX x NewZ;




NewX = NewZ x LastY isn''t exactly what you want. My original answer assumed you had specified Z and an Up vector (Y) that possibly wasn''t at 90 degrees to Z but was defining the Plane of Z and Y. In this way, your Up vector (Y) determines camera roll. In that case, you get:
X = Y x Z
and then clean up Y
Y = Z x X

What you are doing however is assuming that as your camera changes position it is staying ''Up''. Maybe this is what you want (i.e. the camera always stays aligned with Up). If it is what you want, fine.

As for the skew, make sure your vectors are normalized and keep cleaning up Y.






Share this post


Link to post
Share on other sites
For your question about a setting LookFrom -> LookAt camera, here is the code i use. You have to specify the RollAngle, too, just the way it is used in 3DS Max. It is measured counterclockwise order, from the horizontal identity plane. If you are not using left-handed system, just change the sign in the cross product, and i think that''s all. And if you are using a non-column oriented matrix system, traspose the resulting matrix. For a deeper explanation about what is happening, just ask about the part you don´t understand. At first thi may seem quite complicated, but it''s only algebra.

Sorry, this is my own 3D stuff mixed with D3D, but hope you can figure out

    
typedef struct
{
Vector LookAt;
Vector LookFrom;
float RollAngle;
}ViewStruct,*lpViewStruct;

BOOL
D3DGraphics::SetView(ViewStruct& view)
{
if(!RenderState) return FALSE;
if(!D3DEnabled) return FALSE;

HRESULT h;
D3DMATRIX Mat;

Vector c(view.LookAT-view.LookFrom);

D3DVECTOR vA;
vA.x=c.x;
vA.y=c.y;
vA.z=c.z;

D3DVECTOR vB;
vB.x=-vA.z;
vB.y=0.0f;
vB.z=vA.x;

D3DVECTOR vC=-CrossProduct(vA,vB);

vA/=Magnitude(vA);
vB/=Magnitude(vB);
vC/=Magnitude(vC);

float ca,sa;
ca=(float)cos(view.RollAngle);
sa=(float)sin(view.RollAngle);

Mat._11=(vB.x*ca-vC.x*sa);Mat._12=(vB.x*sa+vC.x*ca);Mat._13=vA.x;Mat._14=0.0f;
Mat._21=(vB.y*ca-vC.y*sa);Mat._22=(vB.y*sa+vC.y*ca);Mat._23=vA.y;Mat._24=0.0f;
Mat._31=(vB.z*ca-vC.z*sa);Mat._32=(vB.z*sa+vC.z*ca);Mat._33=vA.z;Mat._34=0.0f;

float dB,dC;
dB=-(vB.x*view.LookFrom.x+vB.y*view.LookFrom.y+vB.z*view.LookFrom.z);
dC=-(vC.x*view.LookFrom.x+vC.y*view.LookFrom.y+vC.z*view.LookFrom.z);

Mat._41 = dB*ca-dC*sa;
Mat._42 = dB*sa+dC*ca;
Mat._43 = -(vA.x*view.LookFrom.x+vA.y*view.LookFrom.y+vA.z*view.LookFrom.z);
Mat._44=1.0f;

h=lpD3DDevice->SetTransform(D3DTRANSFORMSTATE_VIEW,&Mat);
if(FAILED(h))
{
D3DE(h);
return FALSE;
}
return TRUE;
}

Share this post


Link to post
Share on other sites
"Cleaning up Y"

When you specify camera "look at" arguments, they are typically the direction you are looking towards, which transforms to the Z axis and the "up" vector, which transforms to the Y axis. The thing is though, there is no requirement that you make the "up" vector exactly 90 degrees to the "look at" vector. Just as long as "up" is different from "look at", is sufficient to define a plane. From this, we derive the X axis. To "clean up" Y, we need to make it 90 degrees to Z, and since we now have calculated X we can get the proper Y by crossing Z and X.


Share this post


Link to post
Share on other sites