Skip to main content
GameDev.net gamedev.net
🔒 Locked

getting the direction

Started by Bruno May 25, 2004 at 4:24 PM 10 replies 850+ views
Original Post
Bruno
Bruno
Hi I have an object wich is at position P , and has it''s rotations (x,y,z). With this information, is it possible to calculate where the object is facing ? The object is a light source, and i want to project a texture to the place he his facing., but i''m having troubles in calculating it. thanks for any help, Bruno
Strife
Strife
Where an object is "facing" is completely arbitrary. You have to first choose which face of an object is the "front." Then the direction the object is "facing" is simply the normal to the surface of the "front."
Zakwayda
Zakwayda
This can be done (among other ways) with matrix multiplication. Keep in mind that the order in which you apply the yaw, pitch and roll will affect the final orientation.

In your case, you would start by creating a matrix representing your x rotation. Then you would multiply this by a matrix representing your y rotation, and finally be a z rotation matrix. The appropriate matrices and how to multiply them can be found in any good 3d graphics book - try ''3D Math Primer,'' for example.

Your final matrix represents the final orientation of your object, with the three rows or columns representing the side, up and forward vectors, respectively. So your ''facing'' vector would be the third row or column of the matrix.

If you just use elevation/azimuth (or whatever it''s called) instead, there''s probably a quicker way to do it. I''m not sure if there''s a quicker way to handle three rotations (aside from using quaternions).
Bruno
Bruno
Strife, i cannot user normals in this case.

jyk, thanks for the ideia,
I already have the matrix of the object, it''s from that matrix that i''m able to extract the angles.
So, you say i have the facing vector of my mesh in the third row or colum of the matrix ?
thanks., i''l check it out, and see if that does the job.,

Bruno
Zakwayda
Zakwayda
"I already have the matrix of the object, it''s from that matrix that i''m able to extract the angles.
So, you say i have the facing vector of my mesh in the third row or colum of the matrix ? "

Yeah, if you have the matrix, you should already have the orientation vectors. Let us know if it works for you.
Bruno
Bruno
Actually it did not work

I also tried to multiply the vector (0,-1,0) by the rotation angles of the model, but no luck either.
Zakwayda
Zakwayda
"Actually it did not work "

Hmmm. I''m not sure what to tell you. An orientation matrix is generally an orthogonal basis with three axes (x, y, z), either in the columns or the rows. If it''s a 4X4 matrix, you''ll just be dealing with the upper left 3X3 portion. How is it not working?
Bruno
Bruno
Just to make sure, i''m not forgetting anything.

It''s a 4x4 matrix, d3d style.,
First a transpose it to OGL style, and i get the orientation matrix.
Then, to get the destination point, i just add the orientation to the original position, like this :

Ropes_Cubes.matrix.Transpose(tp);
dir[0] = tp.matrix.mArray[0];
dir[1] = tp.matrix.mArray[1];
dir[2] = tp.matrix.mArray[2];

Then i have,
Light_Origin;

and i calculate Light_Destiny :

Light_Destiny[0] = Light_Origin[0] - dir[0];
Light_Destiny[2] = Light_Origin[2] - dir[2];
Light_Destiny[1] = Light_Origin[1] - dir[1];

That''s about it.,
Just a note, i''m using the first position of the matrix ( 0,1,2 ) , because it gives better results ( still wrong ) than ( 4,5,6 ). With the last ones, the light projection goes totally wacko.
You spot something wrong here ?

I posted two videos ( divx )

This one, is using positions (0,1,2) of the matrix:
www.fysoftware.com/temp/shadow.avi

This one, is using positions (4,5,6) of the matrix:
www.fysoftware.com/temp/shadow2.avi

thanks,
Bruno

Zakwayda
Zakwayda
4, 5, 6 will give you nonsense results. Remember that although we''re dealing with the upper left 3X3 portion of the matrix, it''s still a 4X4 matrix. So the third row or column is actually 8, 9, 10.

Try that and see if it works any better.

Actually 4, 5, 6 is wrong even for a 3X3 matrix - it should be 6, 7, 8. Anyway, try 8, 9, 10...
Zakwayda
Zakwayda
Oops, my last post is wrong - ignore it. 4, 5, 6 is the second row or column of the matrix, as you intended it to be. But if you want a ''facing'' or ''look at'' vector, you should still try 8, 9, 10. That is, assuming z is the forward axis in your world space.

You might also try 2, 6, 10, just in case you have something transposed.
Bruno
Bruno
thanks, but still no deal.
I think i''l scrap everything, and start all from scratch

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.