Physics Engine question

There is a question related to physics. Suppose I have some platforms from which the sphere bounces. Platforms can spin along the Y axis. If the sphere touches the flat surface of the platform, then I give it an impulse to bounce. Every time the sphere touches the platform. But once the sphere is in contact with the edge of the platform or the corner, then it is given too much momentum which pushes the sphere very high. The same effect is observed if during the passage of the sphere into the hole, start turning the platform.

In contact with the sphere - it begins to shake (the sphere). There is also a moment when the sphere, having gained speed, flies through the platforms.
The sphere can move only along the Y axis.
As physic engine used ammo.js

http://joxi.ru/1A5GagPiDnbO0m

The scope is declared as follows:
Game.Ball = TModels.generateBall ();
Game.Ball.physicsImpostor = new BABYLON.PhysicsImpostor (Game.Ball, BABYLON.PhysicsImpostor.SphereImpostor, {mass: 0.1});

                Game.Ball.physicsImpostor.physicsBody.setAngularFactor (new Ammo.btVector3 (0, 0, 0));
    Game.Ball.physicsImpostor.physicsBody.setLinearFactor (new Ammo.btVector3 (0, 1, 0));

The platform is declared as follows:
disc.checkCollisions = true;
disc.physicsImpostor = new BABYLON.PhysicsImpostor (disc, BABYLON.PhysicsImpostor.MeshImpostor, {mass: 0});

                        disc.physicsImpostor.registerOnPhysicsCollide (Game.Ball.physicsImpostor, function (main, collided) {
                            
                            Game.Ball.physicsImpostor.applyImpulse (new BABYLON.Vector3 (0, 0.3, 0), Game.Ball.getAbsolutePosition ());
                    
             ....

Pinging physics guru @trevordev

Could you put a console.log before the apply impulse for debugging? I think the registerOnPhysicsCollide function might be called multiple times if the ball lands on an edge.

If it indeed is called twice, giving your ball a BoxImpostor instead of a sphere one might help in your case.

Per one hit, event work’s 2 times
http://joxi.ru/v29GnE6iZ3pDgr

Does it hit two times on edges only, or always?

If on edges only, try the BoxImpostor as mentioned above.

Yes. Always 2 events per hit. On enges differently may 1 event or 10.

If i use boxImpostor, my sphere cannot fly through emptiness.

Use A BoxImpostor for the sphere, not for the platforms

Ok. Now on edges i have 6 events per hit))

Could you also tell me how many events you get, if you make the platform a box (just need that to confirm a theory)

Now always 2 events

UPD: do you mean:
BABYLON.PhysicsImpostor.BoxImpostor for platforms?

Ok, I think, because your platforms are meshes, you get an event for every triangle that collides.

To debounce that, I would try something like this:

let lastFrame;
disc.physicsImpostor.registerOnPhysicsCollide (Game.Ball.physicsImpostor, function (main, collided) {
   const currentFrame = scene.getFrameId();    
  if (lastFrame !== currentFrame) {
  lastFrame = currentFrame;
Game.Ball.physicsImpostor.applyImpulse (new BABYLON.Vector3 (0, 0.3, 0), Game.Ball.getAbsolutePosition ());
}

...
}

With that your Impulse will only get applied once per frame.

1 Like

Hey,

This might be the same as the issue described here Two questions about CannonJSPlugin, see this playground which is able to control the accuracy with the fixedTimeStep control but note if this is increased too low it might affect perf: https://www.babylonjs-playground.com/#BEFOO#362

For the other issue (edge causing too much force) would you be able to create a playground replicating the issue?

No effects. :frowning: 2 events per hits

I included next code:

 var p = new BABYLON.AmmoJSPlugin(true)
 Game.Scene.enablePhysics(new BABYLON.Vector3(0, -4.81, 0), p);
  p.setFixedTimeStep(1/(60*2*2))

And got a little better. But on edges all very bad.
About playground:
There is a lot of difficulty with unloading code on the playground.

This might be a bug in _isImpostorInContact of the ammoJSPlugin. registerOnPhysicsCollide depends on that function. Yesterday (even though its private) I experiemented a bit with _isImpostorInContact to check whether I can use it for detecting if my player isGrounded. I got pretty random results from that function. With one collider I always got a collision no matter what. With another collider I got a collision once on startup and then no more.

Yep, you can find the code here: Babylon.js/ammoJSPlugin.ts at master · BabylonJS/Babylon.js · GitHub

Ammo provides 2 functions that i could find world.contactPairTest and world.contactTest, contactPairTest fired way too many events so I only fire an event when both contactPairTest and contactTest are true but I guess there are still issues.

Is there any possibility to repro using this playground? https://www.babylonjs-playground.com/#1NASOD#25

No.

I sometimes don’t get a PG. There is access only through TOR but it does not work webGL
http://joxi.ru/ZrJk01lSw9nBWm

in tor:

Сan i manually animate the falling and rebounding of the sphere? how best to do it? Initially, I wanted to go along this path, since physics was actually useless. But faced with the fact that the sphere did not want to fly through the empty spaces in the platforms.

http://joxi.ru/Y2LQE7JU79EP6A

It might be worth evaluating the Manifolds method for collision checking described here:
https://web.archive.org/web/20170716030527/http://bulletphysics.org/mediawiki-1.5.8/index.php?title=Collision_Callbacks_and_Triggers
(They have taken there wiki down for whatever reason)

I already did some testing with the Manifold method yesterday. But it returns an object like: {J:123456} as physicsBody that is related in a collision. Simple reference checking or checking the equality of the number didn’t work to get the associated babylon meshes from that. I guess that is somekind of emscripten special object?

The also say:

Be careful when using contact callbacks: They may be called too frequently for your purposes.

That might be related to your experience. Sadly they don’t provide any explanations on why that is the case