Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

12000 Posts in 1588 Topics- by 3508 Members - Latest Member: PienueDut

26. May 2012, 11:58:40 am
Xith3D CommunityGeneral CategoryGeneral Discussion (Moderators: Marvin Fröhlich, 'n ddrylliog)Rotate to shooting object
Pages: [1]
Print
Author Topic: Rotate to shooting object  (Read 703 times)
elijah
Enjoying the stay
*
Offline Offline

Posts: 72


View Profile
« on: 13. July 2009, 01:47:51 pm »

Hi EveryOne,

I'm facing  a problem. I have an object ( gun ) in 3d space. When the user select another object , the gun must move to the direction of the object. I have look at some example  the PointAndClick test. It works when it does with the view. Can somebody give some code snippets. 

God Bless
Eng Huat
Logged
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #1 on: 13. July 2009, 02:05:14 pm »

It should be quite the same for a TransformGroup? What exactly is the problem?

Marvin
Logged
elijah
Enjoying the stay
*
Offline Offline

Posts: 72


View Profile
« Reply #2 on: 13. July 2009, 06:13:43 pm »


I got this code from the internet

Code:
// O is your object's position
// P is the position of the object to face
// U is the nominal "up" vector (typically Vector3.Y)
// Note: this does not work when O is straight below or straight above P
Matrix RotateToFace(Vector3 O, Vector3 P, Vector3 U)
{
  Vector3 D = (O - P);
  Vector3 Right = Vector3.Cross(U, D);
  Vector3.Normalize(ref Right, out Right);
  Vector3 Backwards = Vector3.Cross(Right, U);
  Vector3.Normalize(ref Backwards, out Backwards);
  Vector3 Up = Vector3.Cross(Backwards, Right);
  Matrix rot = new Matrix(Right.X, Right.Y, Right.Z, 0, Up.X, Up.Y, Up.Z, 0, Backwards.X, Backwards.Y, Backwards.Z, 0, 0, 0, 0, 1);
}

It does not work when O is straight below or straight above P. I must wondering is there any other better code around. I hear that
quaternion4f can do the job in a much simpler way and does not suffer from any short falls. But I'm lost in quaternion.

The example I follows from xith-tk, when I set the lookup(), the translation is being altered. I need the translation to be the same.

I hope this explains my problem in clear detail.

God Bless
Eng Huat
Logged
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #3 on: 13. July 2009, 11:13:58 pm »

Read the translation beforehand and apply it back after.

Marvin
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic