• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How would i push an object back when exploding?

SirCaliber

Member
Here's the code i have:
Code:
if(instance_exists(oExplosion)){
motion_add(image_angle,-distance_to_point(oExplosion.x,oExplosion.y))
}
The problem is is that the closer the objects are to the explosion, the less they get pushed back. How would i invert this?

Basically i want the closer objects to get pushed back further than the objects that are further away.
 

NightFrost

Member
Firstly, define a maximum distance at which the push happens. Then when you check if oExplosion exists also check if distance is less or equal to maximum push distance. If so, the motion to give is maximum distance minus distance from explosion, multiplied by some fraction to make the push slower - otherwise everything gets pushed away in a step or two.

(Technically, I suppose push force should be inverse of distance squared, but details.)

EDIT: wait, motion_add was a command that combines to existing movement vector, instead of setting a new one? In that case, you should calculate initial velocity from distance to explosion and make sure you check only once from that explosion (since it might exist for multiple steps). Then you have to decrease that velocity in the pushed instances every step, until it has reached zero.
 
Last edited:

SirCaliber

Member
Thank you very much! I really appreciate the help! Yeah, I never thought about defining a max distance variable and subtracting the distance from that. I thought there was some clever one-line math equation I could do.
 
B

BlueCat6123

Guest
Thank you very much! I really appreciate the help! Yeah, I never thought about defining a max distance variable and subtracting the distance from that. I thought there was some clever one-line math equation I could do.
Bit late, sorry, lol, but I just wanted to add a really nice way I like to do this is by using that y = mx + b formula and plugging in two distances (as a x) and two motions (as a y) and I just put that formula into the game. So if you know a bit about slope intercept that could be a good place to go for stuff like this (I actually did something very similar to this not long ago !)
 

Yal

šŸ§ *penguin noises*
GMC Elder
Or an even simpler way, use lerp to interpolate between a max and min value using the distance.
 
Top