Quick and Dirty Heat Seeking Missiles...
BarkBarkCo
Member Posts: 1,400
Here is a game I'm messing with that is in the very early stages...
I liked the result of the heat seeking missiles logic I added so much, I thought I'd share the joy!
http://gamesalad.com/game/play/56585
I liked the result of the heat seeking missiles logic I added so much, I thought I'd share the joy!
http://gamesalad.com/game/play/56585
Comments
Heat seekers are my favourite weapons of choice :P
Dr. Sam Beckett never returned home...
Twitter: https://twitter.com/Quantum_Sheep
Web: https://quantumsheep.itch.io
Dr. Sam Beckett never returned home...
Twitter: https://twitter.com/Quantum_Sheep
Web: https://quantumsheep.itch.io
It's real simple ... and kinda dirty.
There is no live tracking of the falling bombs. They are set to fall at a random x position, at set intervals. About .5 seconds into there existence, I mark their current X position in a game (real) attrib, `game.LastBombX`. Then, when missiles are spawned, I have them interpolate their own x position over a 1~ second period to `game.LastBombX`. That is what makes them look like they are "thinking" ...
I'm in the theoretical stages on a method that will allow me to always track the bomb with the lowest `self.position.y` ... but that is yet to be added.
I've also used interpolate to make 'Enemy' homing missiles that track the player.
In your method, are you changing the last bomb value to the individual bombs position? So if two bombs spawned, for .5 seconds bomb1 would have the value 'last bomb' until bomb 2 spawned and it would switch to that?
BAH! Won't get home for another 2 hours or so and I'm gagging to see how this works...
QS
Dr. Sam Beckett never returned home...
Twitter: https://twitter.com/Quantum_Sheep
Web: https://quantumsheep.itch.io
That said, if you fire at a rate greater than 1 missile per .5 seconds, at least one missile would track every bomb.
Thanks for the epiphany!
Yeah you could simply have every enemy check if their position x and y are closer to the player's than the global variables for closest_e_x and closest_e_y and change closest_e_x and y if they are.
I'm afraid this would be a little too heavy for an iPhone shooter (every enemy would have to check and perhaps change variables with every tick of the game's clock), but I think it would work fine on iPad, and maybe even the newer phones (never tested on 3Gs).
not looked at your example yet (no mac here) but I found this really nice heat seeking flash example (with source), with modifiable parameters for turn speed etc.
i've pasted the main function below....if you altered it to use game variables eg followerMoveX, targetX etc should be adaptable with constrains. (note vectorToAngle = atan2)
http://www.freeactionscript.com/2008/12/game-weapons-heat-seeking-missiles-rockets-torpedoes/
`
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;
}
`
http://gamesalad.com/forums/topic.php?id=7197#post-47531
but have issues.