Two questions about CannonJSPlugin

I’m using Babylon.js 4.0 and Cannon.js to make a scene with physical effects,then I come across two questions。

As this gif , some squares crossed the board when others are blocked by the board.What makes this difference?
I use setLinearVelocity to set the squares linearVelocity.As you can see,the squares flying with a slight deviation in the horizontal direction.More seriously when throw the squares upwards the initial speedy was lost!
Here‘s the code I deal with the throw action:


Hello and welcome!!
Can you repro in the playground? @trevordev could them help investigate :slight_smile:

I had a similar problem which I could fix by doubling the standard physics step:

Just scaling up everything might help too.

If understand the working of physics engines right, the reason is the following:
Physics-Engines move all object forward a bit and then check whether collisions happened. A collision happening is defined by two objects intersecting with each other.Thus if you have a small, fast moving object and too big time steps it can happen that your box is just above your ground in one step, which is fine and in the next step it is already beneath, so for the physics engine, no collision happened. If you increase the step frequency, you also increase the likelihood of the physics engine capturing the in-between step when your box is intersecting with your ground. Only if it captures this state, it can prevent it going all the way through it, by moving it back.

Here is a drawing for clarification:

For your second question, this might help:


The answer mauricedoepke provided looks good to me. Depending how your scene needs to work you could add your own checks to correct for errors in physics. In the past I’ve used raycasting between the last position and the new position of the object to check if it goes through a plane aswell.

Hello!Thanks for your help.I will upload the code to the github after finishing this part.It is somewhat disappointing that we can hardly upload a complete project to the playground:slightly_smiling_face:

Thank you very much!I will try to solve these problems in your way.

Thank you for your attention.In this project I want to achieve physics collision between a lot of meshes.I found three physical engine are mentioned in the document(cannon,oimo,ammo),but I can only find out the detailed usage of Cannon.js.Is Cannon.js the Best physical engine we can use in Babylon.js?How can we use the other engines?

Examples covering all physics engines Use Forces - Babylon.js Documentation and also follow Further Reading at the bottom. Generally usage is similar.

Ammojs is based on bullet physics engine so their docs can be useful bullet3/Bullet_User_Manual.pdf at master · bulletphysics/bullet3 · GitHub


Thanks for mauricedoepke’s method,I have reduced the frequency of the first question.
This is the repro in github
But what I didn’t expect was the second question develop into the opposite direction !All the downward Initial linear velocity come to be upward! It looks like a collision happened when the squares set up.Does anybody know what caused it?

In addition,considering the limitations of computer performance we can’t increase the calculation frequency infinitely.For the board,maybe we could increase the thickness to make the collision perform better.However,how can we optimization the collision between the tiny squares?

The upwards motion seems to be because you are launching the cubes from within the floor, maybe raise the player higher up?

Are you seeing perf issues between the tiny squares, what type of optimization are you looking for?

There are six invisible thin boards in the scene,and the player is in the square formed by them.I tried raise the player up or launch the cubes outside the six boards.It dosen’t make any difference.

As this picture:
In my imagination,if I throw some cubes up first,then throw another cube forward when the previous cubes falling down.The cubes with different direction may collide with each other and come apart with new motion direction and self rotation.
But in reality,they just go across and noting happened.(If one of them is static on the ground the collection could happen)
Could I achieve this goal with Cannon.js?

I found another question when I test this scene:
There seems to be some fragments of the wireframe material appeared in the wrong place.(the red rectangle)

If you lower the fire speed does this work? It might be similar to the wall issue where they are moving too fast or that it is too hard to hit them and judging distance on a 2D screen is hard to do

I am not seeing that when I use the test url you shared. Can you share a playground ( for the code as it will be easier to debug.

Thanks for your reply.Because I added some code to the rendering loop,I find I can hardly share the code as a playground.But you can download the code from this github page .
In the project,test.html create the whole scene,function CreateCubeArenaWithPhy3 in newland.js create the boards,function ThrowSomeBall in khrule.js in charge of throwing the ball.
Today,I commented out the board at the bottom of the box,and I found the missing of velocity on Y axis not happen. It makes me very confused.

I just tested this scene with Oimo.js.The ‘Y-missing’ did not occur in this case.Maybe it’s a bug of Cannon.js?
Thanks for JohnK’s examples.