• Advertisement
Sign in to follow this  

Angle Between two 3d vectors and object's direction.

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

Hi, I need to adjust object's direction in 3d. I have current point vector , and destination point vector. When current point == destination point, destination point gets a new random position. The poblem that i can't torn and adjust object to a new direction. i need to use function: glRotatef (XAngle, 1.0f, 0.0f, 0.0f); glRotatef (YAngle, 0.0f, 1.0f, 0.0f); glRotatef (ZAngle, 0.0f, 0.0f, 1.0f); but i can't calculate XAngle,YAngle,ZAngle. Please help. P.S. function don't work void DCreature::MobilisInMobile() { if(StepSize>=1) { InitPoint=DestPoint; float x1 = (rand()%2==0?-1:1)*(rand()%(-10))+((float)rand()/RAND_MAX ); float y1 = (rand()%2==0?-1:1)*(rand()%(-10))+((float)rand()/RAND_MAX ); float z1 = (rand()%2==0?-1:1)*(rand()%(-10))+((float)rand()/RAND_MAX ); DestPoint=Dpoint(x1,y1,z1); StepSize=0; } Dpoint DeltaXYZ(DestPoint-InitPoint); CurrPoint=InitPoint; CurrPoint+=DeltaXYZ*StepSize; //Angle Between two 3d vectors and object's direction /* Dpoint D; D.GetDistance(CurrPoint,DestPoint); if( D.z!=0) YAngle = atan( D.x / D.z ); else YAngle=0; YAngle = TODEG(YAngle); float len = sqrt( D.x * D.x + D.z * D.z ); if(len!=0) XAngle = atan(D.y / len ); else XAngle = 0; XAngle = TODEG(XAngle); */ //normal1 = Cross( WORLD_UP, CAMERA_DIR ) //normal2 = Cross( CAMERA_UP, CAMERA_DIR ) //roll = angle_between( normal1, normal2 ) // ComputeRPY(); glPushMatrix(); glTranslatef(InitPoint.x+DeltaXYZ.x*StepSize, InitPoint.y+DeltaXYZ.y*StepSize,InitPoint.z+DeltaXYZ.z*StepSize); DrawCreature(); glPopMatrix(); // StepSize+=Speed; }

Share this post


Link to post
Share on other sites
Advertisement
Hi,
did you consider using gluLookAt() function? It has 9 parameters. First 3 are point you are now at. Next 3 are point you are looking to. And the last 3 are Camera's Up vector (u can use 0,1,0). This way you can always look in the direction you specify directly, without need of computing the angles.

Hope this helps a little :)
Y.

Share this post


Link to post
Share on other sites
thank you yanco for a reply.
the point is that i don't need to change the camera view.
the objects must move in 3d space and the head of the objects
have to look at the direction point.

here is the function:

void DCreature::MobilisInMobile()
{
Dpoint DeltaXYZ(DestPoint-InitPoint);

CurrPoint=InitPoint;
CurrPoint+=DeltaXYZ*StepSize;

glPushMatrix();
glTranslatef(InitPoint.x+DeltaXYZ.x*StepSize, InitPoint.y+DeltaXYZ.y*StepSize,InitPoint.z+DeltaXYZ.z*StepSize);

//how to rotate?
// glRotatef (XAngle, 1.0f, 0.0f, 0.0f);
// glRotatef (YAngle, 0.0f, 1.0f, 0.0f);
// glRotatef (ZAngle, 0.0f, 0.0f, 1.0f);

DrawCreature();
glPopMatrix();

StepSize+=Speed;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by alicaman1
the point is that i don't need to change the camera view.
the objects must move in 3d space and the head of the objects
have to look at the direction point.

gluLookAt() post-multiplies the currently 'active' matrix. Note, this matrix doesn't have to be the matrix for your _camera_, so you should be able to use it instead of the glTranslate/glRotate calls to simultaneously place your object in desired spot (eyex, y, z) and have it look in desired direction (centerx, y, z) ^^;;

Share this post


Link to post
Share on other sites
sorry, i use 'manual' lookAt function so didn't really play with glu version of it >.<; if it helps any, simple code to build look-at matrix goes somewhat like:

matrix4&
matrix4::set_lookAt( const vector3& Eye, const vector3& Target ) {

vector3 direction = Target - Eye; direction.normalize();
vector3 right;
if( math::is_zero( direction.x ) && math::is_zero( direction.z ) )
right = cross( direction, vector3::unit_x );
else right = cross( direction, vector3::unit_y );
right.normalize();
vector3 up = cross( right, direction );

set_identity();
// x, y, z and t are 1st, 2nd, 3rd and 4th matrix column, respectively
x = right;
y = up;
z = -direction;
t = Eye;

return *this;
}



then in the draw call for your creature you'd use it somewhat like this:

// pseudocode
// eye is location where the creature should currently be
// target is the spot to look at
glPushMatrix();
matrix4 transformation; transformation.set_lookAt( eye, target );
glMultMatrixf( transformation );
drawCreature();
glPopMatrix();

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement