#### Archived

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

# Homing missles.

This topic is 6255 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If I want a weapon to shoot homing missles, should I use that DOT product thing? If so, what do all the varibles mean? Thanks. -Rich

##### Share on other sites
Have a look at this site.
http://www.planetquake.com/code3arena/tutorials/tutorial35.shtml

Its a homing missle tutorial for a quake 3 mod. They give an explanation how it works. I think they talk about the dot product on that code3arena site. All it is, is just the angle between two vectors. Now keep in mind this angle is not in degrees.

-SirKnight

##### Share on other sites
Try this simplified version of the example called Tracking that I submitted to Chris but still needed some corrections. It consist of the use of dot products.

Basically what the programs does is that when you run it you see a magenta shaded entity folling a yellow entity around also the magenta colored entity will roll when the yellow entity rolls but will try to be aligned like the yellow entity.

The entity1 or what ever varible you want, will be your missle entity and entity2 is your target.

So the varibles dotx, doty , roll1(or could of be dotz)
are the dot products which is a mathmatical way of finding the cosine between to vectors such as:

dotx = ux1*nx + uy1*ny + uz1*nz;
doty = rhx1*nx + rhy1*ny + rhz1*nz;
roll = rhx1*ux2 + rhy1*uy2 + rhz1*uz2;

Your probably don''t need the roll1 dot product. Thus simplifing the code a bit. All a missle needs to get to the target is its pitch, yaw and thrust value.

-----------------------------------------------------------------
The ez1 and ez2 is your thrust value. ex1 and ex2 could be used for sidward movement such as strafing or doging and the varibles ey1 and ey2 are your up and down such as maybe a vtol type of craft. ex1, ex2, ey1, and ey2 or set to zero for the example.

ez1 = -11*move_count;
PR_Globals.tvector[0] = ex1;PR_Globals.tvector[1] = ey1;PR_Globals.tvector[2] = ez1;
PR_Transform(entity1->orientation.rot_mat);
mx1 = PR_Globals.tvector[0];my1 = PR_Globals.tvector[1];mz1 = PR_Globals.tvector[2];

PR_RotateEntity(entity2, cx, cy, cz);// This entity is controled by the keyboard

ez2 = -12*move_count;
PR_Globals.tvector[0] = ex2;PR_Globals.tvector[1] = ey2;PR_Globals.tvector[2] = ez2;
PR_Transform(entity2->orientation.rot_mat);
mx2 = PR_Globals.tvector[0];my2 = PR_Globals.tvector[1];mz2 = PR_Globals.tvector[2];

PR_Globals.tvector[0] = 0;PR_Globals.tvector[1] = 1;PR_Globals.tvector[2] = 0;

// Calculate direction vector to target
//This is built in distance function for calculating the distance
from the entity1 to entity2

tvect_dist1 = PR_Distance3D(entity1->orientation.location.x,
entity1->orientation.location.y, entity1->orientation.location.z ,
entity2->orientation.location.x, entity2->orientation.location.y,
entity2->orientation.location.z);

dx1 = entity2->orientation.location.x - entity1->orientation.location.x;
dy1 = entity2->orientation.location.y - entity1->orientation.location.y;
dz1 = entity2->orientation.location.z - entity1->orientation.location.z;

// Normalized vectors
nx = dx1/tvect_dist1;
ny = dy1/tvect_dist1;
nz = dz1/tvect_dist1;

// Calculate Dot Products
dotx = ux1*nx + uy1*ny + uz1*nz;
doty = rhx1*nx + rhy1*ny + rhz1*nz;

// Adjusts pursuing entity''s roll to align with target entity
roll = rhx1*ux2 + rhy1*uy2 + rhz1*uz2;

// Pitch Tracking
if(dotx < 0)
sw1 = 1;
else
sw1 = -1;

// Yaw Tracking
if(doty < 0)
sw2 = -1;
else
sw2 = 1;

// Roll Tracking
if(roll > 0)
sw3 = -1;
else
sw3 = 1;

// The following numbers are used to get the absoulute value
// All I want are only positive numbers
if(dotx < 0)
dotx = -dotx;

if(doty < 0)
doty = -doty;

if(roll < 0)
roll = -roll;

if(roll < 0)
sw3 = -sw3;

// Adjust Pursuing entity to that of targets movement and orientation

// The multiplier such as 1.2 can be used to determine your rate of rotation

// dotx, doty and roll is used to smooth out the sw1, sw2 and sw3
values since their only used to determine the direction of rotation such as 1 or -1;
pitch = 1.2*sw1*dotx;
yaw = 1.2*sw2*doty;
roll1 = 1.2*sw3*roll;

PR_RotateEntity(entity1, pitch*move_count, yaw*move_count, roll1*move_count);

//entity1 tranformed up vector
PR_Globals.tvector[0]=0;PR_Globals.tvector[1]=-1;PR_Globals.tvector[2]=0;
PR_Transform(entity1->orientation.rot_mat);
ux1 = PR_Globals.tvector[0];
uy1 = PR_Globals.tvector[1];
uz1 = PR_Globals.tvector[2];

//entity2 transformed up vector
PR_Globals.tvector[0]=0;PR_Globals.tvector[1]=-1;PR_Globals.tvector[2]=0;
PR_Transform(entity2->orientation.rot_mat);
ux2 = PR_Globals.tvector[0];
uy2 = PR_Globals.tvector[1];
uz2 = PR_Globals.tvector[2];

// entity1 transformed right vector
PR_Globals.tvector[0]=-1;PR_Globals.tvector[1]=0;PR_Globals.tvector[2]=0;
PR_Transform(entity1->orientation.rot_mat);
rhx1 = PR_Globals.tvector[0];
rhy1 = PR_Globals.tvector[1];
rhz1 = PR_Globals.tvector[2];

// Tranlation of entitys based on orientation per entity
PR_MoveEntity(entity2, mx2, my2, mz2);

PR_MoveEntity(entity1, mx1, my1, mz1);

Hope this helps

Doug

##### Share on other sites
Well, I need some advice on how to use this on only the Z axis. The game I have homing missles in is a side-scroller, so the only thing needed will be to check if the missle is facing the given coordinate by rotating the object on the Z axis.

      ____     /    \    /      \   /        \   \        B    \     \      \      A

so it comes out of point A going up, then it arcs around to get to point B. The missle moves based on the direction it is facing (that part of the programing is already done), I just need something that will rotate the entity until it is facing the destination coordinate. Thanks!
-Rich

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631788
• Total Posts
3002356
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!