Homing Missles

Started by
2 comments, last by PsycoBlade 22 years, 10 months ago
I wasn''t sure where to put this, I guess the general discussion would be cool. OK, I''m writing a game in C++/DX8, it is a 2D side-scrolling shooter.. I have the basic engine finished and now I''m working on weapons. I want to have a bullet that emitted from my ship home in on a target. Here''s the deal, the bullet has a X and Y position and an "angle". The angle variable represents which way the bullet will move. The target of the bullet is an X and Y position. So all I need to figure out is an algoritm that will slowly change the "angle" variable until the bullet is facing the target. I was thinking of using DOT products, but that seems like overkill for a simple 2D homing missle. So, I was wondering, could people give me some suggestions on the best way to do this I would really appreciate it. Thanks! -Rich
Advertisement
I was working on a flocking algorithm a while ago that needed the boids to face in the direction of a target - I took the current angle of the boid, and the angle to the target, and interpolated the two:

nextAngle = (3*currentAngle + 2*targetAngle)/5

It needs a bit of work for it to correctly turn the missile when currentAngle & targetAngle are greater than 180 degrees apart, but it seems to work fine.

Depending on how you want the missile to turn - there''s a slight problem here in that it turns slower as it approaches the target, rather than turning at a constant rate, but it generally looks pretty good. Give it a try & see what it looks like....

Da Fish.
Try something like this...


newMissleAngle = atan2(missleY-targetY, missleX-targetX);

if(newMissleAngle-oldMissleAngle > MAX_ANGLE_VEL)
newMissleAngle = MAX_ANGLE_VEL;
if(newMissleAngle-oldMissleAngle < -MAX_ANGLE_VEL)
newMissleAngle = -MAX_ANGLE_VEL;



the higher MAX_ANGLE_VEL, the better the missle will be at tracking. Make sure I did everything right... especially that atan2 call.
it goes something like:

int targetx, targety;
int bulletx, bullety, bulletang;

// every time the target moves you calc it again
bullang = atan2(targety - bullety, targetx - bulletx);

bulletx += COS(bulletang *3.141/180) * rad;
bullety += SIN(bulletang *3.141/180) * rad;

if (bullet hit target) killbulle, hit-targer;

that''s about it i guess
ofcourse make a look up table for the SIN/COS
and don''t use 360 degrees use something like 2**x

hmm i usually use 512 for example
then i can easily do ang &= 0x1ff;

hope i helped


Arkon
[QSoft Systems]

This topic is closed to new replies.

Advertisement