Getting object to bounce off of solids (Without the behavior)

Greetings again, sorry for posting so frequently as of lately

I’m making a prototype game where I have an object that moves kind of like an asteroids ship, with things like moving away from the mouse, slowing down when nothing is pressed, etc etc

That being said, I want to make my object “bouncy.” As in they’ll bounce away from a solid wall if they bump into one.

So I got to work, but I quickly ran into a problem…

See, instead of using forces, I use positions instead. The forces just don’t give me the movement I’m looking for (Unless I’m missing something), which is why I favored positions for the movement. I use VX and VY variables to track positions to adjust speed here.

However, since those values fluctuate into positive AND negative values often, I feel like there might not be a workaround for the bounce system I have in place…

Here’s the code, and a preview of what’s happening so far:


GDevelop_SY7b0TTkPi

So currently, the code has two main problems:

  1. Since the checks comparing the VX and VY only test if the values are GREATER than each other, the bouncing only works one way. As you can see, I can only bounce off of the right wall and floor. Touching the left wall and ceiling do absolutely nothing. I didn’t add these because I’m afraid if I add values that check for lesser values, it’ll conflict with the greater than values, causing NOTHING to work…
    (If I use less than, the body will bounce off of the ceiling and left wall instead)

  2. It might not be obvious, and it might be hard to explain, so I’ll draw an example:



    Basically, the body doesn’t bounce realistically off of surfaces if it hits it at a certain angle and value.
    Let’s say that if I’m about to hit the floor, but my VX (Horizontal speed) is higher than my VY (Vertical speed), then I bounce straight backwards instead of realistically.

Unfortunately, I cannot use the bounce extension for this, because, well, the bounce extension relies on FORCES, that of which I’m not using.

That’s where I’m stuck. Is there a way to simulate realistic bouncing for ALL surfaces with the position setup I have right now? Or is there a way to convert this faithfully using forces instead (That way I can just use the bounce behavior)?

Any help is appreciated, thanks

Hi @Shadowbonnie7

Can I ask why don’t want to use forces?

Because you can achieve the exact same behavior using physics and forces. You can set the gravity to 0, use permanent forces to move the object away from the cursor, set the bounciness of the object using restitution, and set the friction/dampening to achieve smooth movements.

Since the physics behavior already calculates everything for you, you’ll be saving yourself a lot of math, calculations, other potential problems and of course a big headache.

1 Like

I just noticed that you already answered my question. As I said in my previous answer, physics and forces are the way to go. You don’t need to use the “bounce” extension and use forces instead of change positions.

One uses bounce other uses physics
And it also would be possible for recreate it more or less with forces
You would just need proper expressions for calculating from which angle ball came from and to which angle repeal it

Ahhh, thanks, I got it to work with much less code now.

I never used the Physics2 behavior before, so I never tried.

Thanks again!

1 Like