Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Showcase Incredibly good deep dive into car physics

Discussion in 'Physics' started by Kurt-Dekker, Jan 29, 2023.

  1. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    This video by Toyful Games does an amazing job breaking down the physics and how to simulate it:



    Best of all you can play it in their game, Very Very Valet for Nintendo Switch
     
    Last edited: Jan 30, 2023
  2. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    2,063
    Just watched it, it was beyond amazing. Not just a good introduction to car physics, but to a lot of basic concepts like springs, friction and acceleration.

    Highly recommended, thanks Kurt!
     
    Kurt-Dekker and Unifikation like this.
  3. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,565
    Absolutely! What a great introduction to what is the absolute essentials to simulate car physics. Even realistic car physics begin with very similar basis.

    However, in my opinion there's a flaw with the description of the suspension that may lead to misunderstandings. The description of the spring (minute 5:00) says that the spring can be stretched and that produces a force in the opposite direction. Then it states that the "offset" must be signed. While this is true for springs, it is not for car suspensions. The video leads to the erroneous conclusion of that a negative force must be applied to the car's rigidbody. In actual car physics this doesn't apply: if the suspension spring happens to get stretched somehow, then that wheel is in the air and that suspension doesn't apply any force to the rigidbody.

    Also, the video doesn't state any maximum distance for the raycast. If the maximum distance is the rest position of the spring, then it will be mostly ok and in line with actual car physics. If it's beyond it will apply negative forces to the rigidbody, which is pure arcade car physics. If there's no maximum distance (infinity) then the car will never jump or leave the ground.

    Don't get me wrong, the video is awesome describing a simulation model intended for arcade vehicles. Applying negative suspension forces is a perfectly valid trick to improve the stability, and the gameplay (i.e. how easy/hard the vehicle can roll over or jump) can be fine tuned easily. However, I think it's important to mention this aspect.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    I saw that too!!! But I played with it a bunch and basically what I ended up with this this for my suspension:

    Code (csharp):
    1. namespace ArcadeCar
    2. {
    3.     using System.Collections;
    4.     using System.Collections.Generic;
    5.     using UnityEngine;
    6.  
    7.     [CreateAssetMenu]
    8.     public class CarSuspensionParams : ScriptableObject
    9.     {
    10.         public float SuspensionFullDistance;
    11.         public float SuspensionRestDistance;
    12.         public float SuspensionSpring;
    13.         public float SuspensionDamping;
    14.  
    15.         void Reset()
    16.         {
    17.             SuspensionFullDistance = 1.0f;    // <--- full raycast (maxDistance:)
    18.             SuspensionRestDistance = 0.7f;   // <--- what we consider zero
    19.             SuspensionSpring = 1.0f;   // <- scaled to mass of car
    20.             SuspensionDamping = 0.5f;  // <- scaled to mass of car
    21.         }
    22.     }
    23. }
    And with the computation, as you point out, when the "spring" is beyond length, yes, it does compute a negative P term, which makes the wheel sort of like "glue" and pulling down on the car at that point.

    This is NOT how real physics works but you know what? It is very subtle but actually helps the car feel a bit more solid and stuck to the road!!

    Anyway, when I'm finished fiddling with this I'm putting it all out on a github repo, but I didn't get that done over the weekend, so it might need to wait until next weekend.
     
    bigy and Edy like this.
  5. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,565
    Exactly! In the end, this model is for gameplay and doing so is a perfectly valid technique to improve stability. Or, more importantly, a stability that can be configured easily.

    My concern is that watching the video can lead one to believe that car suspensions work in that manner, which is incorrect.
     
    Kurt-Dekker likes this.
  6. HardCoreBanda

    HardCoreBanda

    Joined:
    Apr 23, 2022
    Posts:
    1

    Hey!
    I Am Very New TO Unity And I Have No Idea Of How To Implement This Physics In A Unity Project
    Any Help From You Guys Can Teach Me ALot
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,571
    Welcome. Be sure to read forum rules. Do NOT necro-post to old threads.

    Sounds like you need to get busy with Youtube tutorials... LOTS of them. Here's how:

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Imphenzia: How Did I Learn To Make Games:

     
  8. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,661
    Since the thread was necroposted anyway, I played with the concept in that video, but must've done something wrong with my implementation of the slip forces. Even though all four springs were oriented in the same direction, I always end up with a runaway torque buildup on the body and the body starts spinning and drifting away. The video doesn't really cover yaw torque at all.
     
  9. amirkhanpashtun

    amirkhanpashtun

    Joined:
    Apr 16, 2023
    Posts:
    2
    Can you write down your github link to your this car physics project?
     
  10. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    One of the most significant issues I've been trying to solve is how to imitate static friction. No matter what I do, I can't seem to eliminate sliding on sloped surfaces like the native wheel collider can do. :(
    Any pointers or help on this matter would be greatly appreciated.
    Thank you. :)
     
  11. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    2,063
    Get the velocity of the car and the normal of the surface it is sitting on. Project the velocity vector onto the surface normal, and subtract the result from the original velocity. This gets you the velocity on the direction tangential to the surface.

    If this velocity is below a threshold, just apply an acceleration in the opposite direction (same magnitude). This has the effect of zeroing out any velocity along the surface below the threshold, which should get you a pretty good approximation of static friction.
     
    Last edited: Mar 4, 2024
    Ruchir likes this.