Hi, I’m trying to replace AmmoJS with cannon-es in my project. I’m using this cannon-es example
and at this point I’m stuck with how to get the vehicle to be shown in the scene?
My (not working, I mean it’s working but it’s not adding the body to the scene) code so far:
The examples in the cannon-es are using Three.js. I checked that they are using some demo.addVisual function, which uses bodyToMesh function. So I guess I should do something similar and create BabylonJS meshes?
I think probably @Cedric is the person that may give me some hints
Thanks in advance!
The general idea is - after the physics is done calculating the next position, update the babylon meshes corresponding the impostors. for example, in the postStep event in cannonjs, iterate over each impostor and “copy” its transformation to a babylon mesh you have already created
@RaananW I would be thankful if you could take a look at my code.
As I told before I’m trying to marry this example
with BabylonJS.
So at first I’m creating the meshes:
and later I’m creating the bodies for the physical engine
And at the end I’m setting the positions of the wheels but it doesn’t work.
The whole PR is just here
I had “working babylon vehicle” with usage of mesh.parent but I can’t get it working with physics engine.
Gosh, I feel like I should take some “basic gamedev course”.
that seems to be causing a fight between babylon’s physics loop and the cannon changes, no? You are using babylon’s phyiscs impostors on the wheels, and then update them in the postStep again. Do you need the physics impostors? or better - do you need the physics engine plugin in this case?
Removing the physics impostors on the wheels doesn’t seem to change anything.
Maybe there is something wrong with this postStep function. I copied it from cannon-es example.
If I use only BabylonJS Physics, and I have box as parent with wheels it doesn’t work because mesh children impostors don’t work - so I guess I need the physics engine plugin.
This kind of hybrid solution would require a lot of playing around with the babylon cannon plugin and special configuration to the impostors.
You want to avoid transforming your meshes twice. either the babylon physics engine is doing it (as in my example), or youa re doing it manually (and then you don’t need enabling the babylon physics engine. I will be happy to find time and show you a simple example of this way of working, but i won’t make it today, that’s for sure…
No problem! I’m also done for today. Thank you for your help - it really opens new doors in my head
So when you wrote about the physics engine plugin you meant the Babylon impostors?
If so - I think I won’t need it. I want to build simple car racing game with heightmaps so I will need Cannon plugin - it should do all the work.
(I switched the camera’s position). There seems to be an issue with the force application. This might be the version of cannon we are using over the version of cannon this was developed for. It is possible that cannon-es has fixed a but (or added params), that are not supported in the old cannon implementation. that’s probably why steering is not working as you expect.
Thanks @RaananW, I’m on that too atm.
But it doesn’t go forward / backward it goes to the sides…
Did I messed up something while building the vehicle for the physics engine?
No, i really believe this is a difference between cannon and cannon-es.
In my demo I fixed it by changing the axis for every wheel and applied difference force on both wheels. I can’t seem to find the line that does it, but it seems like the force is applied incorrectly.