Skip to main content
GameDev.net gamedev.net
🔒 Locked

Arcade car physics

Started by Brunni Nov 6, 2018 at 1:14 AM 23 replies 23.1k views
Original Post
Brunni
Brunni

I would like to make a kind of Mario Kart (a little different though, but the base would be great) and therefore I'm trying to acquire the background required to implement that in Unity.

I've seen this video mentioned in many places and it's indeed very interesting, so I went on and tried to implement it myself.

First, with the car suspensions. The guy doesn't mention the damper (a notion that I haven't quite grasped yet btw), he just talks about applying a force relative to the suspension compression. Is there any way it could work this way, or has he just forgot to mention it? By implementing it without damping my car will "wobble" when on the ground, but then I don't know what constants I should use for the spring.

Cheers :)

Septopus
Septopus

Yeah, that video is pretty awesome, I fudged my way into a similar solution a few months ago. When he was saying the devil is in the details and that it takes a lot of tuning, that's exactly what he was talking about. It took me a few weeks solid to get a similar game mechanic in Unity to work(was my first time working with physics like that). And I don't think I watched that video first. ;) It can work but you pretty much have to find all of those values for yourself, expose all of them that you can in the editor and just start playing with different values until they start behaving. If it's just wobbling when it's not moving, you could always turn some of the physics forces off when at rest.

Make sure you are applying the correct types of force, i.e. https://docs.unity3d.com/ScriptReference/ForceMode.html They are each very different, and sometimes they operate differently than you might think they will, unless you really have it down. Experiment with them.

I can say that starting small helps, only apply one force at a time(or group of them 4 corners/etc.) and only apply the minimal force you need to achieve the desired effect. Then add another force(or counter force) and tune it, then re-tune the first force because they will probably effect each other. Then when both of those are working perfectly, add another, and tune all 3 to work together.. etc.. etc.. It can be a little tedious. But worth it. ;)

Hope it helps, if you get stuck with some specific code, feel free to post it up. Lots of smart folks around here.

*Damper: The way I think about damper is that you're restricting the speed at which something can move or "bounce".

If you are trying to dampen a force, then you have to apply an opposite force that is a certain amount LESS than the force it is attempting to counter or (dampen).

Oh yeah, you can also create some nice dampening effects by modifying the rigidbody drag values. ;)

Brunni
Brunni

Thank you for your kind comment Septopus :) Glad to see someone tried that too :)

Yes that's what I'm intending to do, I like the self-taught process here. I have one specific question for now, it's "does it make any sense to implement a spring (suspension) the way he mentions? That is, only with a force proportional to the compression ratio of the suspension.". I've been toying a few days without it and the result is nowhere near satisfying. So I'm just wondering if that could even lead anywhere.

Septopus
Septopus

If that's the effect you are going for, yeah, this is about the only way to do it that I know of.

I think even the standard assets vehicle controller uses the same methodology, it's almost impossible to use out of the box though because it has TONS of fairly obscure variables.

Brunni
Brunni

Hmm sorry for double-checking, but that "only way" means that having just a force that pushes the vehicle back upwards depending on the current compression ratio (== distance from the bottom of the vehicle to the ground) is enough?

The standard unity assets use damping, among tons of things. But I don't truly understand this concept, only the formula.

Septopus
Septopus

You'll probably have to dampen it or try some different force modes, I'm sure the physics are a bit differently controlled between ue4 and Unity. I ended up scrapping my pure physics approach(which worked fine) due to game environment complications, so I can only really speak from memory. I believe I ended up scaling the forces independently based on individual distance to the ground, with a rapid fall off to equilibrium at the desired ride height. Matched by a downward force that kicked in rapidly when the corner height exceeded a threshhold(I was trying to avoid flipping completely).... Also, make sure you are setting a reasonable value for your rigidbody mass! This makes a huge difference in how the force modes misbehave. ;)

*fall off to equilibrium, not zero. If the force goes to zero you will hop... Which might be part of your "wobble"..

Septopus
Septopus

Another thing you can try is applying forces based on velocity, specifically dampening forces. Iirc, my dampening system was modified or triggered by point velocity checks. So if a corner's upwards velocity was over a threshold then the dampening would increase...etc..

Brunni
Brunni

Thanks for the comments :) Sorry for the lack of reply, I'm actually working towards it, learning every bit one after the other. So far I've spent the last few days on the springs. I thought I had got it alright, but there's a problem with slopes where the simulation sometimes blows, and it doesn't even support stiff suspensions (k > 150) without oscillating badly, so I'm back at it :)

In the meantime I've got the acceleration and braking working the way he describes in his video. It feels awesome, because no standard car simulation gets anywhere close to that :x

Brunni
Brunni

A little demo of what I have so far :)

It's all coming up so nicely. I feel like a student again haha, trying many things until I just get it right (and leave no room for something fishy or not figured out).

I'm not completely blocked for now. I'll add the downforce (starting with a simple force parallel to the normal to the ground surface below the car, multiplied by the velocity of the car in its local coordinate system), then friction (so that I can push the engine much further, as it's currently almost free-wheel, and it can fight gravity better, allowing for fun, arcade slopes) and change the shape of the car collider obviously (sphere?).

Will keep you posted :) thinking about starting a project topic btw, if you have something for that? :)


On 11/6/2018 at 6:04 PM, Septopus said:

Another thing you can try is applying forces based on velocity, specifically dampening forces. Iirc, my dampening system was modified or triggered by point velocity checks. So if a corner's upwards velocity was over a threshold then the dampening would increase...etc..

Yup exactly! Thinking about doing exactly that. Like him I have a target suspension compression of 0.75 (well in my system it means the opposite of him, where 1 is fully extended and 0 compressed), so I'm thinking about increasing the damping a lot when we reach 0.25, this way the car body will never actually hit the ground. This is one of the things that are so easy to do when you do it your own way: I believe Crash Team Racing didn't even check for collisions on the bottom of the body, only simple sideways checks, and then restituting the same equivalent force to both bodies so that they split quickly, and for walls it's likely the same as they do in most FPS (one thing that I haven't grasped at all yet).

Septopus
Septopus

That's looking really great!! You should definitely start a project page/blog here(it's all available on gamedev.net), it's a great way to get some more feedback and have a central location to keep your updates if nothing else. ;) Just click on Blogs or Projects from the home page and you should get the option to start a new blog or project respectively. A project page is just general information about your game/project, and a blog can be used to post updates about a project or anything on your mind really. Can't wait to see what you make!

Brunni
Brunni

Thanks a lot for your encouragement @Septopus B|

I'll do as you said then, just need some time to prepare it :)

In the meantime, I managed to get the sideways force implemented properly (got back to dot product and its amazing uses), fixed the suspensions which always cause issues, added friction (separated from the sideways force: it seems like you could go with one that does both, but if you really want the amazing drift mechanics like Crash Team Racing, you'll have to implement it properly. Thanks to that I could also lower the friction to a realistic minimum, and at the same time the force of the engine, so that the slopes provide a realistic slow down), made a more realistic downforce which allows to take loops reliably, put back the vehicle on its feet, computed the vehicle speed (in the flat surface plane, that is not counting the gravity component), added a dropout condition when taking sharp bumps and a few small things.

Now I'll have to actually modelize a track. Then add a way to know if you're going forward, if you achieve laps as expected, and handle collisions with other players. That's probably when I'll start to model my car not as a soap box but with spheres or similar :)

Septopus
Septopus

WOW.. That's a lot of meantime!! :D Once you're ready to start blogging I hope you write up some how-to's on this project for people to follow. As you well know, these mechanics can be fairly frustrating to get working.

Keep it up!! Can't wait to do some laps.

Brunni
Brunni

Thanks! :x

I just went ahead and did it right away ;)

It's really just a start and I'll take care of it, including much more laid-back explanations about the algorithmic contents :)

Brunni
Brunni

Really nice! :) looks like you have very heavy cars ;) and you implemented the gizmos and all!

Btw what does your collider look like? Is it a capsule, two spheres, a box, or something custom? (compound)

ongamex92
ongamex92
On 11/19/2018 at 6:08 AM, Brunni said:

Really nice! :) looks like you have very heavy cars ;) and you implemented the gizmos and all!

Btw what does your collider look like? Is it a capsule, two spheres, a box, or something custom? (compound)

Thanks a lot!
Well for that particular car it is just a convex mesh, similar to a box, but cut where the front window is (I'm at work currently so I cant show it to you).
Wheels do not have any collision representation, they are just ray casts. (I'm using Bullet Physics if anyone is intrested).

Hodgman
Hodgman

FWIW we have a "realistic" simulation used for driver training, traffic simulation (and making a futuristic racing game :D) and it uses a lot of the same techniques. Wheels are just ray-casts and a whole bunch of "springs". Instead of a single spring, the suspension can be multiple springs all connected together, plus the tire and rim are stiff springs too. In complex suspensions the two sides of the car might be linked somehow. The most complex difference is how friction is calculated -- for an arcade feel, you can just make up some friction based on forwards + sideways sliding and tweak the numbers until it feels fun. For a simulation feel, you try to copy Pacejka's magic friction :)

In our game, we also make the collision-shape for the car much higher off the ground than the visuals (e.g. if the car's floor is 10cm off the ground and the roof 1m off the ground, then the physics shape might have a floor that's 50cm off the ground). This does mean that when "bottoming out", the visuals can intersect with the ground somewhat... but it avoids an annoying gameplay moment where the vehicle-vs-ground collision causes unpredictable results for the player. It often feels more natural to just let the springs resolve the situation rather than a rigid-body collision.

Brunni
Brunni
On 11/20/2018 at 6:55 PM, ongamex92 said:

Thanks a lot!
Well for that particular car it is just a convex mesh, similar to a box, but cut where the front window is (I'm at work currently so I cant show it to you).
Wheels do not have any collision representation, they are just ray casts. (I'm using Bullet Physics if anyone is intrested).

OK got it, thanks :)

I switched back to a box for now because the elongated capsule would allow the car to (attempt) climbing on walls ? (since I made the capsule to be above the ground so that I use the raycast from suspensions to rest on the ground, the spherical "nose" with a good force applied forward from below the car could allow the physics to rotate it on its back and start climbing. I think I'll switch to a "home" like (triangle for the nose + box).

Another thing I struggled with was to get the physics engine to react ""kind of"" natural in this kind of situations:


I played Mario Kart on my 3DS the other night… man how good their engine is :D (it's clear that they don't use a physics engine, and the more I go, the more I see that it's completely pointless, I just have to learn the maths behind the "body.ApplyForceAtPosition" and how a box rotates based on an imbalance of forces at different points in the body)

Btw I've posted an article on my blog. I intend to continue as long as it feels useful, focusing on things that the video in my first post doesn't really explain in detail :)

bmarci
bmarci
10 hours ago, Brunni said:

I just have to learn the maths behind the "body.ApplyForceAtPosition" and how a box rotates based on an imbalance of forces at different points in the body

This is exactly what I did in my simulation :)

This is a quite good starting point: https://gafferongames.com/post/physics_in_3d/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.