Advertisement

Rotate object to face another.

Started by September 06, 2002 07:28 AM
7 comments, last by B_old 22 years, 5 months ago
Hello! I need to calculate the angle a object must be rotated to face another one, I saw some simlar treads here but they do not solve the basic problem. If you reply, please start from the beginning as I have no experience with this. I would love to get a value between 0 and 360. Thanks for your help!
Hi,

2D or 3D?


fatal error C1004: unexpected end of file found
Swedish: #Gamedev.se on EFNET
Advertisement
the way i saw this,
first you need some vector for the direction you want to face
then an another for the direction that you currently are facing

take the dot product of them (not sure if you need to normalize before)
that will be an angle between the two directions

i don''t remember what i did next

i think the last time i did this
i was doing some space game

i started by drawing lots on lines on the screen
indicating the directions and any other information that was used
and i eventaully figured it out

maybe try this (got it from some old version of my old space game)
i think when i look at it,
it do it by just linear interpolation
it worked for me
but i may be hard to work for anyone else
===========================

void ShipManager_ScoutFlyer_ShipObject::rotateatdirection(D3DXVECTOR3 &face_dir,float time)
{
D3DXVECTOR3 dir;

D3DXVec3Normalize(&face_dir,&face_dir);
D3DXVec3Normalize(&this->obj->dir,&this->obj->dir);

dir=face_dir-this->obj->dir;

this->obj->dir=this->obj->dir+dir*time;

D3DXVec3Normalize(&this->obj->dir,&this->obj->dir);

calc_YawPitchRoll_mat(&this->obj->rmat,&this->obj->right,&this->obj->up,&this->obj->dir,0,0,0);
vect2view(&this->obj->rmat,&this->obj->right,&this->obj->up,&this->obj->dir);

}

/*
extracts a rotation matrix to m from 3 vectors right,up,direction(r,u,d)
*/
void vect2view(D3DXMATRIX *m,const D3DXVECTOR3 *r,const D3DXVECTOR3 *u,const D3DXVECTOR3 *d)
{
m->_11=r->x;
m->_12=r->y;
m->_13=r->z;
m->_14=0;

m->_21=u->x;
m->_22=u->y;
m->_23=u->z;
m->_24=0;

m->_31=d->x;
m->_32=d->y;
m->_33=d->z;
m->_34=0;

m->_41=0;
m->_42=0;
m->_43=0;
m->_44=1;
}
/*
updates the yaw pitch row rotation matrix in mat
rotates right,up,direction vectors (r,u,d)
by rx,ry,rz
if mat is null, doesn''t compute matrix
*/
void calc_YawPitchRoll_mat(D3DXMATRIX *mat,D3DXVECTOR3 *r,D3DXVECTOR3 *u,D3DXVECTOR3 *d,float rx, float ry,float rz)
{
D3DXMATRIX matt;

D3DXMatrixRotationAxis(& matt, r,rx);

D3DXVec3TransformCoord(d, d, & matt);
D3DXVec3TransformCoord(u, u, &matt);

if (mat)
D3DXMatrixMultiply(mat,mat,&matt);

D3DXMatrixRotationAxis(& matt,u,ry);

D3DXVec3TransformCoord(d, d, & matt);
D3DXVec3TransformCoord(r, r, &matt);
if (mat)
D3DXMatrixMultiply(mat,mat,&matt);

D3DXMatrixRotationAxis(& matt,d,rz);

D3DXVec3TransformCoord(u, u, & matt);
D3DXVec3TransformCoord(r, r, &matt);
if (mat)
D3DXMatrixMultiply(mat,mat,&matt);

//orthonormalize
D3DXVec3Normalize(d,d);
D3DXVec3Cross(r,u,d);
D3DXVec3Normalize(r,r);
D3DXVec3Cross(u,d,r);
}

===========================
Thats not a very nice explination for a beginner

/D

fatal error C1004: unexpected end of file found
Swedish: #Gamedev.se on EFNET
Oh, here I am again.
2D, Dies_Irae.
sflare, umm OK. I would say this is more I could possibly have ment to ask for but anyway, thanks.

Well, anyway after some serious thinking (or should I say confused trial&error) I figured a solution for me!

Here it goes (not that anybody would care)

dx = mouse_left - 512; //The player is in the middle of a
dy = 384 - mouse_top; //1024*768 screen

player_radian = atan2(dx,dy);
player_angle = player_radian * 180/pi;

if (player_angle < 0)
player_angle = player_angle + 360;
//As I said I prefer a value between 0 and 360

Yeah the code does only work for one resolution with a fixed playerposition I am trying to alter that soon though.

Thanks for your support!
Oh, here I am again.
2D, Dies_Irae.
sflare, umm OK. I would say this is more I could possibly have ment to ask for but anyway, thanks.

Well, anyway after some serious thinking (or should I say confused trial&error) I figured a solution for me!

Here it goes (not that anybody would care)
________________________________________________________________
dx = mouse_left - 512; //The player is in the middle of a
dy = 384 - mouse_top; //1024*768 screen

player_radian = atan2(dx,dy);
player_angle = player_radian * 180/pi;

if (player_angle < 0)
player_angle = player_angle + 360;
//As I said I prefer a value between 0 and 360
_______________________________________________________________
Yeah the code does only work for one resolution with a fixed playerposition I am trying to alter that soon though.

Thanks for your support!
Advertisement
object position Ap pointing in direction of Ad wants to point to object at position C

vector3d V=C-Ap
float angle = arccos((V.Ad)/(|V|*|Ad|))

the angle doesnt tell you which direction is shortest, ill leave that up to you

in 3d, let N=normalised(VxAp)
now build a quaternion Q=[cos(angle/2),sin(angle/2)[N]]
the rotation in the opposite direction is Q*=[cos(angle/2),-sin(angle/2)[N]]

D3DX contains quaternion helper functions and converting to matrices. if you''re not using directX, the equivalent matrix for quaternion [w,x,y,z] is in the gamasutra article "Rotating Objects Using Quaternions" along with a load of other stuff

********


A Problem Worthy of Attack
Proves It''s Worth by Fighting Back
spraff.net: don't laugh, I'm still just starting...
sorry
for anything new
i just like seeing the answer (usually)
and then either figure what''s going on by looking over it slowly or just copy it and ignore the deatails for a while
sorry
for anything new
i just like seeing the answer (usually)
and then either figure what''s going on by looking over it slowly or just copy it and ignore the deatails for a while

This topic is closed to new replies.

Advertisement