Why sometime the collision & rigid looks doesn't work correct?

In the following Pin Ball game, why sometime the flipper can’t hit the ball? And sometime the ball can fly out the frame which is rigidbody entity?

The ball drop through the flipper.
_20240625234725

The ball flyes out the frame.
微信截图_20240625235126

The link of the project is here.
https://playcanvas.com/project/1231185/overview/pinball-game

Hi!

this is happening due to the frame rate of the physics calculation, to solve this problem use an CCD

Example: Physics with CCD | PlayCanvas Developer Site

Add this script for your ball entity: PlayCanvas | HTML5 Game Engine

1 Like

Thank you Wagner, I have tried to use the ccd.js in the ball, it couldn’t fly out the frame with high speed.
But the flipper still can’t hit it when the ball was slowing rolling down on the flipper tips. (As same as the screencaptured video in last post).

Hi @goinix_project!

The flipper is also moving at high speed, so maybe you can try to add the script to the flipper as well.

CCD uses a linear math to solve the collision. Say a fast moving ball - it takes its start position (first frame) and end position (second frame), and creates a capsule out of those. Then does collision test with other objects against that capsule. This won’t work for angular momentum. So CCD doesn’t work for any rotations. If your box is rotated, there isn’t an easy way for the physics engine to construct a primitive shape that would take both the start and end rotations of that box. Technically the box doesn’t move (linearly), only rotates, so CCD algorithm is not even triggered on it.

You can:

  1. Reduce the ball’s max speed.
  2. Lower fixed timestep:
app.systems.rigidbody.fixedTimeStep = 1/120;
  1. Increase max sub steps:
app.systems.rigidbody.maxSubSteps = 20;
  1. Use math and solve nonlinear root problem to find the point of contact manually, then simply reflect the ball’s velocity based on collision normal.
2 Likes

Thank you all to reply,
Looks like a complicated problem. I just have found a post has the same question & in the very similar Pin Ball game project. :sweat_smile:

BTW @LeXXik Where can I use the following codes?

app.systems.rigidbody.fixedTimeStep = 1/120;

app.systems.rigidbody.maxSubSteps = 20;

Any guides can show me how to use them?

Just anywhere in your scripts during initialization. This is an unofficial feature, so no public documentation, unfortunately, and it can silently change in the future.

Basically, the physics will run at fixedTimeStep intervals. The more you have updates per second (smaller fixedTimeStep), the smaller the distances the ball will move between each update, so less chance that it will go through a wall.

Substeps is not a hard value, but a max limit. The physics will divide each fixedTimeStep update into sub steps to improve the collision detection. Although, 10 is more than enough, I think, since it is mostly for correcting the position after the collision. If you tunnel through an object, then it won’t help much.

You can calculate your smallest fixedTimeStep using max speed of the ball to find the max distance it can travel per update. 1/120 is just an example. I have no idea if it is enough or too much. Measure the diameter of the ball + thinnest part of the flipper. The ball should not be able to travel more than that in a single update. If it does, it will go through the flipper at max speed.

MyScript.prototype.initialize = function() {
    this.app.systems.rigidbody.fixedTimeStep = 1/120;
};

Thank you very much, I will try it. It’s too much parameters need test. It’s tough work. :sweat_smile: