BabylonJS + cannon-es + raycastVehicle

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 :slight_smile:
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

Thanks @RaananW!

I’ve found your demo in some old topic :smiley:

I think I can get some inspiration from that code.

2 Likes

That’s what I call “travel the world” :slight_smile:

This must be 6 years old!

3 Likes

@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”.

pulling this PR will be enough to run the project?

1 Like

Yes, it works locally.
@RaananW I can try creating PG. I think it should work with cannon, it’s pretty basic stuff.

1 Like

Here is the PG:

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?

You know, I’m really like

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.

1 Like

The story of my life! :wink:

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 :slight_smile:
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.

Hey, I followed your instructions and I removed Babylon impostors and at the moment I’ve got something like this:

Don’t know what a heck is going on with the wheels but I’m heading somewhere definitely :smiley:

EDIT:
Ok, so the
chassisBody.angularVelocity.set(0, 0.5, 0);
was the reason of the rotating wheels. So I’ve got

@RaananW do you think it looks ok?
If so I’ll try to add driving code to it.

It does look ok! There were a few issues with the data conversion, but this should work now - cannon simple vehicle | Babylon.js Playground (babylonjs.com)

(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 :smiley: 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.

1 Like

Okey, so I opened the demo you shared

and I’m just clicking WSAD and I see
car

Sorry, I meant - the demo I have created 6 years ago. You can see how I solve it there, to fix the cannon architecture

1 Like