Archived

This topic is now archived and is closed to further replies.

Guest Anonymous Poster

Angles in 2d/3d

Recommended Posts

Hi Ed,
There was a great post a couple of months back that explained how to do this & included source code. If you just check back a couple of months worth of messages you should find it. BTW it worked in full 3D, not just 2D.

Cheers
Darrin

Share this post


Link to post
Share on other sites
Hi Ed,
Here a copy of the post, I found it among some of my archived stuff.
Courtesy of Jarrod, JDS Games.


//-----------------------------------------------------------------------
// Vector Routines
//-----------------------------------------------------------------------
void GV_VectorCrossProduct(PR_POINT *dest, PR_POINT *src1, PR_POINT *src2)
{
dest->x = src1->y * src2->z - src1->z * src2->y;
dest->y = src1->z * src2->x - src1->x * src2->z;
dest->z = src1->x * src2->y - src1->y * src2->x;
}

//-----------------------------------------------------------------------
// Matrix Routines
//-----------------------------------------------------------------------
void GV_RotateMatrixToPoint(PR_MATRIX mat, PR_POINT *src, PR_POINT *dest)
{
PR_POINT i, j, k;

// calc forward vector
k.x = dest->x - src->x;
k.y = dest->y - src->y;
k.z = dest->z - src->z;
PR_Normalize(&k);

// calc up vector
j.x = 0;
j.y = 1;
j.z = 0;

// calc right vector
GV_VectorCrossProduct(&i, &j, &k);

// calc new up vector
GV_VectorCrossProduct(&j, &i, &k);

// store in rotation matrix
mat[0] = i.x;
mat[1] = i.y;
mat[2] = i.z;
mat[3] = 0;
mat[4] = -j.x;
mat[5] = -j.y;
mat[6] = -j.z;
mat[7] = 0;
mat[8] = k.x;
mat[9] = k.y;
mat[10] = k.z;
mat[11] = 0;
//mat[12] = trans->x;
//mat[13] = trans->y;
//mat[14] = trans->z;
mat[15] = 1.0;
}

Just pass in the Entity->orientation.rot_mat, the source pos of the entity and the dest pos of where you what to look. Afterwords, the entity will be pointing at the dest. If you want a higher end function that will allow you to interporlate from any orientation to the dest point across N number of frames, e-mail me and I can send it to you. I am using this in my current game to make the enemy ships find there way to the player or any object, find powerups and so forth.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi guys! Question for the more math oriented out there...

If I have an object at xyz and another at abc, can I get some type of angles to make the first object rotate and point at the second object? I imagine in 3d this might be a pain in the ass, so 2d (xy, ab) would work as well (but Im hoping for 3d!)

thanks!

- ed

Share this post


Link to post
Share on other sites