3D Model Orientation

I am having a problem setting orientation for my 3d model my code goes like this…

Consider a GUN model made in 3d Studio Max pointing towards -z Axis ( if it matters) …

I used 3D Studio Model Class by: Matthew Fairfax Model_3DS.h to include my 3ds model in OpenGL

In rela World user Moves/rotates a Device and I have to Show it on the Screen How its rotating Moving I have two Informations Point P is the Position of the GUN
and Point Q a point the GUN is Pointing at

I know i need to do something like this

Translate(To point P)

So everytime user Changes the GUN orientation in Real World my P and Q points will be changed and In OpenGL my Model will have the same Orientation aswell…

I have Googled alot and I know It has do somethign with Euler Angles/Rotation and Quaternions But I am really Confused Can some one explain me a Bit since I am Bad at maths

How to get these Angels X Y Z
In which Order should i rotate them

Thanks in Advance

Try this.
You are trying to rotate the vector (0,0,-1) so that it aligns with the vector (Q-P). Take the cross product of (0,0,-1)x(Q-P) which will give you the axis of rotation. Then figure out the angle between (0,0,-1) and (Q-P) vectors which will be your angle of rotation. Use glRotated with the calculated axis-angle.

Remember vectors u dot v = |u||v|cos(Angle). Compute the angle using that formula.

Thanks a Lot for Reply but my problem is more than that…

With this Code my Gun starts facing towards the Q point but i can not set IF its UPSIDE down or … think of a Spry Paint User might be handling it UPside down or Handle at Right and head at left

To set it Exactly as its in the real World… I will have to rotate it on X Y and Z All three of them they are the roll, pitch, and yaw Stuff :frowning:

one thing more the (0,0,-1) does’t make any differnce I have tried with (1,0,0) and other axis aswell my Gunb alwyas face towards the q point…

I’m not sure what you mean. Your model is defined such that it’s pointing at (0,0,-1). Given Q = (0,1,1) and P = (0,0,0) what is the axis-angle that you get using your methods?

I don’t really use roll,pitch,yaw so I can’t help with that method.

Hello Best4Gotten -

Budric has given you good advice …

You have to do vector operations including subtraction, cross product, and dot product in addition to writing OpenGL code. Do you have routines to do this? If not, get them, or write them. It is fairly straightforward. Using Budric’s approach, the resulting GL code would be simpler than what you think. Only one rotation (not 3) is needed.

It would look something like …


// P -> be the gun’s position vector
// Q -> target point vector
// R -> initial pointing vector of gun


// D -> Q - P (vector subtraction)
// C -> R X D (vector cross product)
// A -> angle between R and S.

glPushMatrix ();

  glTranslate (  P );
  glRotated   (A, C);
  DrawModel   (    );


Best4Gotten - your description is vague about the orientation of the gun in the beginning and at the end. There are an infinite number of orientations which can point a gun at a target point (by rotating around the line of sight). Do you care about this? If so, it might add some code to this approach.

Good luck.

HI Budric and Maxh

Sorry for late reply and wrong information

:@ MaxH

I do have all those functions and I have done what Budric Said but it wont solve my Problem… Consider this is my Gun in 3D

If I just Rotate on ONE Axis i.e take the Angle between -Y ( as my Gun is facing at -Y by Default not -Z as i said before) And the Axis Q-P my GUN will start facing towards the Point Q

But Imagen for a Second that User has Acttualy Rotated the GUN in the WAY that its Still on P and Pointing Towards Q but the GUNS LEFT AXIS is UPWARDS and RIGHT is DownWards How would I KNOw that and How will i Change that Orientation with only two Known Points P and Q

So my Information is incomplete and Not enough to set a Proper Orientation … I guess
But If there is a Magical way to Do this I would really be intrested in that… :frowning: