Space Dust Racing UE4 Arcade Vehicle Physics Tour

Space Dust Racing UE4 Arcade Vehicle Physics Tour

Hello, this is Michael Davies from Space Dust
Studios. In this video, I’m going to give you a whirlwind tour of our custom vehicle
physics implementation for our party combat racer Space Dust Racing, which is coming in
2015 for PC and consoles. We’re using Unreal Engine 4, but I’m not going to get into any
code, blueprints or formulas, this is really just an overview of the general principles
that we’re using, and you can use the same approach within Unity 3D or any other game
engine that has a physics component. I’m assuming some basic knowledge of physics engine principles,
so velocity, forces, torques, raycasts, and also some basic knowledge of 3D coordinate
spaces, vectors and so on. To start off, let’s talk about the requirements
for Space Dust Racing. We’re aiming for fairly low-detail arcade physics, not high detail
simulation like you’d find in Gran Turismo. We want bouncy suspension, we want the ability
to flip and roll vehicles, and the ability to crash into other vehicles and ram them
off the road. We don’t want to get bogged down in complex traction simulation, or gear
ratios, or you know, forward wheel drive or rear wheel drive support, generally just putting
too much detail into the individual wheel simulations, because some of our vehicles
might not have wheels, they might hover, or have tank treads, and we want the same physics
for all of them regardless of how they look, for gameplay balancing reasons. We want the
ability to toggle the physics on and off at any point, for example making the vehicle
perform a canned animation like a winning dance, and we also want the ability to change
the physics completely in certain states, like spinning out in oil slicks. We’re not
using Unreal Engine 4’s inbuilt vehicle physics, as this is a bit too simulation oriented for
our needs, and it’s not easy to override, or in other words hack to pieces, which is
what we’re going to do. So let’s start out with a simple physics box
primitive. This is going to be our basic collision proxy for the vehicle, the thing that will
hit the world and other vehicles. This could easily be a custom physics mesh, a capsule,
a sphere, a compound of multiple primitives, whatever fits around your vehicle’s visual
geometry the best. We’ll add the visual component later on. For now since this vehicle is just
a lump, I’ve hooked up a debug button to flip it by applying a physics impulse, and now
we can just see how it reacts when it lands on the ground. Pretty exciting stuff so far. Now let’s add some suspension to the bottom
corners of this box, so it’s pushing itself up off the ground, just like actual wheels
would. To do this, every frame we perform a raycast from each bottom corner of the box
– so the front left, the front right, the back left, the back right – going downwards
in the local vehicle space for the length of the suspension, so in Space Dust Racing
it’s about 60cm for us. Let’s focus on one raycast for a second. If
this raycast doesn’t hit anything, then the suspension is fully extended so we don’t need
to do anything, we can let the physics continue simulating, but if it hits something, we can
calculate the compression ratio of that suspension as a number between 0 and 1, so 0 means it’s
fully extended – it hasn’t hit anything – and 1 means it’s fully compressed, so it’s at
absolute compression, so the hit point is exactly where it started the raycast. Then
all we need to do is add an upward force on the box at the appropriate position using
the physics engine. We scale this upward force by the compression ratio, so basically more
force acts on the vehicle as the suspension compresses more. Now when you look at all
four raycasts doing this work together, you get this great bouncy suspension effect and
it keeps the vehicle elevated off the ground. One side note: For each raycast that hits
something, we’ll make sure to remember the compression ratio for that suspension, the
surface impact point, and also the surface impact normal, and these are results of the
raycast, and we’ll use them later on to help us figure more stuff out. We’re off to a kind of cool bouncy start,
but we still can’t accelerate or brake. So to fix this, whenever the accelerate button
is pressed, we apply a forward force to the vehicle. Braking is just the same thing with
a negative force. When you’re finding the vehicle’s forward direction, it’s best to
project it onto the road plane, which stops the vehicle from launching off the ground
if it’s tilting upwards a little bit, or pushing into the ground if it’s tilting down. And
because we remembered the impact details from those raycasts earlier on, we already have
the information we need to calculate all this stuff. Another simple trick is to add the accelerating/braking
force at a slightly lowered position from the center of mass, towards the front of the
vehicle, and this makes the vehicle tilt backwards and forwards as you accelerate and brake,
just like a real car would. It’s very easy to overdo though, so you have to tune it to
get it feeling about right. Now we need to make this thing turn based
on the left/right turning amount from the input device. This is a little bit more interesting.
The naive approach here might be to simply bypass the physics engine and just rotate
the vehicle around its up axis each frame, but this skips around the physics solver which
will lead to glitches when collisions happen, and the proper way to do this for a physics
based approach is to apply a torque instead, and let the physics engine do all the rotating
for us. This ensures that if the vehicle is obstructed, maybe there’s a wall in the way
or another vehicle, it will react appropriately and not glitch out. The basics of the vehicle controls are in
now, but we’ve still got no traction, and as you can see we’re slipping and sliding
all over the place. That’s kinda fun for a drifting mechanic or driving on ice, but we
need to get control over it so we can decide on the amount of slip applied to the vehicle,
maybe based on the surface or the road type. To fix this, each frame we can find the local
sideways component of the vehicle’s velocity, and apply a force to the vehicle in the opposite
direction scaled by some factor that feels good to us. This makes the vehicle grip the
road better, and we can play with this value to simulate all the different road surfaces
we want, from ice to concrete. There’s one final tweak we can make to keep
the vehicle upright more often, and that’s to lower the center of mass to be beneath
the vehicle, now this is totally fudged and not realistic, but it makes the vehicle act
like an inflatable punching bag with a really heavy bottom and a light top, and the effect
of this is that the vehicle always wants to roll back upright, and you’ll see this effect
in many offroad driving games where it’s important that the player can flip their vehicle, but
you still want to ensure that they can roll back on their wheels most of the time so that
they stay in control, because it’s a bit more fun and a bit more forgiving. In Space Dust
Racing, we use this hack for vehicles while they’re alive, but for dead vehicles we put
the center of mass back into the center of the object so that they’ll tumble and roll
more easily and it just looks a bit more fun that way. Essentially what we’ve done here
is just a series of hacks to make our box feel like a vehicle, and now we can just drop
a visual mesh on top for the chassis and the same for the wheels. For the wheels, to find
out where they should be relative to the chassis, we can use the suspension raycast information
we gathered earlier because we have the distance there. We can also reflect the turning input
on the wheels, and that’s done by just rotating them left/right by the amount of controller
input turning amount, with a little bit of smoothing. This is what it all looks like in action.
You can see the big advantage of using physics to drive your vehicle movement is that you
can now use physics impulses from explosions and crashes to throw your vehicles around,
and the physics does all of the hard work for you to make it look cool and give you
these exciting moments. With physics though the devil is definitely in the details, and
it does take quite some time to tune properties like friction, restitution, and also balance
forces and torques against each other to feel good, because everything affects everything
else. But with small adjustments and lots of playtesting, the results are definitely
worth it. So that’s it! I hope you found this video
interesting, if you have any questions or feedback you can leave a comment on our dev
blog, or email me directly. Please subscribe to our channel if you’d like to see more videos
like these and thank you for watching!

44 Replies to “Space Dust Racing UE4 Arcade Vehicle Physics Tour”

  1. That was a very good instructional video. Good build order of concepts, each explained with precision, and without getting too wordy. I hope you are going to continue making these because you're very good at them!

  2. This is very impressive and insightful. I really appreciate videos like this that don't just "show" but also explain how your achieving your results. Very cool stuff. Hope to see more of these in the future! 

  3. So I eventually managed to figure out the springs, but I can't for the life of me figure out how you do the angular drag force. Tried all sorts of combinations of rotators and vectors but can't seem to find the magic equation. Don't suppose you'd be able to show the way you did it?

  4. Disappointed… trying to find a good resource for car suspension as a newcomer to unreal, and not impressed with how the solution you put here wouldn't actually work in an actual game, missing important info on dampers that newcomers like me would need to know – and in fact you did something completely different for your demo anyway and didn't bother to mention it here. wasted my time 

  5. Hi,

    First off, this looks very cool and fun to play with.

    I've tried implementing this in Blueprint.

    I have a problem when it comes to damping though. I can't make it work. I can use normal physics damping stuff on the block itself, but I can't figure out how to implement the damping described in the reddit.

    Is there any chance you could elaborate on the solution from reddit?

    I'm talking about this text:
    Get the current velocity at the suspension contact point (Cv)
    Project it onto the vehicle up vector to find the current suspension force (Cf)
    Calculate the new suspension force (Nf = UpVector x CompressionRatio x SuspensionStiffness)
    Calculate the delta suspension force (Df = Nf – Cf)
    Add the delta suspension force to the suspension contact point

    Could you elaborate a bit on what you mean by each of your variables?

    What I understand is.
    Cv is the velocity of the suspension point. So the total velocity of that corner of the vehicle. (This doesn't seem right to me as that would include the speed of the car as well. Or do you mean the speed of the corner of the vehicle relative to the center of the vehicle?) So what is this?

    I know how to project the vector onto the up vector of the car, so I have that down I believe.

    UpVector, I'm guessing this is your "upward force" that you mention in the video.
    Compression Ratio is clear.
    Stiffness would be a thing not discussed in the video. So I guess we need to experiment with this.

    I hope you won't mind explaining a little bit in extra detail, it would be much appreciated.

    Thank you.

  6. After spending a few days on the ue4 vehicle physics… im tempted to start from scratch.  So I end up here and you really inspired me.

  7. This is so helpful that I will only need about 5 years of studying forces and vector math to get this to work the way you have. 😀 I got the box bouncing up and down and then hovering nicely with some dampening. However there are so many things that you didn't even mention that it will blow any beginner's mind and 500 hours away. Have a good day 🙂

  8. Awesome. I'll probably keep banging my head against the wall with the built in UE4 vehicle stuff. But I'm not exactly thrilled with what I've gotten so far. I like your approach alot more. I have alot of weird issues with the PHAT stuff currently. Maybe I'll try and get something like what you have here, up and running. Although I may not be smart enough, hehe.

  9. Hey SpaceDustStudios! Really great stuff, thanks a lot! We are currently working on a game that also requires custom vehicle physics, like you implemented, and I am trying for weeks now to get proper physics done, and the movement itself is cool, but the car is always jerking when not moving on a perfectly smooth plane. I think our approach was too "realistic", with a physics asset for the vehicle and collision boxes for each wheel and the chassis. So, you only use one collision box and simulate the wheels just optically? I really like the idea of the suspensions, my problem is that I don't know what values are appropriate for suspension and mass for a simulated object, but I guess I will come up with some by just testing. I think the suspension really could do the trick for the "laggy" movement on the ground. As I said, we are pretty happy with the movement itself, except for driving on non-planar underground.

    Ah one more question: So, you guys use physics forces to accelerate and decelerate / turn the car? Currently I am using just WorldOffsets, and I am facing some collision problems in the terms of crashes (distinguish between small impacts and bigger ones, that would slow down the car, for when the car can drive again). So I was thinking wheter to switch to use physics forces for this. How is your performance with this? I think we would need about 22 vehicles in a match. Okay, I bother more about movement-prediction. Do you have already tested multiplayer and movement prediction?

    I will subscribe to this channel, awesome project, and we would love to stay in contact and maybe exchange some knowledge and experience 🙂

    Thanks in advance and greetings 🙂

  10. This is awesome, thanks so much for making this video! It's very inspiring to see how you guys made your game and then also teach the principles in a very detailed manner, starting from the ground up. This is great video I know I'll keep coming back to when I'm working on vehicles!

  11. Hi, thank you for the very informative video. Something that most game companies do not do.

    I am experience the problem where the car takes off depending on where it's looking, For example, I jump off a ramp, I nose dive since it's launching off/on to the ground.

  12. Really great breakdown. I'm particularly curious about the traction/slip reduction. How do you project the velocity to the sideways vector in BP?

  13. If someone managed to get something really similar in UE4 blueprints, please share it! Even with this video, I attempted, but faced too many complications. I can't figure out how to dampen the suspension without dampening every direction, including gravity. Also I can't figure out how to properly assign the suspension to a skeletal mesh.

  14. Thank you so much, I loved this video. Short, crisp, beautifully explained and the best part, completely engine independent. But I think you should add tags like Vehicle, Physics, Mechanics, and also common engine names like Unreal and Unity. This is what I've been looking for but it never showed up in search results…finally had someone in the Godot Forum recommending this to me.

  15. Solid video! I am attempting to make a game with a huge amount of small cars (think, like, 100's of swarming micromachines) and your vid greatly helped me implement the car "drifting" physics I was looking for! I avoided the suspension, and simply made the base of each car a friction-less box (collider). But the rest? SO helpful!


  16. So good! Thank you so much for sharing 🙂 How does one calculate the surface angle from the raycast hit? (so that the car doesnt acclerate into the ground or air when the body isn't level with the ground)?

  17. Thank you, Mathew Wadstein recommends me to come here to solve the problem of a flying car that I would want to make. I would like to ask a question: how to solve the LineTrace can not let the asymmetric 3D car model correct hover off the ground? Once I changed the model, it will away roll over.

  18. Hi, anyone tried to implement this in 4.16? I'm not sure why I'm getting this results:
    and the blueprint:

  19. Thanks for the amazing tutorial. Just a quick question: to find the plane to project the forward vector on to, can I just average the impact normals?

  20. Such a helpful video on the high concepts. I wish we had more videos like this. I just need to find a Unity tutorial on raycast suspensions.

  21. Can anyone help me find the "local sideways component of the vehicles velocity"? I have no idea how to do that, and I've been stuck on it for weeks.

  22. I implemented it in Unity C#. Thanks for the excellent video Michael !!!

  23. video is great, the idea is simple yet very effective.
    and yet I fail big time trying to implement it in Unity. Every time I try to code 4-point balance system it just starting to oscellate up and down, resulting in jiggly mess of a car. There is probably some tricks to make forces stick around certain point, but I am yet to figure it out.

  24. Thanks for the help Mr. Smartypants. When I set the local sideways velocity it disables gravity. Are you going to make a tutorial video on how to fix that?

  25. I can make a system like this in roblox, using bodyposition, the position property of the bodyposition is based of the compression of the spring, which will now convert large bumps to small humps. Which is like a real spring :D!

Leave a Reply

Your email address will not be published. Required fields are marked *