Player velocity changed by explosion
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
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
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
Thanks T... ill report the outcome
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!
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
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!!
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! ?
It would solve the problem but not sure it is available...
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.
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.