#### Archived

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

# Homing Missles

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

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
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.

##### Share on other sites
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;

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]

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

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001781
×