How to make an object bounce from a stand still?

I have a circle resting against a flat static surface. There's no gravity. (See A)

Now, I have another circle coming towards that circle at an angle (See B)

What's happening is the moving circle is bouncing off, and the stationary circle is moving along the surface (See C)

What I want to have happen instead is both circles bounce off, away from the surface, like the surface is a cushion that gives a little before snapping back into place and pushing both circles away. (See D)

Attachment:
bounce.gif
bounce.gif [ 1.77 KiB | Viewed 286 times ]


This works if the circle is just a little bit away from the surface (i.e. not touching it) but if it is touching, it doesn't work. The restitution of the surface is 1, and the restitution of both circles is 0.9.

Any suggestions?
This is an interesting problem. I think it has to stem from how Box2D handles collision response (one at a time, no?). I tend to find that Box2D is excellent at maintaining physical laws, but this example unveils a shortcoming. The thing is, I modeled this and found that momentum is not conserved in the y-direction. I set friction on all shapes to zero and used 1.0 restitution just for good measure. The problem is that, if ball2 (I'll call ball2 the initially stationary ball) only moves along the surface, then ball1 must recoil in such a way that only the x-component of its momentum should change. I actually performed calculations and px is conserved in the two-ball system, but py is not. Kinetic energy should also be conserved when using zero friction and perfectly elastic shapes, but it is not either.

I also tested the case where the ball is not in contact with the static body and momentum and kinetic energy are both conserved. I haven't looked that much into exactly what happens in the code to handle collision response, but I would have to think that this is the problem. It may be not computing an impulse for the ball2-static body pair since they are already in contact.
I think your problem is that:
durnurd wrote:
... like the surface is a cushion that gives a little before snapping back into place
is not the case because you are dealing with hard surfaces. And if you are using CCD like I expect you are, then the ball will not penetrate the wall in the first place.

How about having a sensor along the wall which sticks out a little bit, about as much as you would ever expect the ball to compress the cushion. If the ball overlaps this, then manually add some force to push it away from the wall. This will still give you the hard limit of the original wall, with an adjustable cushion layer in front of it.
That seems like a difficult solution to deal with. How would I know when to apply the force, for example? I would have to test every collision to see if it's between two circles, and if it is, check if one of them is touching the sensor, and if it is, apply a force approximately equal to the y-component of the moving circle, and if the stationary circle is sitting against a vertical wall instead, then I would have to apply it in the x-direction instead (since it also happens in that case), and I don't know how the situation applies to angled walls...

I think in my case I may just leave it as is, if it's not a bug in the physics engine. It works well enough.

As a note, am I really dealing with hard surfaces? I would think I'm only dealing with "hard" surfaces if restitution is 0 for both the circle and the surface.
Quote:
As a note, am I really dealing with hard surfaces? I would think I'm only dealing with "hard" surfaces if restitution is 0 for both the circle and the surface.

Box2D is a rigid body physics engine. All of the shapes are hard and have no give. The restitution only defines how much energy is lost to a collision between two shapes. You're thinking that a restitution of 1.0 is like a bouncy ball and that higher restitution means squishy. In practice, this is generally true, but there are some things with reasonably high restitution that aren't squishy; think of billiard balls. They aren't going to bounce off the ground so well, but when they hit each other they do a pretty good job. So well, in fact, that when we do physics problems involving billiard balls, we usually assume the collision to be perfectly elastic when we certainly don't have to.

As far as irresistible force's solution, I wouldn't recommend doing it, but not for the reasons you stated. While your reasons are valid enough because it would be overly complicated, the more pressing issue is that using a sensor wouldn't solve anything. Sensors detect collisions in the same way as rigid shapes. So if your circle is already intersecting the sensor, the collision between the two circles isn't going to trigger another collision between the circle and the sensor. Another thing to consider is that, if you were to apply realistic impulses to any circle that hits the sensor, it would be no different than just making the sensor a solid body and letting the engine do the work instead. There would be no way for the circle (that's next to the wall) to get next to the wall, inside the sensor, in the first place. The sensor would be the new wall and would suffer from the same problem.

The only way to fix this problem is to modify the collision detection/response code in the engine.
pogalog wrote:
...if your circle is already intersecting the sensor, the collision between the two circles isn't going to trigger another collision between the circle and the sensor.
Naturally, since there has only been one collision which is ongoing, that's why we have BeginContact/EndContact events to efficiently do this only when collision states change, instead of being told that things are still colliding... yep, still colliding... for every pair of intersecting fixtures every single timestep 😀 You are supposed to make a note of the fact that the ball and the sensor started touching, and until you are told that they stopped touching you take the appropriate response, that being:

pogalog wrote:
...if you were to apply realistic impulses to any circle that hits the sensor
Not an impulse, a force. Since we want 'a cushion that gives a little' we are not trying to immediately return the ball outside the sensor in one timestep. You don't need to worry about x-y-components of the ball's movement or 'angled walls'...??? The force will always be perpendicular away from the wall and depends only on how far the ball has penetrated the sensor, which is also easy to determine. The further into the sensor the ball is, the stronger it should be pushed away, kinda like a long flat spring.

pogalog wrote:
The sensor would be the new wall
Yes, that's right, you would need to adjust the position of things to account for that.

However, I can also think of reasons why you might not want to do it this way, but not for the reasons stated so far 😀
1. If you want to affect the ball's rotation as a result of bouncing off the sensor, you would have to calculate it yourself.
2. I'm guessing you are making a billiards simulation in which case for a high-speed hit, the number of timesteps the cushion will be forcing the ball away could be very small, and might not have much effect unless you made it quite a strong force, which could then look weird for a slower bounce.

Other things to keep in mind would be:
3. If the ball is going fast enough to hit the 'real wall' behind the cushion, you might not want to add your cushion force to the ball since the normal Box2d reaction will be happening too.
4. If you are modelling billiards using real-world dimensions, you might need to adjust the linear slop distance which I think is 5mm by default.
I am working on a billiards game, yes, but everything is scaled up by a factor of 10, so the radius of a pool ball is about 0.3 instead of 0.03. What is linearSlop anyway? And would I still need to adjust it if I've scaled everything up by a scale of 10?
I believe the linear slop specifies a distance which is so small that box2d considers it to be zero. The dimensions you are using sound fine.
To clarify, is this technically a bug in the engine? And how hard would it be to make it work as expected if someone were to devote their time to it?
No it's not a bug. I expect a working example of what I mentioned could be done in an hour or so if you know what you are doing, but the bigger problem is whether it would would really work to your satisfaction. In my my 'minds eye' it seems pretty sound but something could come up that complicates things further.

Original thread: https://box2d.org/forum/viewtopic.php?t=5791