Player velocity changed by explosion

EwokPDEwokPD Member, PRO Posts: 35
edited November -1 in Working with GS (Mac)
Hello, i have a maths problem that hurts me head so wondered if anyone could shed some light on it for me please.

I have a player who is tootling along and an explosion happens nearby (poor player i hear you say) - well the player survives but i want his velocity to be altered by the force of the explosion relative to the distance away from the explosion. I do not want to simply use change velocity as in the blast effect tutorial. I want a senerio where if the player is moving towards the blast at 100 and the blast magnitude at source is 100, the player would stop at the source of the blast. If that makes sense.

So the player will keep their own velocity and be pushed by the explosion.

Values i have available.

D - Distance to blast
A - Angle to blast (though not sure i need this or not)
P - Power of explosion
R - radius of blast effect, for now assume power of blast dissipates linear to the edge of radius in a circular effect

X - Player x linear velocity
Y -player y linear velocity

X1 - player velocity after explosion effect
Y1 player velocity after explosion effect.
So i need an equation that links all this together to reduce/change the players linear velocity based on where the blast takes place. Can anyone help?

So X1 = X - ???

Y1 = Y - ???
Thanks

Comments

  • mu-kowAPPSmu-kowAPPS Member Posts: 233
    well, im thinking you will need to use the angle to make it work correctly.

    unless... you are only going to hit the explosion from up down left and right.

    i dont really know the particulars of your game, but if your char is passing by the explosion and then just slows down... that wouldnt be right at all. the char should be knocked off course, in the opposite direction, relative to how close to the explosion it was.

    you may need to use some cos and sin expressions to make it work right.

    its past 3 am here so... thats about all ull get outta me for now :D

    :D
  • EwokPDEwokPD Member, PRO Posts: 35
    hmmm.... that would probably work.... i will give it a go... i think i have over engineered my problem :) ...... off to test

    Thanks T... ill report the outcome
  • EwokPDEwokPD Member, PRO Posts: 35
    i am not sure the change velocity is working - it could be however the way i have it set up. if i am travelling left to right on the screen and get hit by an explosion at the bottom of the screen when i am directly over the top of the explosion all left right velocity is lost. Where as i want the player to be deflected off course. I am not trying to stop the player from moving but react realistically to the explosion effect based on distance from explosion and angle to it.

    Current code

    att Game.explosion is TRUE

    // get distance from blast

    CA - self.DistanceToBlast to Mag(GameExpX-SelfPosX,GameExpY-SelfPosY)

    // write player velocity at blast

    CA - self.VelocityatBlast to Mag(self.Motion.LinearVelX,self.Motion.LinearVelY) (this could be wrong?)

    when game.ExpRadius > DistanceToBlast

    CV Direction- (vectorToAngle(-(gameExpX-selfPosX),-(gameExpY-selfPosY)))

    Speed gameExpPower - selfDistanceToBlast - selfVelociyatBlast

    I did go down the path of getting the distance to blast and the vector to angle to blast, then resolving these into X Y components using Cos and Sin. But then got lost as to how to use those to alters the players X Y velocities! ? ? Its been far too long since i have done any complex math!
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    I'd suggest trying Change Velocity again:

    Direction = (vectorToAngle(-( game.Mouse.Position.X - self.Position.X ),-( game.Mouse.Position.Y - self.Position.Y )))
    Speed = (5000/magnitude( game.Mouse.Position.X - self.Position.X , game.Mouse.Position.Y - self.Position.Y ))
    Relative To = scene

    The speed formula is based upon distance from mouse to actor. You can work with the speed formula to get the effect you want. But the direction formula should work for you.

    Hope this helps
  • EwokPDEwokPD Member, PRO Posts: 35
    thanks RThurman. I think the speed formula will work, the direction is similar to what i was using

    It doesn't seem to carry player momentum therefore does not look to deviate the player off a path but change it completely - I have forwarded the problem to a couple of math pro's so will get some thing working. I think its completely achievable in GS....with the correct formula

    Imagine a fairly light ball rolling past your mouth and blowing gently on it, that is the look i am after - the balls new direction should be a combination of its old velocity and the power of the force causing it to deviate.

    Thanks all for your efforts, much appreciated. Keep any more ideas coming!! :)
  • EwokPDEwokPD Member, PRO Posts: 35
    ok i went back to basics with this and did the following.

    1 - Turn explosion magnitude and player momentum into vectors

    2 - resolve vectors into their XY components

    3 - Combine components in new vectors XY components

    4 - Convert resulting components to polar form giving me a resulting speed and direction.

    This works but for one problem. The angle only works most of the time. I think it has something to do with the arctan only giving values between -90 and 90 degrees... so depending where the explosion is the resting velocity is either correct or 180 degrees wrong. So now just need to hack in a fix, when i work out what the fix is! ?
  • EwokPDEwokPD Member, PRO Posts: 35
    Is there a way to use a aTan2 function in game salad? - http://en.wikipedia.org/wiki/Atan2

    It would solve the problem but not sure it is available...
  • EwokPDEwokPD Member, PRO Posts: 35
    ok finally solved this thanks to a programmer friend... there is an equivalent formula to aTan2 here - http://en.wikipedia.org/wiki/Atan2 and when i plug my numbers into that it works.

    So should anyone want to deviate the path of an object with an explosion effect you can do the following. I would be interested to hear any simpler methods.. but this does work very nicely.

    At the point of explosion Write out

    Explosion XY coord

    Distance to explosion from player XY

    Player speed Mag(linear velocities)

    Player direction vector to angle (linear velocities)

    Explosion speed - use explosion power and distance to explosion in formula of your choice

    Explosion direction - vector to angle player and explosion

    This can give you two vectors based on

    Ps = player speed
    Pa - player angle

    Es - explosion speed
    Ea - explosion angle

    Resolve vectors into their X Y components

    Px = PsCosPa
    Py = PsSinPa

    Ex=EsCosEa
    Ey=EsSinEy

    combine to produce desired speed and direction as a vector

    Rx = Ex+Px
    Ry = Ey+Py

    from this you can work out the speed Rs = sqrt((Rx x Rx)+(Ry x Ry))

    and the angle

    Ra=2*arctan((((sqrt((Rx*Rx)+(Ry*Ry)))-Rx))/Ry)

    then simply plug Rs and Ra into a change velocity command and your player should deviate off their path based on explosion power and distance to explosion.

    Cheers.
  • RThurmanRThurman Member, Sous Chef, PRO Posts: 2,881
    Oh -- OK you were not after a massive explosion effect. You wanted the object to be influenced, but not too much (as if it had a lot of mass).

    You came up with a nice approach.

    By the way, you asked about atan2. I was surprised to see that GameSalad did not have an atan2 function until I realized that vectorToAngle utilizes it. VectorToAngle = atan2(y,x)*180/pi so if you really need atan2(y,x) in the raw, you can derive it from this.
  • EwokPDEwokPD Member, PRO Posts: 35
    Thanks RThurman I honestly hope I don't need it for anything else :) but is very useful to know....I need a few weeks easy stuff before I tackle another one of these to let my brain recover!!
Sign In or Register to comment.