Heat Seeking Missile


(Zouhair Elamrani Abou Elassad) #1

Hi,

I’m trying to create a Heat Seeking Missile that follows the enemy, in my case and for test purposes ’ The Mouse '.

First of all i changed to transformation point to the center of my missile then its rotation to rotate toward the mouse, now i try to make follow the mouse, i’ve been reading about it in this tutorial:

But i guess i have a misconception between the AS3 rotate and the FlashPunk angle :

 public function Missle(x:Number=0, y:Number=0, graphic:Graphic=null, mask:Mask=null) 
	{
		_missile = new Image(MISSILE_ART);
		mask = new Pixelmask(MISSILE_ART);
		
		_missile.centerOO();
		
		super(x, y, _missile, mask);
		
	}
	
	override public function update():void 
	{
		
		var targetX:int = FP.world.mouseX;
		var targetY:int = FP.world.mouseY;
		
		_missile.angle = Math.atan2(targetY, targetX) * 180 / Math.PI;
		
		var vx:Number = _speed * (90 - Math.abs(_missile.angle)) / 90;
		var vy:Number;
		
		if (_missile.angle < 0)
			vy = -_speed + Math.abs(vx);//Going upwards.
		else
			vy = _speed - Math.abs(vx);//Going downwards.
		 
		x += vx;
		y += vy;
		
		super.update();
	}

what do you guys think ?


(Jacob Albano) #2

How about doing it the Flashpunk way:

_missle.angle = FP.angle(x, y, world.mouseX, world.mouseY);

If I’m not mistaken, Flashpunk angles are backwards compared to regular Flash (a holdover from Gamemaker, which Chevy based a lot of the engine on). You might need to reverse that value when doing math with it. Also bear in mind that Flashpunk angles are within the range [0, 360], where Flash angles are between [0, 180] and [-180, 0].

By the way, you don’t want to be using FP.world for things like this. Just use the world property on any Entity class.


(Zouhair Elamrani Abou Elassad) #3

I see, but from where exactly are the angles calculated in both Flash and FlashPunk ?


(John Andersson) #4

I’ve been trying to create proper heat-seeking missile behavior.

    'target' is an int which is used when the missile is spawned, to give it a target of where you point the gun, 'target2' is an int which is used to move the target toward the nearest soldier

var nearest:Soldier = world.nearestToEntity("soldier", this) as Soldier;
target2 = FP.angle(x, y, nearest.x, nearest.y);
		
    //If the target is close to the opposite of the missile's angle
if (target2 < target - 45 || target2 > target + 45)  target2 = (target2 + 360) % 360;
		
    //Turn the missile
if (target2 > target + 5) target += turnSpeed;
if (target2 < target - 5) target -= turnSpeed;

    //Move the stupid missile
xSpeed = speed * Math.cos(target * FP.RAD);
ySpeed = speed * Math.sin(target * FP.RAD);

Sometimes, the missile does these stupid full turns when a slight turn to the left or right would suffice. I know it has to do with if

(target2 > target + 5) target += turnSpeed;
if (target2 < target - 5) target -= turnSpeed;

and I tried to fix it by making something like

xSpeed = speed * Math.cos(((target + target2)/2)* FP.RAD);
ySpeed = speed * Math.sin(((target + target2 )/2)* FP.RAD);

But that didn’t work for some obvious reason which is ambigous to me


(Kyle) #5

Why not just use soldier.moveTowards?


// in the update
moveTowards(targetX, targetY, speed);


(Jacob Albano) #6

moveTowards will move the entity in a straight line. John wants to have his missile gradually home in on its target.


(Zouhair Elamrani Abou Elassad) #7

… I’ve been doing some research, i found an example of HomingMissile with FlashPunk, this is the link :