• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Oct 21st, 2019 78 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.
121.     if (currentVelocity < currentTopSpeedRedline && input == "ui_up"):
122.         currentVelocity = currentVelocity + currentAcceleration * delta # Acceleration
123.         determineOmegaE(currentVelocity, gearRatios[currentGear])
124.         determineAcceleration(gearRatios[currentGear], currentVelocity)
125.
126.     elif (currentVelocity > maxBackingSpeed && input == "ui_down"):
127.         currentVelocity = currentVelocity - currentAcceleration * delta # Active braking
128.         determineOmegaE(currentVelocity, gearRatios[currentGear])
129.         determineAcceleration(gearRatios[currentGear], currentVelocity)
130.
131.     else:
132.         determineEngingeBraking(currentOmega) # The engine braking depend on the rpm
133.         var brakeForce = -Teb / wheelRadius # Like the torque forward of the engine but opposite :)
134.         determineDrag(currentVelocity) # Drag affects the braking as well and needs to be updated based on currentVelocity
135.         var engineBraking = (brakeForce - Fr - Fd) / mass # acceleration of engine braking
136.         if (currentVelocity > 0):
137.             currentVelocity = currentVelocity + engineBraking * delta # Enginebraking
138.             determineOmegaE(currentVelocity, gearRatios[currentGear])
139.             determineAcceleration(gearRatios[currentGear], currentVelocity)
140.         elif (currentVelocity < 0):
141.             currentVelocity = currentVelocity - engineBraking * delta # Enginebraking
142.             determineOmegaE(currentVelocity, gearRatios[currentGear])
143.             determineAcceleration(gearRatios[currentGear], currentVelocity)
144.         print("engineBraking: ", engineBraking)
145.
146.
147.
148. # Old variables:
149. var wheelDist = 70
150. var angle = 15
151. var power = 800
152. var friction = -0.9
153. var drag = -0.001
154. var breaking = - 450
155. var speedReverse = 250
156.
157. var acceleration = Vector2.ZERO
158. var velocity = Vector2.ZERO
159. var steerAngle
160.
161. func _physics_process(delta):
162.     acceleration = Vector2.ZERO
163.     get_input(delta)
164.     calculate_steering(delta)
165.
166.     print("v = ", currentVelocity, " acc = ", currentAcceleration, " rpm = ", currentOmega, " gear = ", currentGear + 1)
167.     velocity = move_and_slide(12 * currentVelocity * transform.x) # * 12 for more realistic movement in the scale of the sprites
168.
169. func get_input(delta): #FIX
170.     #Turn or not turning
171.     var turn = 0
172.     if Input.is_action_pressed("ui_right"):
173.         turn += 1
174.     if Input.is_action_pressed("ui_left"):
175.         turn -= 1
176.     steerAngle = turn * deg2rad(angle)
177.
178.     #Accalerations forward and for breaking
179.     if Input.is_action_pressed("ui_up"):
180.         if (currentOmega == 0): # start of engine
181.             currentOmega = 1000
182.         determineGas(delta, "ui_up")
183.     elif Input.is_action_pressed("ui_down"):
184.         determineGas(delta, "ui_down")
185.     else:
186.         determineGas(delta, "")
187.
188.     # Gear up or down
189.     if Input.is_action_just_pressed("ui_select"):
190.         if (currentGear < 5):
191.             rpmAfterShift(gearRatios[currentGear], gearRatios[currentGear + 1])
192.             determineTopSpeedRedline(gearRatios[currentGear])
193.     if Input.is_action_just_pressed("ui_cancel"):
194.         if (currentGear > 0):
195.             rpmAfterShift(gearRatios[currentGear], gearRatios[currentGear - 1])
196.             currentGear = currentGear - 1
197.             determineTopSpeedRedline(gearRatios[currentGear])
198.
199.     #Animations
200.     if velocity.length() > 0:
201.         \$AnimatedSprite.play("Forward")
202.     if velocity.length() < 0:
203.         \$AnimatedSprite.play("Backwards")
204.     if velocity.length() == 0:
205.         \$AnimatedSprite.stop()
206.     if turn > 0:
207.         \$AnimatedSprite.play("Turn")
208.         \$AnimatedSprite.flip_h = false
209.     if turn < 0:
210.         \$AnimatedSprite.play("Turn")
211.         \$AnimatedSprite.flip_h = true
212.
213.
214. func calculate_steering(delta):
215.     # Location of front- & rear wheel
216.     var rearWheel = position - transform.x * wheelDist / 2.0
217.     var frontWheel = position + transform.x * wheelDist / 2.0
218.     rearWheel += velocity * delta
219.     frontWheel += velocity.rotated(steerAngle) * delta
220.
221.     # Calculating our new velocity
222.     var newVelocity = (frontWheel - rearWheel).normalized()
223.     var d = newVelocity.dot(velocity.normalized())
224.     if d > 0:
225.         velocity = newVelocity * velocity.length()
226.     if d < 0:
227.         velocity = -newVelocity *min(velocity.length(), speedReverse)
228.     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.

Top