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!
