Welcome, Guest. Please Login or Register.  • Help
SMF Underground
+ SHMUP-DEV » SHMUP DEV FORUMS » Assistance
|-+ Adjusting a homing missiles angle! (AS3)

Pages: [1]   Go Down
0 Members and 1 Guest are viewing this topic. Topic Tools  
Read February 21, 2011, 01:42:21 AM #0
Matt McFarland

Adjusting a homing missiles angle! (AS3)

Hi guys!

I'm using homing missiles in my game, and the missile movement is excellent.  It curves around and homes in perfectly.

Code:
package game_objects.weapons
{
import flash.display.BitmapData;
import game_objects.baddie;
import game_objects.explode;
import net.flashpunk.Entity;
import net.flashpunk.graphics.Emitter;
import net.flashpunk.graphics.Graphiclist;
import net.flashpunk.graphics.Image;
import net.flashpunk.tweens.misc.VarTween;
import game_ui.gameworld;
import net.flashpunk.utils.Ease;
import net.flashpunk.FP;

/**
* ...
* @author Matt McFarland
*/
public class rocket extends Entity
{
//Upgradeable vars
protected var damage:Number;

//Graphical vars
protected var _rocket:Image;
protected var rocketTrail:Emitter;

//Movement vars
protected var launchPhase:int;
protected var rocketLaunch:VarTween;
protected var vx:Number=0;
protected var vy:Number=0;
protected const TURN_SPEED:Number = .1;
protected const SPEED:Number = 3;

//targeting system
protected var targetAquired:Boolean = false;
protected var target:baddie;
protected var targetDir_x:Number;
protected var targetDir_y:Number;
protected var angle:Number;

/**
* Runs when rocket is created.
*/
override public function added():void
{
//set damage and launch phase
angle = 180;
damage = 10;
launchPhase = 0;
//set random coordinates
x = gameworld.Player.x - 10 + Math.random() * 20;
y = gameworld.Player.y - 10;
//create graphics
_rocket = Image.createRect(4,8);
graphic = _rocket
//set hitbox
setHitbox(10, 10);
//set initial movement pattern.
rocketLaunch = new VarTween(nextPhase);
rocketLaunch.tween(this, "y", y - 20, .5, Ease.quadOut);
addTween(rocketLaunch, true);
}


/**
* Runs on each frame.
*/
override public function update():void
{
if (launchPhase == 2) return;
CheckCollisions();
if (launchPhase == 0) return;
if (launchPhase == 1 && !targetAquired) getNewTarget();
if (launchPhase == 1 && targetAquired) chaseTarget();
//polar to cartesian conversion
vx = SPEED * Math.sin(angle);  
vy = SPEED * Math.cos(angle);
_rocket.angle = angle;
trace (_rocket.angle);

//update missiles cartesian position

x += vx;
y += vy;

}

/**
* Increments the launchphase. 0 = launch, 1 = homing, 2 = exploding
*/
protected function nextPhase():void
{
launchPhase ++;
}


/**
* Checks for collisions with any baddie
*/
protected function CheckCollisions():void
{
var b:baddie = collide("baddie", x, y) as baddie;
if (b)
{
b.takeDamage(damage);
nextPhase();
kaboom();
}
}


/**
* Gets a new target and stores it as a variable
*/
protected function getNewTarget():void
{
target = world.nearestToEntity("baddie", this) as baddie;
targetAquired = true;
}


/**
* Chases current target.
*/
protected function chaseTarget():void
{
//if the target has changed then find another one.
if (!target ||
target.health < 1) {
targetAquired = false;
return;
}
//make vector pointing from missile to target

targetDir_x = (target.x - x);
targetDir_y = (target.y - y);

//rotate missile 90 deg from (x,y) to (y,-x)
//then if the dot product says its perpendicular, it actually means its parallel
//if the line from missile to target is parallel with the angle of the missile,
//the missile is pointing at the target//

if ( vy * targetDir_x - vx * targetDir_y > 0 )    
angle += TURN_SPEED;
else
angle -= TURN_SPEED;
if (angle < 0) angle = 360;
if (angle > 360) angle = 0;
}


/**
* Explodes and leaves probably splash damage
*/
protected function kaboom():void
{
var boom:explode = new explode(x, y, 2);
//world.remove(this);
}
}

}

As you can see I'm using    
_rocket.angle = angle;

My problem is that the actual angle is staying around 180.  This is because of how the angle and velocity is being computed.  

here is a trace readout, showing the angle at each update:
Code:
179.337
177.47600000000003
178.364
179.337
177.79600000000002
178.364
179.337
179.67
177.466
178.364
179.337
180.003
177.799
178.364
179.337
179.683
177.479
178.364
179.337
180.003
177.799
178.364
179.337
179.67
177.466
178.364
179.337
179.98999999999998
177.786
178.364
179.337
180.32
177.456
178.364
179.337
179.987
177.789
178.364
179.337
180.307
177.469
178.364
179.337
179.987
177.789
178.364
179.337
180.32
178.12199999999999
178.364
179.337
180.32
177.79199999999997
178.364
179.337
178.12499999999997
178.364
179.337
177.80499999999998
178.364
179.337
177.48499999999999
178.364
179.337
177.81799999999998
178.364
179.337
177.48799999999997
178.364
179.337
177.82099999999997
178.364
179.337

Because of the turn speed and regular speed the missile does a great job of looking like its angle is changing but the image isn't rotating to reflect that (angle is staying around 180)

Any ideas?
« Last Edit: February 21, 2011, 01:52:32 AM by Matt McFarland »

<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read February 21, 2011, 02:36:20 AM #1
Matt McFarland

Re: Adjusting a homing missiles angle! (AS3)

Fixed!
changed:

vx = SPEED * Math.sin(angle); 
vy = SPEED * Math.cos(angle);
to:

vx = SPEED * Math.sin(angle*Math.PI/180); 
vy = SPEED * Math.cos(angle*Math.PI/180);


<a href="http://www.mattmcfarland.com/flash/myFlashSig.swf" target="_blank">http://www.mattmcfarland.com/flash/myFlashSig.swf</a>
Offline  
Read June 17, 2011, 03:20:29 AM #2
Tiao_Ferreira

Re: Adjusting a homing missiles angle! (AS3)

Hey, brou!

Your code shows great movement action!

I got a similar effect in AS1, but the missile only follows an individual movieclip, per time...

At GOOGLEY'S PAGE, there source-codes to build RAIDEN-X, Im using it to build ALL OF MY SHMUP GAMES.


"O melhor da escola é a merenda!" - Alf, o E.T.imoso
Offline  
Pages: [1]   Go Up
Jump to:  

Page created in 0.058 seconds with 17 queries.