2D physics and balls within rectangular borders

I’m trying to create game where player pushes with vessel (ball) another balls within area that is limited by rectangular borders.

I’ve encountered two problems:

First problem is that when player pushes ball in shallow angle against border wall, ball doesn’t bounce but glides along the edge.

Second problem is that when ever ball ends up in the corner it’s impossible to get it out of there.

What kind of physics material are you using?

Make sure that the 2D physics objects that are colliding are actually moving with their own velocity. If all you’re doing is repositioning them via the Transform component then you’re simply forcing overlaps and the physics system will do its best to resolve the overlaps. In other words, it’s just causing problems that the physics system is trying to solve but nothing is actually ‘moving’ thus there’s no bouncing etc.

I’m not touching location (or forces) of the balls that player is supposed to push after initial layout. Only object that gets force applied is player. Balls do bounce properly if angle is big enough but smaller angles between dirction of movement and wall balls do tend to slide along the edge for some reason.

I’ve put up prototype here: pusher prototype

Player ship is controlled with: A = counterclockwise, D = clockwise, W = forward. Only blue ball currently does have a proper physics material and rigidbody.

If that blue ball is pushed in small angle against wall it will glide. Also it’s very easy to make that ball stuck in a corner without way to get out it there.

Originally my idea was to have real clip + mirror on each edges but found out that’s relatively hard to achieve, specially with similar physics requirements. I mean that if ship is half way over left hand edge, half of the ship is already visible on right hand side.

Wall, Friction 0, Bounciness 0.8
Player: Friction 0, Bounciness 0.8
Blue ball: Firction 0, Bounciness 0.2 (tried with 0.4 and 0.6, same effect)

Hm I have absoluteley no experience with that kind of thing but what I would try next ist setting the friction of the wall a bit higher. My guess would be, that if the angle is too small, the physics calculates that the ball has to slide the wall, that means, all of the force is directed along the wall and nothing is there to bounce. If you think in real world terms, if you have a wall out of perfect ice then it would be the same behaviour.

I’ve tried pretty much everything. I updated my prototype to have different bounciness and frictions to ball and walls. Apparently small angle will eventually end up with gliding along the edge - I really want to avoid that, after all this is supposed to be more arcade game than physics simulation.

So I’m open for all suggestions - if everything else fails, I can always manually do the physics math.

have you tried using Edge colliders for the borders?

in one snooker2D test I used these modified physics2D settings:
Velocity iterations:12
Position iterations: 5
Velocity threshold: 1

But cant remember was it the edge colliders that helped the most…(for avoiding wall sliding)

If you’re producing large tangental forces (along the wall) as oppose to small forces ‘into’ the wall then it’s bound to ‘slide’ as the energy has to go somewhere. I’m not sure if it’ll help but you can set friction to values >1 to produce non-realistic results.

I’m still not clear on exactly what you want but have you considered adding a force to repel the ball from the walls when in contact? This’ll force them out of corners or against edges.

Large tangential forces sounds something that I’m most probably experiencing. Have to play around with frictions a bit more, and if that fails I probably have to add manually force on contact, which isn’t bad option either.

Tried to do pretty much everything proposed but I can’t physics work as I would like them to.

To help understand my problem I created image that should explain what happens


Maybe I’m still missing something obvious here.

After I converted my scene to 3D I got exactly what I wanted. I needed to change “Bounce threshold” in Editor → Physic Settings.

There are few other threads that experience same thing since (I guess it’s called Box2D physics) doesn’t expose that value for modification.

1 Like

Box2D has a velocity threshold (in 2D physics settings) which I converted from a compile-time constant to something you can change at runtime. Any collisions with a relative linear velocity below this threshold will be treated as inelastic.

4 Likes

Thank you very much! That setting did the trick. I think I tried to play with that but maybe I didn’t set it low enough.

1 Like

You’re welcome. Sorry I didn’t connect you with that property earlier. :slight_smile:

2 Likes

Thank you for this, this helped me a bunch.

Spent hours looking for the solution for this and finally found it! Thank you!

1 Like