Seek Example Uploaded - but needs improvement
jmp909
Member Posts: 73
Hi,
i've adapted the flash code from this page
http://www.freeactionscript.com/2008/12/game-weapons-heat-seeking-missiles-rockets-torpedoes/
and uploaded an example here
http://gamesalad.com/game/play/61428
You can change the turn speed and the missile speed (game attributes), but for some reason if you increase the missile speed to >=1 it goes haywire and the missile just disappears off screen
anyone any ideas?
thanks
j
(Ps hold the mouse down and move the alien to activate the seeking behaviour)
i've adapted the flash code from this page
http://www.freeactionscript.com/2008/12/game-weapons-heat-seeking-missiles-rockets-torpedoes/
and uploaded an example here
http://gamesalad.com/game/play/61428
You can change the turn speed and the missile speed (game attributes), but for some reason if you increase the missile speed to >=1 it goes haywire and the missile just disappears off screen
anyone any ideas?
thanks
j
(Ps hold the mouse down and move the alien to activate the seeking behaviour)
Comments
Keep track of the Player's X and Y and use those as the Move To coordinates.
Works great, and very easy to implement.
To get the missile to always point at the player, use VectorToAngle.
It might actually stop updating once it finally reaches the target, but at that point you would usually destroy it anyway.
Uncheck "stops at destination". Set it to a fairly high speed.
Just tried it, works great.
And you don't even need vectorToAngle. (although I'm sure that is what it is using behind the scenes)
I made a Race Car Demo a while back, which sort of what you are talking about. It has a kind of "power steering" so you can only turn based on how fast you are going - and you aren't able to turn the car when you are standing still...
It is under my profile, feel free to dig through it.
It's not exactly what you need, but maybe you can pull some of the math out of it.
And the speed in the Rotate to Position can be an expression, but the math to make it work like you say is beyond me. I BELIEVE the math needed is the same needed for either centripetal or centrifugal acceleration, but that is way out of my league!
here's the main code again, that i turned into rules on the missile (so for follower use self)
`
function doFollow(follower, target):Void
{
//get distance between follower and target
follower.distanceX = target._x - follower._x;
follower.distanceY = target._y - follower._y;
//get total distance as one number
follower.distanceTotal = Math.sqrt(follower.distanceX * follower.distanceX + follower.distanceY * follower.distanceY);
//calculate how much to move
follower.moveDistanceX = turnRate * follower.distanceX / follower.distanceTotal;
follower.moveDistanceY = turnRate * follower.distanceY / follower.distanceTotal;
//increase current speed
follower.moveX += follower.moveDistanceX;
follower.moveY += follower.moveDistanceY;
//get total move distance
follower.totalmove = Math.sqrt(follower.moveX * follower.moveX + follower.moveY * follower.moveY);
//apply easing
follower.moveX = missileSpeed*follower.moveX/follower.totalmove;
follower.moveY = missileSpeed*follower.moveY/follower.totalmove;
//move follower
follower._x += follower.moveX;
follower._y += follower.moveY;
//rotate follower toward target
follower._rotation = 180 * Math.atan2(follower.moveY, follower.moveX)/Math.PI;
}
`