1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
extends "res://4WheelCar/Wheel.gd"
#Reduces steering strength when braking
var steerDamp = 1
#Steering Curve Vars
var steerSplitA = 20
var steerSplitB = 40
var steerHeight = 2.6
var steerLimit = 73
var steerMinimum = 1
# Called when the node enters the scene tree for the first time.
func _ready():
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
#Variable Update
#---null_slide vars
nullStrength = 0
velocity = measure_velocity()
velVector = get_node("../../../CarBody").get_linear_velocity()
carAngle = get_node("../../../CarBody").get_transform().get_rotation()
#---Steering Vars
velAngle = atan2(velVector.y,velVector.x)
isForward = is_forward()
#---
set_rotation(carAngle)
isSkid = Input.is_action_pressed("grip")
#Determines if drifting
if Input.is_action_pressed("grip") || Input.is_action_pressed("brake"):
nullStrength += max(5,velocity/7)
else:
nullStrength += 1
#Braking
setBrake(0)
null_slide(nullStrength,delta)
#Steering
if Input.is_action_pressed("steer_left"):
apply_central_impulse(steerDamp*Vector2(0,steer_curve(steerSplitA, steerSplitB, steerHeight, steerLimit,steerMinimum)).rotated(steer_angle())*delta*5000)
if Input.is_action_pressed("steer_right"):
apply_central_impulse(steerDamp*Vector2(0,-steer_curve(steerSplitA, steerSplitB, steerHeight, steerLimit,steerMinimum)).rotated(steer_angle())*delta*5000)
#returns the angle the car is facing, relative to the direction it is moving
func steer_angle():
if isForward:
return carAngle + (PI/2.0)
else:
return carAngle - (PI/2.0)
#Determines strength of steering as a function of the speed
func steer_curve(var splitA, splitB, var height, var limit, var minimum):
#Rules:
# splitA < splitB < limit
# height > 0, limit >= 0
# ---
#Desmos: SteerCurve
#Link: https://www.desmos.com/calculator/jkrd8zzoj9
# splitA = a
# splitB = b
# height = h
# limit = f
#note: minimum is not in the graph, it is simply the minimum y value you want when x > splitB
# ---
if(velocity >= splitB):
return max((-pow((velocity-splitB)/((limit-splitB)/sqrt(height)),2)+height)*abs(cos(abs(velAngle-carAngle)+PI/2)),minimum)
elif velocity >= splitA:
return height
else:
return max((-pow((velocity/(splitA/sqrt(height)))-sqrt(height),2)+height)*abs(cos(abs(velAngle-carAngle)+PI/2)),0)
func is_forward():
var carVector = Vector2(cos(carAngle + PI/2),sin(carAngle + PI/2))
if velVector == Vector2(0,0) || carVector == Vector2(0,0):
return true
if velVector.dot(carVector) <= 0:
return true
else:
return false
|