PhysicsV2 Car Simulation Template

Hi All,

I’ve created a car simulation example with Havok/PhysicsV2 that I think would be a great starting point for anyone interested! It implements:

  • Motorised joints for powered wheels and steering
  • Stiffness and damping (spring) 6DoF joints for suspension
  • Effective use of mass and friction

This means it might also be helpful for anyone struggling with those aspects of PhysicsV2.

PhysicsV2 Car Simulation Example | Babylon.js Playground (babylonjs-playground.com)

Hope it helps!
TR

16 Likes

Awesome, thank you for sharing :smile:

1 Like

This is absolutely amazing, thank you so much for sharing, I have been working on something like this for a while and couldn’t get it to work.

1 Like

Thanks for the positive feedback! Here’s what it can look like with a nice car model dropped in and some dramatic lighting -

Driving Simulation with Babylon.js / Havok Physics (youtube.com)

8 Likes

Spiral race track - https://www.babylonjs-playground.com/#ANV5OM#123

4 Likes

the guardrail is a nice touch :laughing: I would keep falling without it

1 Like

Circular race track - https://www.babylonjs-playground.com/#ANV5OM#139
With speed meter and time counter.
The next step is to add some obstacles :slight_smile:

1 Like

Really useful thing

You’ve inspired me to tweak the physics to be a bit more fun and fast PhysicsV2 Car Simulation Example | Babylon.js Playground

Makes your track a lot more difficult though!

Yes, now it has more sporty riding style :slight_smile:

If I wanted to change the initial position of the car, how would I do that? I’ve tried adding carFrame.position.x = carFrame.position.x + 1; as the last line of CreateCar() but that doesn’t seem to have any effect.

I’d like to try this out on some external models, which is why I’m looking to update the initial position.

I’ve been able to make this work by doing the following:

const initialPosition = new BABYLON.Vector3(0, 2, 0);
carFrame.position = new BABYLON.Vector3(0 + initialPosition.x, 0.3 + initialPosition.y, 0 + initialPosition.z);

And then the same for all the wheels and axles. I’ll try this out on an external model, but will then also look into how to set the initial rotation.

1 Like

Hello, you have to set the disablePreStep property to false on each physics body before changing the cars position. Here is an updated playground with this implemented PhysicsV2 Car Simulation Example | Babylon.js Playground

Pressing “r” triggers the Reset() function which moves the car back to the start of the track.

3 Likes

I really love it. Thank you very much for sharing! How did you solve the lighting to work so smooth in your youtube video and also reflect on the floor? I tried to do a based on your code and the physics works smooth even with a custom model, but I cannot get the lights working so smooth or else my FPS drop really low: https://projects.manuelhintermayr.com/babylon-js-car-example (and here the code behind it babylon-js-car-example/game/babylon-game.js at main · manuelhintermayr/babylon-js-car-example). Again, great work, thanky for sharing.

2 Likes

Your game is looking great so far - feels a lot more fun than the original playground for sure! I ran it with the headlights and tail lights enabled but it ran totally smoothly on my computer so it’s hard to pin point any problems.

In theory these should help though:

  • Swap the pointlights for spotlights (setting the angle to 120 or less ideally)
  • Removing shadow generators from the tail lights (you would probably barely see the shadows these generate anyway)
  • Reducing shadow generator texture size

Hopefully that can get it running better on your machine!

2 Likes

Thank you for the input, i love now it works with the lights and very smooth

Only the lights seem to have no effect at all at the car (light reflections/lightning, just like in your video), only all the other objects, but i think after that, it is a really cool playground example (updated code is in the github repo)

1 Like

See my fork for reflections and glow layer :slight_smile:

1 Like

You may make some parts of the car more reflective, especially glass parts, and, for example, apply clearcoat to the metal parts.

Love it
preview

Thanks, I included the solution in the main repo for everyone else to see in action :slight_smile:

4 Likes