summaryrefslogtreecommitdiffhomepage
path: root/Racing-Game/4WheelCar/Wheels/Wheel.gd
blob: ab00de602f70c139ffe3ab90ad6384b090111363 (plain)
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
extends RigidBody2D

var velocity #How fast car is moving
var velVector #What direction the car is moving
var velUnitVector #Direction vector, but in a single unit(no magnitude)
var velAngle #The angle of the velocity(relative to world)
var carAngle #The angle the car is facing(relative to world)
var wheelAngle #the angle the wheel is facing(relative to world)
var nullStrength #sums up all sources of null to here
var isSkid = false #this one is used when user presses shift. Initially called in this function

#Skidmarks on floor

#var gripDelay = 0

var wheelSlip = Vector2(0,0)

var isForward = true

# 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):
	pass

func initVars(locX,locY):
#	#---Creating and attaching pinjoint to wheel
	var tempA = PinJoint2D.new()
	tempA.set_position(Vector2(locX,locY))
	tempA.set_node_a(get_path())
	tempA.set_node_b(get_parent().get_parent().get_path())
	tempA.softness = 0
	tempA.bias = 0
	tempA.disable_collision = true
	get_parent().add_child(tempA)
#	#---
	pass

func null_slide(var strength, var delta):
	#strength is how strong you would like the nullify to be
	#higher is less sliding/drifting
	var movementUnitVector = get_linear_velocity().normalized()#the direction of the velocity
	var directionAngle = wheelAngle + (PI/2.0)#the angle the car is facing(relative to the world)
	var directionUnitVector = Vector2(cos(directionAngle),sin(directionAngle)).normalized()#the direction the car is facing
	var nullify = directionUnitVector * movementUnitVector.dot(directionUnitVector)
	wheelSlip = (-(movementUnitVector - nullify))*strength
	apply_central_impulse(wheelSlip*delta*5000)

#checks if the car is braking, and applies brake physics
func setBrake(var strength):
	#Braking
	if Input.is_action_pressed("brake"):
		if velocity > 20:
			linear_damp = 3
		else:
			linear_damp = 6
	else:
		linear_damp = 0.01


func measure_velocity():
		return floor(sqrt(get_linear_velocity().dot(get_linear_velocity()))/12)