Seek Example Uploaded - but needs improvement

jmp909jmp909 Member Posts: 73
edited November -1 in Working with GS (Mac)
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)

Comments

  • firemaplegamesfiremaplegames Member Posts: 3,211
    You can very simply use the MoveTo behavior in the missile.

    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.
  • jmp909jmp909 Member Posts: 73
    do i "move to" on a timer so it always updates itself?
  • firemaplegamesfiremaplegames Member Posts: 3,211
    The Move To behavior always updates itself automtcally.

    It might actually stop updating once it finally reaches the target, but at that point you would usually destroy it anyway.
  • jmp909jmp909 Member Posts: 73
    one thing though... setting self.rotation = vectorToAngle doesnt interpolate the rotation (based on the turn speed) if it switches directions, it just flicks from pointing one way to the other. rotateToAngle didn't seem to work either
  • jmp909jmp909 Member Posts: 73
    hmm so does mine, if its an 180 degree change
  • firemaplegamesfiremaplegames Member Posts: 3,211
    Use Rotate to Position, not Rotate To Angle.

    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)
  • jmp909jmp909 Member Posts: 73
    great thanks. although the turning seems a little unnatural depending on the speed. ie it should be turning in relation to its forward speed, resulting in a "turning circle" like a car would have.. at the moment it kind of rotates in place partly.. eg if speed is around 200-350 range
  • firemaplegamesfiremaplegames Member Posts: 3,211
    Since the Move To speed is constant, it's hard to use that to make a variable rotation speed.

    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!
  • jmp909jmp909 Member Posts: 73
    if you look at the flash example it's just interpolation to a point, over time (onEnterFrame). i just can't work out why my GS version of it freaks out if i up my missile speed.

    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;

    }

    `

Sign In or Register to comment.