Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to add Clutch setting in Truck Physics.

Discussion in 'Scripting' started by Kashif Amin, Nov 3, 2010.

  1. Kashif Amin

    Kashif Amin

    Joined:
    Oct 28, 2010
    Posts:
    30
    Dear all, kindly advise me about the clutch setting of Truck . There is no tutorial available on how to simulate clutch in vehicle.
     
  2. FluidPixel_Gavin

    FluidPixel_Gavin

    Joined:
    Jan 6, 2010
    Posts:
    88
    bool bIsClutchPressed;

    if( bIsClutchPressed )
    // Allow gear changes

    You arent going to find a tutorial for such a game specific thing and why youd want to simulate such a boring action i dont understand.
     
  3. dgutierrezpalma

    dgutierrezpalma

    Joined:
    Dec 22, 2009
    Posts:
    404
    Simulating a clutch is a very complex task. I have read several "racing physics" books and I haven't found enough information to do a complete realistic simulation of the clutch, but this is what I do:

    1) Clutch pedal not pressed at all (clutch = 0.0f):
    - I use the average drive wheels RPM in the last frame to update the engine RPM in the current frame.
    - The engine transmit all its power to the drive train (except a small lose because of friction).

    2) Clutch pedal completely pressed (clutch = 1.0f):
    - In this case, I has a variable called "engineInertia" that I use for updating the engine RPM.
    - The engine doesn't transmit any power to the drive train.

    3) Clutch pedal is pressed but not completely pressed (clutch > 0.0f clutch < 1.0f):
    - I calculate the weighted average using the clutch position as a weight coefficient.


    Of course, you should only change gears when the clutch pedal is completely pressed and if the user want to change gears without pressing the clutch pedal, you should simulate this in code.
     
  4. Ravel

    Ravel

    Joined:
    Nov 21, 2010
    Posts:
    605
    Hey David, would you also be so kind and tell me how you calculate the engine rpm relative to wheels rpm?(A code/script would be nice)
    My problem is that i calculate the engine rpm from only the wheels angular velocity, so there is no acceleration (because the wheels are not rotating when the car is standing still).
    This is where i think clutch should do the magic but it needs some magic constants and calculations i think...

    In real life it's the way that when you release the clutch, the engine transmits the throttle power to the wheels, and then wheels find the friction of road and kinda slow down the engine?

    Anyway thanks for reading and sorry for my bad english.
     
    Last edited: Nov 21, 2010
  5. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Welcome to the forum, Ravel!

    The ratio of the engine RPM to wheel RPM is just the gear ratio. Although an engine running at 0 RPM technically isn't producing any power at all, the torque isn't actually proportional to engine RPM. It is usually easiest just to design the engine's output curve so that it is still producing torque at 0 RPM rather than simulate the complex action of the clutch, unless you need to produce a very realistic simulation of driving which includes engine stalls, etc.
     
  6. Ravel

    Ravel

    Joined:
    Nov 21, 2010
    Posts:
    605
    Thanks for the welcome andeeee!

    Well im kinda working on a racing sim, i allready have torque curves and gear ratios etc. But the problem is that the simulation is not doing the "natural acceleration"(for burnouts and stuff you can do in real car games/sims) because i dont have a clutch(I think this is the reason? Because i allready have a working tire model, pacejka)
    so i am basically trying to understand on how the clutch should look and work in a mathematical value.

    My current project is done after reading marco monsters tutorial, but it did not cover the clutch part...
    To make it simple, my engine maths are something like this:
    Now I would like to know, where the damn should i add the clutch?
    So that it would work, and how should i add it. I would like to know how clutch simply works in a mathematical value, just like i get the rpm etc.

    Thanks for reading! And i hope someone could help me explaining the clutch torque transfers and stuff like that. Because i can do the very basic clutch by myself, but the part that i am confused is the clutch torques and powers and choclates and milk and spagethyyyyy LOL:confused:
     
    Last edited: Nov 22, 2010
  7. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    IANA-Physcist but I thought burnouts occur due to wheel slippage - the power on the wheels overcomes friction and the wheels spin rather than grip.

    The only thing the clutch has to do with it is that 'popping' the clutch at high RPMs provides a sudden application of power to a static wheel. This overcomes static friction and sends the wheel into a slipping state. Since static friction is higher then rolling resistance/friction, the wheels continue to spin rather than grip, creating heat which burns the rubber.

    Contrast this with a 'good' start - enough power is applied to the wheel to get it to roll, but not enough to exceed static friction.

    I probably have the details wrong, but I think it is actually the tire model, and the speed/magnitude you apply torque into the tire model, that will give you slippage and wheelspin.
     
  8. Ravel

    Ravel

    Joined:
    Nov 21, 2010
    Posts:
    605
    well tire model plays alot rolle in this case yes, but the problem is, that when i gswitch from reved up neutral gear to the first gear, there is no rpm acceleration, if you know what i mean? In real life the wheels would start to spin and slip and then finally find the grip(this is where tire model thakes its action but the rest, engine reving and acceleration is caused by the mehanics of transmission and clutch torque transfer) ... attleast i think this is how it is in real life?

    The thing is that when i switch from my "reved up" neutral gear to the first gear, then the engine simulation starts from 1000 RPM, as the wheels are having no torque... because the Torque Curve Starts from 0 and goes to 90 for example and so on step by step.

    What i need, is to transfer the reached torque from reved up, neutral gears torque, to the shifted up gear, using the "magical" clutch.

    I hope you understand where i am aiming at...
     
    Last edited: Nov 22, 2010
  9. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    Ah. Well I'm gleefully wading into waters that are too deep for me, but it looks like you are using wheel RPMs to set engine RPMs, then using engine RPMs to drive wheel RPMs.

    Wouldn't it make more sense to set Engine RPM to a minimum based on wheel rpm, then use throttle position to add more RPMs? So where the clutch is in (transmission is disengaged) , RPMs are set entirely by throttle (and throttle lag)?

    Are you going off a known technique for modeling cars? If so, is this model a feels-good-enough type model or one that is physically based? If the former, it might just not work the way you think it should and adding the clutch might just be kludge on top of another kludge.

    (BTW, in the model you have outlined above, it looks like your throttle is actually acting more like a clutch or steering brake than a throttle. Which is fine if you are driving a wwii tank or a sno-cat, I suppose)
     
    Last edited: Nov 23, 2010
  10. dgutierrezpalma

    dgutierrezpalma

    Joined:
    Dec 22, 2009
    Posts:
    404
    Modifying your example:
    Code (csharp):
    1.  
    2. // Clutch is a float var with a value between 0f (not pressed at all)
    3. // and 1f (completely pressed)
    4.  
    5. // Engine_Inertia  is a float var with a value greater than 0
    6.  
    7. // Calculate the average wheel RPM and the Transmission Ratio
    8. Left Wheel Rpm = Left Wheel Angular Velocity *60 / (2 * Pi)
    9. Right Wheel Rpm = Right Wheel Angular Velocity *60 / (2 * Pi)
    10. Transmission Ratio = Gear Ratio * Differential Ratio
    11.  
    12. // Calculate the Engine RPM when the clutch is completely pressed and
    13. // when it isn't pressed at all
    14. Clutch_NP_Engine_RPM = (Left Wheel Rpm+Right Wheel Rpm)/2 * Transmission Ratio
    15. Clutch_CP_Engine_RPM = Engine_Inertia*Throttle + Engine RPM /*last iteration*/
    16.  
    17. // Calculate the final Engine RPM
    18. Engine_RPM = Clutch * Clutch_CP_Engine_RPM + (1-Clutch) * Clutch_NP_Engine_RPM
    19. Engine_RPM -= GetEngineFriction(EngineRPM)
    20.  
    21. // Calculate the drive torque
    22. // Engine Torque = Throttle Position * Max Torque
    23. // I have commented this line because I use a more complex function
    24. // to calculate the torque
    25.  
    26. Engine_Torque = Throttle * GetTorque(EngineRPM)
    27. Drive Torque = (1 - Clutch) * Engine Torque * Transmission Ratio * Transmission Efficiency
    28.  
    When the player shift gears (including when he goes from neutral to 1st gear) the clutch is pressed automatically and then it is released smoothly. If the "releasing time" is smaller, it should be easier to make burnouts (because you apply more torque to the wheels in less time and they start to slip) but the tire model is fundamental for achieving realistic burnouts.

    To release the clutch smoothly the easiest way would be using a coroutine or a animation framework such as iTween.
     
    Last edited: Nov 23, 2010
  11. fededevi

    fededevi

    Joined:
    Jun 11, 2010
    Posts:
    29
    You have to consider your engine and you wheels like 2 spinning wheels with a certain mass.
    The clutch value (0 to 1) and the RPM difference between the 2 systems will control how much energy/time will be transferred from the engine to the wheels.

    Energy/time transferred should be proportional to:
    Let c be the "clutch" value
    And d the delta between wheels rpm and engine rpm. (assuming gear ratio: 1)

    E/s = d*c(1/(1-c))
    With c=0 no energy is transferred.
    With c=1 infinite energy is transferred.

    Example:
    wheel mass 1
    engine mass 1
    gear ratio 1

    FRAME1:
    clutch = 0
    engine = 1000 rpm
    wheels = 0 rpm

    No torque is transferred to the wheel.
    .
    FRAME2
    clutch = 0.5

    2 units of time have passed.
    DeltaRMP = 1000;
    energy/time transferred = 1000 / 0.5 = 5;

    A torque proportional to 2000 is applied to the wheels.
    A torque proportional to -2000 is applied to the virtual engine wheel.
    (Wheel collider will manage slip/ wheel rpm)

    You should set a maximum torque or the value may become huge with clutch values near 1. (Example maxEngineTorque X 10, or something like that). Also energy lost by engine in a frame should never bring engine rpm below wheels rpm (assuming gear ratio:1)

    PS: This is probably physically completely incorrect but should give a good approximation of reality.
    PS2: Quite a few special cases need to be handled (DeltaRPM = 0, clutch=1, etc )
    PS3: Once the clutch became 1 and wheels rpm matches engine rpm you should ignore the clutch and apply your engine rpm directly to the wheels.
    PS4: I have no idea if this really works well :)
     
  12. Ravel

    Ravel

    Joined:
    Nov 21, 2010
    Posts:
    605
    Hey, David, i think i owe you a big one, because i think this is exactly what i was after! Thanks!, but now i found that my aerodynamics are wrong, so im starting to work on that, but still thanks a lot!
     
  13. Sondre

    Sondre

    Joined:
    Jan 12, 2013
    Posts:
    2
    I see this is an old thread, so I hope no one mind me reviving it...
    Hi, I am developing a driving game on a game engine called 3drad (but my question isnt engine spesific though).
    I've had some trouble putting together a proper gearbox script, but reading Unity forums were very helpful, and this tutorial aswell:http://www.asawicki.info/Mirror/Car Physics for Games/Car Physics for Games.html

    I got the rpm calculations figured out and, now I am trying to get the wheel torque calculation right. I am a lil confused because I've seen many different equations for this...
    In on example it is: wheelTorque = engineTorque / (finalDriveRatio * gearRatio);
    In the car physics tut, its like this: drive torque = engine_torque * gear_ratio * differential_ratio * transmission_efficiency

    I guess I'm missing something, because torque should increase as the gears increase...

    When I use the first one I dont get any torque at all, for the secound one, I get way to much torque in first gear and low torque in the higher gears..
     
    Last edited: Feb 9, 2013
  14. narlankaaditya

    narlankaaditya

    Joined:
    Sep 23, 2020
    Posts:
    4
    Yes second one is correct. Torque should decrease as u go up the gears