SHARE
TWEET

Untitled

a guest Oct 21st, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends KinematicBody2D
  2.  
  3. # Porsche Boxter S specific variables. Can be changed to arrays to account for more than one car:
  4. var gearRatios = [3.82, 2.20, 1.52, 1.22, 1.02, 0.84] # gk
  5. var currentGear = 0
  6. var numberOfGears = 6
  7. var G = 3.44 # Final drive ratio
  8. var wheelRadius = 0.3186 # rw
  9. var omegaRedline = 7200 # Highest possible rpm.
  10. var currentOmega = 0 # RPM, 0 at start
  11. var Cd = 0.31 # Drag-coefficient
  12. var area = 1.94 # Frontal area of Porsche Boxter S
  13. var mu = 0.015 # Coefficient of rolling friction
  14. var Te = 0 # Torque engine
  15. var mass = 1393 # Mass in kg
  16. var currentTopSpeedRedline = 0 # Top speed without friction or drag
  17. var currentTopSpeed = 0 # "Real" top speed
  18. var airDensity = 1.2 # kg / m^3
  19. var b = 0 # Slope of torque-curve
  20. var d = 0 # y = kx + m, this is the m of the torque-curve
  21. var gravitation = 9.82 # m / s^2
  22. var Fd = 0 # Drag-force
  23. var Fr = 0 # Friction-force
  24. var Tw = 0 # Torque wheel
  25. var totalForce = 0
  26. var currentAcceleration = 0
  27. var currentVelocity = 0
  28. var Pe = 0 # Power of the engine
  29. var we = 0 # Rotationspeed of the engine
  30. var Teb = 0 # Torque engine braking
  31. var muBraking = 0.74 # Engine braking coefficient
  32. var accelerationBraking = -5.0 # m/s^2. Calculated with existing data and a = -v0^2 / 2x. Though estimated for he car.
  33. var maxBackingSpeed = -20
  34.  
  35. # ATT FIXA:
  36. # * determineTopSpeed()
  37.  
  38. func determineTorque(rpm): #OK!
  39.     if (rpm <= 1000):
  40.         Te = 220
  41.         b = 0
  42.         d = 220
  43.     elif (rpm < 4600):
  44.         Te = 0.025 * rpm + 195
  45.         b = 0.025
  46.         d = 195
  47.     else:
  48.         Te = -0.032 * rpm + 457.2
  49.         b = -0.032
  50.         d = 457.2
  51.        
  52. func determineTopSpeed(currentGearRatio, currentOmega): #INTE OK!
  53.     determineTorque(currentOmega)
  54.     # Float variablerna strular och avrundas till 0...
  55.     var c1 = -0.5 * (Cd * airDensity * area) / mass
  56.     var c2 = (60 * (currentGearRatio * currentGearRatio) * (G * G) * b) / (2 * PI * mass * (wheelRadius * wheelRadius))
  57.     var c3 = ((currentGearRatio * G * d) / (mass * wheelRadius)) - (mu * gravitation)
  58.    
  59.     var root = sqrt((c2 * c2) - 4 * (c1 * c3))
  60.     var speed1 = (-c2 + root) / (2 * c1)
  61.     var speed2 = (-c2 - root) / (2 * c1)
  62.    
  63.     currentTopSpeed = max(speed1, speed2)
  64.    
  65.     # currentTopSpeed = max(((-c2 + sqrt((c2 * c2) - 4 * (c1 * c3))) / (2 * c1)), ((-c2 - sqrt((c2 * c2) - 4 * (c1 * c3))) / (2 * c1)))
  66.     # Keep an eye on this... Might be max instead of min...
  67.    
  68. func determineTopSpeedRedline(currentGearRatio): #OK!
  69.     currentTopSpeedRedline = (2 * PI * wheelRadius * omegaRedline) / (60 * currentGearRatio * G)
  70.    
  71. func determineDrag(currentVelocity): #OK!
  72.     Fd = 0.5 * Cd * airDensity * (currentVelocity * currentVelocity) * area
  73.  
  74. func determineFriction(): #OK!
  75.     Fr = mu * mass * gravitation * cos(0)
  76.    
  77. func determineTw(currentGearRatio): #SEEMS OK, DEPENDS ON RPM
  78.     determineTorque(currentOmega)
  79.     Tw = Te * currentGearRatio * G
  80.    
  81. func determineTotalForce(currentGearRatio, currentVelocity): #OK!
  82.     determineTw(currentGearRatio)
  83.     determineFriction()
  84.     determineDrag(currentVelocity)
  85.     totalForce = (Tw / wheelRadius) - Fr - Fd
  86.    
  87. func determineAcceleration(currentGearRatio, velocityInput): #SEEMS OK, DEPENDS ON RPM DOE?
  88.     determineTotalForce(currentGearRatio, velocityInput)
  89.     currentAcceleration = totalForce / mass
  90.    
  91. func determineWe(rpm): #OK!
  92.     we = (2 * PI * rpm) / 60
  93.    
  94. func determineEnginePower(rpm): #OK!
  95.     determineTorque(rpm)
  96.     determineWe(rpm)
  97.     Pe = Te * we # Power of the engine = Torque of the engine * rotational speed of engine
  98.    
  99. func determineOmegaE(velocity, currentGearRatio): #OK!
  100.     currentOmega = (velocity * 60 * currentGearRatio * G) / (2 * PI * wheelRadius)
  101.  
  102. func determineCurrentVelocity(rpm, currentGearRatio): #OK!
  103.     currentVelocity = (wheelRadius * 2 * PI * rpm) / (60 * currentGearRatio * G)
  104.     determineAcceleration(currentGearRatio, currentVelocity)
  105.    
  106. func rpmAfterShift(currentGearRatio, newGearRatio): #OK!
  107.     currentOmega = currentOmega * (newGearRatio / currentGearRatio)
  108.     if (newGearRatio < currentGearRatio):
  109.         currentGear += 1
  110.     elif (currentOmega < 1000):
  111.         print("TOO LOW OMEGA FOR VEXLING, FUCK YOU, NO VEXLING! KEEP OMEGA AND WEXEL")
  112.         currentGear -= 1
  113.    
  114. func determineEngingeBraking(rpm): #OK!
  115.     Teb = muBraking * (rpm / 60)
  116.    
  117. # TESTING:
  118. func determineGas(delta, input):
  119.     determineTopSpeedRedline(gearRatios[currentGear])
  120.     if (currentGear == 5):
  121.         determineTopSpeed(gearRatios[5], currentOmega)
  122.         currentTopSpeedRedline = currentTopSpeed # In higher gears, aka 5th gear, we use the top speed with drag.
  123.         print(currentTopSpeedRedline)
  124.    
  125.     if (currentVelocity < currentTopSpeedRedline && input == "ui_up"):
  126.         currentVelocity = currentVelocity + currentAcceleration * delta # Acceleration
  127.         determineOmegaE(currentVelocity, gearRatios[currentGear])
  128.         determineAcceleration(gearRatios[currentGear], currentVelocity)
  129.        
  130.     elif (currentVelocity > maxBackingSpeed && input == "ui_down"):
  131.         determineEngingeBraking(currentOmega) # The engine braking depend on the rpm
  132.         var brakeForce = -Teb / wheelRadius # Like the torque forward of the engine but opposite :)
  133.         determineDrag(currentVelocity) # Drag affects the braking as well and needs to be updated based on currentVelocity
  134.         var engineBraking = (brakeForce - Fr - Fd) / mass # acceleration of engine braking
  135.        
  136.         currentVelocity = currentVelocity - (currentAcceleration + engineBraking) * delta # Active braking + engine braking
  137.         determineOmegaE(currentVelocity, gearRatios[currentGear])
  138.         determineAcceleration(gearRatios[currentGear], currentVelocity)
  139.        
  140.     else:
  141.         determineEngingeBraking(currentOmega) # The engine braking depend on the rpm
  142.         var brakeForce = -Teb / wheelRadius # Like the torque forward of the engine but opposite :)
  143.         determineDrag(currentVelocity) # Drag affects the braking as well and needs to be updated based on currentVelocity
  144.         var engineBraking = (brakeForce - Fr - Fd) / mass # acceleration of engine braking
  145.         if (currentVelocity > 0):
  146.             currentVelocity = currentVelocity + engineBraking * delta # Enginebraking
  147.             determineOmegaE(currentVelocity, gearRatios[currentGear])
  148.             determineAcceleration(gearRatios[currentGear], currentVelocity)
  149.         elif (currentVelocity < 0):
  150.             currentVelocity = currentVelocity - engineBraking * delta # Enginebraking
  151.             determineOmegaE(currentVelocity, gearRatios[currentGear])
  152.             determineAcceleration(gearRatios[currentGear], currentVelocity)
  153.         print("engineBraking: ", engineBraking)
  154.        
  155.    
  156.  
  157. # Old variables:
  158. var wheelDist = 70
  159. var angle = 15
  160. var power = 800
  161. var friction = -0.9
  162. var drag = -0.001
  163. var breaking = - 450
  164. var speedReverse = 250
  165.  
  166. var acceleration = Vector2.ZERO
  167. var velocity = Vector2.ZERO
  168. var steerAngle
  169.  
  170. func _physics_process(delta):
  171.     acceleration = Vector2.ZERO
  172.     get_input(delta)
  173.     calculate_steering(delta)
  174.    
  175.     print("v = ", currentVelocity, " acc = ", currentAcceleration, " rpm = ", currentOmega, " gear = ", currentGear + 1)
  176.     velocity = move_and_slide(12 * currentVelocity * transform.x) # * 12 for more realistic movement in the scale of the sprites
  177.  
  178. func get_input(delta): #FIX
  179.     #Turn or not turning
  180.     var turn = 0
  181.     if Input.is_action_pressed("ui_right"):
  182.         turn += 1
  183.     if Input.is_action_pressed("ui_left"):
  184.         turn -= 1
  185.     steerAngle = turn * deg2rad(angle)
  186.    
  187.     #Accalerations forward and for breaking
  188.     if Input.is_action_pressed("ui_up"):
  189.         if (currentOmega == 0): # start of engine
  190.             currentOmega = 1000
  191.         determineGas(delta, "ui_up")   
  192.     elif Input.is_action_pressed("ui_down"):
  193.         determineGas(delta, "ui_down")
  194.     else:
  195.         determineGas(delta, "")
  196.    
  197.     # Gear up or down
  198.     if Input.is_action_just_pressed("ui_select"):
  199.         if (currentGear < 5):
  200.             rpmAfterShift(gearRatios[currentGear], gearRatios[currentGear + 1])
  201.             determineTopSpeedRedline(gearRatios[currentGear])
  202.     if Input.is_action_just_pressed("ui_cancel"):
  203.         if (currentGear > 0):
  204.             rpmAfterShift(gearRatios[currentGear], gearRatios[currentGear - 1])
  205.             currentGear = currentGear - 1
  206.             determineTopSpeedRedline(gearRatios[currentGear])
  207.        
  208.     #Animations
  209.     if velocity.length() > 0:
  210.         $AnimatedSprite.play("Forward")
  211.     if velocity.length() < 0:
  212.         $AnimatedSprite.play("Backwards")
  213.     if velocity.length() == 0:
  214.         $AnimatedSprite.stop()
  215.     if turn > 0:
  216.         $AnimatedSprite.play("Turn")
  217.         $AnimatedSprite.flip_h = false
  218.     if turn < 0:
  219.         $AnimatedSprite.play("Turn")
  220.         $AnimatedSprite.flip_h = true
  221.  
  222.  
  223. func calculate_steering(delta):
  224.     # Location of front- & rear wheel
  225.     var rearWheel = position - transform.x * wheelDist / 2.0
  226.     var frontWheel = position + transform.x * wheelDist / 2.0
  227.     rearWheel += velocity * delta
  228.     frontWheel += velocity.rotated(steerAngle) * delta
  229.    
  230.     # Calculating our new velocity
  231.     var newVelocity = (frontWheel - rearWheel).normalized()
  232.     var d = newVelocity.dot(velocity.normalized())
  233.     if d > 0:
  234.         velocity = newVelocity * velocity.length()
  235.     if d < 0:
  236.         velocity = -newVelocity *min(velocity.length(), speedReverse)
  237.     rotation = newVelocity.angle()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top