[AS3] Heat Seeking Missile
Hi,
I've looked up the AS2 Heat seeking missile example, but it doesn't work for my game. The position parameters are the target. This game is also a fully scrollable terrain jet fighter game. The Missile doesn't rotate in the correct direction, doesn't turn towards the target, for example if the target is behind the missile.
Code:
public function setGuidance(index:Number,posx:Number,posy:Number, rate:Number):void{
var it=index;
var disX = 0;
var disY = 0;
var disTot = 0;
var turnRate = rate;
var moveX = 0;
var moveY = 0;
var totalMov = 0;
var rot = 0;
var moveTot = 0;
if(this.missiles[it].launched && this.missiles[it].guidance){
disX = posx - (this.missiles[it].object as Bitmap).x;
disY = posy - (this.missiles[it].object as Bitmap).y;
disTot = Math.sqrt(disX*disX + disY*disY);
if(disTot <= 1000){
rot = (this.missiles[it].object as Bitmap).rotation;
moveX = turnRate * (disX / disTot);
moveY = turnRate * (disY / disTot);
moveTot = Math.sqrt(moveX*moveX + moveY*moveY);
this.missiles[it].xspeed+=moveX;
this.missiles[it].yspeed+=moveY;
moveX = this.missiles[it].xspeed*moveX / moveTot;
moveY = this.missiles[it].yspeed*moveY / moveTot;
(this.missiles[it].object as Bitmap).x += moveX;
(this.missiles[it].object as Bitmap).y += moveY;
(this.missiles[it].object as Bitmap).rotation =180*Math.atan2(moveY, moveX) / Math.PI;
}
}