Cannon velocity - the sphere doesn't bounce

Hello, I’m using CannonJs for collision detection between a sphere and a wall but when the velocity of the sphere is greater then 240 the sphere goes through the wall without bouncing.

  sphereBody.velocity.set(-250, 0, 0);  // Up to -240 it bounces

Why it happens?

I also tried to change the velocity of the sphere manually as soon as I detect a collision (following this answer) but I don’t understand how to “reflect” the velocity’s vector in CannonJs.

I attach a demo zip file to recreate the problem:
cannon-walls-collision.zip (593.2 KB)

I tried your example and note that if you change the velocity to -125 it also goes through. I think it might have something to do with near-multiples of 120. Given that the wall has a width of 1, the sphere a radius of 1 = diameter of 2, and your step size is 1/60. I think a velocity of 125 is just enough for the sphere to pop from one side of the wall to the other in a single step. (If that’s true, then you’d expect it to still hit the wall at say 180 but pop through again at 250, 375 and so on.)

If that’s the case (I haven’t tested it more thoroughly) then I think you’re stuck between either making the sphere thicker, making the wall thicker, or using multiple smaller steps – something like

var stepsPerRender = 4;
var stepSize = 1 / (60 * stepsPerRender);
for (var i = 0; i < stepsPerRender; i++) {
    world.step(stepSize);
}
renderer.render(scene, camera);

Of course that’s more CPU work per frame. As a rule of thumb I’d try to impose a “speed limit” of Max Velocity < Max(Minimum Barrier Thickness, Minimum Moving Object Thickness) * Steps Per Render

1 Like

Thanks Jim, unfortunately you are right: the higher is the velocity the more I need to increase the steps per render. I could have lots of spheres in my simulation, so I will probably go for the “speed limit”. (I said "unfortunately because I don’t like the idea I have to limit something)