diff options
| author | realtradam <[email protected]> | 2020-03-14 17:47:27 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2020-03-14 17:47:27 -0400 |
| commit | ace8100600de99aed17c5de718b582749f126168 (patch) | |
| tree | 28caece492919039b26167bf61026618dc17688c /Racing-Game/4WheelCar | |
| parent | f435ce10e93d7798ad4fe43be33cd01a4edd3dfb (diff) | |
| download | optimal-direction-ace8100600de99aed17c5de718b582749f126168.tar.gz optimal-direction-ace8100600de99aed17c5de718b582749f126168.zip | |
Some file refactoring was completed.
Diffstat (limited to 'Racing-Game/4WheelCar')
| -rw-r--r-- | Racing-Game/4WheelCar/4WheelCar.tscn | 6 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/BackWheel.gd | 121 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/CarBody.gd | 61 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/FrontWheel.gd | 157 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/SkidController.gd | 2 |
5 files changed, 343 insertions, 4 deletions
diff --git a/Racing-Game/4WheelCar/4WheelCar.tscn b/Racing-Game/4WheelCar/4WheelCar.tscn index 242e6d5..84b4549 100644 --- a/Racing-Game/4WheelCar/4WheelCar.tscn +++ b/Racing-Game/4WheelCar/4WheelCar.tscn @@ -1,10 +1,10 @@ [gd_scene load_steps=10 format=2] -[ext_resource path="res://CarBody.gd" type="Script" id=1] +[ext_resource path="res://4WheelCar/CarBody.gd" type="Script" id=1] [ext_resource path="res://Art(non-orig)/PNG/Cars/big/blue/car_blue_2.png" type="Texture" id=2] -[ext_resource path="res://FrontWheel.gd" type="Script" id=3] +[ext_resource path="res://4WheelCar/FrontWheel.gd" type="Script" id=3] [ext_resource path="res://4WheelCar/SkidController.gd" type="Script" id=4] -[ext_resource path="res://BackWheel.gd" type="Script" id=5] +[ext_resource path="res://4WheelCar/BackWheel.gd" type="Script" id=5] [sub_resource type="RectangleShape2D" id=1] diff --git a/Racing-Game/4WheelCar/BackWheel.gd b/Racing-Game/4WheelCar/BackWheel.gd new file mode 100644 index 0000000..b5d8297 --- /dev/null +++ b/Racing-Game/4WheelCar/BackWheel.gd @@ -0,0 +1,121 @@ +extends RigidBody2D + +#Accelerating +var hp = 4.0 +var acceleration = 5 +var isForward = true + +var velocity + +var lastLoc = 0; + +var wheelSlip = Vector2(0,0); +var isSkid = false; + +signal slip +signal end +var elapsed = 0 +# +var gripDelay = 0 +var carAngle +var carVector +var velVector + +# Called when the node enters the scene tree for the first time. +func _ready(): + lastLoc = get_position_in_parent() + pass # Replace with function body. + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + carAngle = get_node("../../../CarBody").get_transform().get_rotation() + carVector = Vector2(cos(carAngle + PI/2),sin(carAngle + PI/2)) + velVector = get_node("../../../CarBody").get_linear_velocity() + velocity = measure_velocity() + isForward = is_forward() + set_rotation(carAngle) + isSlip(delta) + +# gripDelay = has_grip(0.4,delta) + + isSkid = Input.is_action_pressed("grip") + + if Input.is_action_pressed("grip") || Input.is_action_pressed("break"): + null_slide(max(5,velocity/7),delta) + else: + null_slide(1,delta) + #Braking + if Input.is_action_pressed("break"): + if velocity > 20: + linear_damp = 2 + else: + linear_damp = 5 + else: + linear_damp = 0.01 + +# createLine(lastLoc, get_position_in_parent()) +# lastLoc = get_position_in_parent() + + if Input.is_action_pressed("forward"): + if !Input.is_action_pressed("break"): + apply_central_impulse(Vector2(0,-gear(velocity, hp, acceleration)).rotated(carAngle)*delta*5000) + else: + pass + elif Input.is_action_pressed("backward"): + if !Input.is_action_pressed("break"): + apply_central_impulse(Vector2(0,(1)).rotated(carAngle)*delta*5000) + else: + 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 = carAngle + (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) + +func isSlip(time): + if (wheelSlip.length() > 0.2): + emit_signal("slip") + else: + emit_signal("end") + +#func createLine(from, to): +# var tires = get_node("./MyLine") +# tires.add_point(from) +# tires.add_point(to) + +#func has_grip(var tractionDelay, var delta): +# var movementUnitVector = get_linear_velocity().normalized()#the direction of the velocity +# var directionAngle = carAngle#the angle the car is facing(relative to the world) +# var directionUnitVector = Vector2(cos(directionAngle),sin(directionAngle)).normalized()#the direction the car is facing +# if velocity > 10: +# if Input.is_action_pressed("grip"):##if pressing shift +# gripDelay = tractionDelay +# elif (gripDelay <= 0 && abs(movementUnitVector.dot(directionUnitVector)) > 0.4):#if not drifting but past steering point +# gripDelay = tractionDelay +# elif gripDelay > 0 && abs(movementUnitVector.dot(directionUnitVector)) > 0.3:#if drifting and past steering point +# gripDelay = tractionDelay +# elif gripDelay > 0:#if at recovery point and drifting +# gripDelay -= delta +# else: +# gripDelay = 0 +# return gripDelay + +func measure_velocity(): + return sqrt(get_linear_velocity().dot(get_linear_velocity()))/12 + +func is_forward():#determines if the car is driving forward, or backward + 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 + +func gear(var rpm, var maxPower, var topSpeed): + return max(-pow((rpm/((1000/topSpeed)/sqrt(maxPower)))-sqrt(maxPower),2)+(maxPower+1),0) diff --git a/Racing-Game/4WheelCar/CarBody.gd b/Racing-Game/4WheelCar/CarBody.gd new file mode 100644 index 0000000..179381c --- /dev/null +++ b/Racing-Game/4WheelCar/CarBody.gd @@ -0,0 +1,61 @@ +extends RigidBody2D + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" +# Called when the node enters the scene tree for the first time. +var angular = 0; +var directionAngle +var directionUnitVector +var cameraNode +var speedometerNode +var isSkidding = 0 +var frWheel +var flWheel +var brWheel +var blWheel + +func _ready(): + frWheel = get_node("./Engine/Wheels/FRWheel") + flWheel = get_node("./Engine/Wheels/FLWheel") + brWheel = get_node("./Engine/Wheels/BRWheel") + blWheel = get_node("./Engine/Wheels/BLWheel") +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + directionAngle = get_transform().get_rotation() + (PI/2.0)#the angle the car is facing(relative to the world) + directionUnitVector = Vector2(cos(directionAngle),sin(directionAngle))#the direction the car is facing + linear_damp = 0.01 + angular = 5 + + if(isSkidding > 0): + frWheel.isSkidOverride = true + flWheel.isSkidOverride = true + brWheel.isSkidOverride = true + blWheel.isSkidOverride = true + frWheel.isSkid = true + flWheel.isSkid = true + brWheel.isSkid = true + blWheel.isSkid = true + #emit_signal("speedometer", measure_forward_velocity()) + cameraNode = get_node("/root/World/Camera2D") + speedometerNode = get_node("/root/World/Camera2D/Panel/Speedometer") + cameraNode.set_position(get_global_transform().get_origin()) + speedometerNode.updateSpeed(floor(measure_velocity())) + + if measure_velocity() < 30: + angular_damp = angular*1 + elif measure_velocity() < 85: + angular_damp = angular*0.4 + else: + angular_damp = angular*0.05 + + + +func measure_velocity(): + return sqrt(get_linear_velocity().dot(get_linear_velocity()))/12 + +func measure_forward_velocity(): + return floor(measure_velocity() * cos(directionUnitVector.angle_to(get_linear_velocity()))) + +func forceSkidMarks(skid): + isSkidding += skid diff --git a/Racing-Game/4WheelCar/FrontWheel.gd b/Racing-Game/4WheelCar/FrontWheel.gd new file mode 100644 index 0000000..2333113 --- /dev/null +++ b/Racing-Game/4WheelCar/FrontWheel.gd @@ -0,0 +1,157 @@ +extends RigidBody2D + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" +var velocity +var velVector +var velUnitVector +var velAngle +var carAngle +var delay = 0 + +onready var skidObj = preload("res://Skidmark.tscn") + +var steerDamp = 1 + +#Steering +var steerSplitA = 20 +var steerSplitB = 40 +var steerHeight = 2.6 +var steerLimit = 73 +var steerMinimum = 1 + +var gripDelay = 0 + +var wheelSlip = Vector2(0,0) +var isSkid = false;#this one is used when user presses shift. Initially called in this function +var isSkidOverride = false;#this one is used when driving over sand, initially called in carbody + +signal slip +signal end +var elapsed = 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): + #Variable Setup + velocity = measure_velocity() + velVector = get_node("../../../CarBody").get_linear_velocity() + velUnitVector = velVector.normalized() + velAngle = atan2(velVector.y,velVector.x) + carAngle = get_node("../../../CarBody").get_transform().get_rotation() + set_rotation(carAngle) + isForward = is_forward() +# gripDelay = has_grip(0.4,delta) + isSlip(delta) + + if(!isSkidOverride): + isSkid = Input.is_action_pressed("grip") + + #Determines if drifting + if Input.is_action_pressed("grip") || Input.is_action_pressed("break"): + null_slide(max(5,velocity/7),delta) + else: + null_slide(1,delta) + + #Braking + if Input.is_action_pressed("break"): + if velocity > 20: + linear_damp = 3 + steerDamp = 0.7 + else: + linear_damp = 6 + steerDamp = 0.4 + else: + linear_damp = 0.01 + steerDamp = 1 + + #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) + + + + +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 = carAngle + (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) + +#func has_grip(var tractionDelay, var delta): +# var movementUnitVector = get_linear_velocity().normalized()#the direction of the velocity +# var directionAngle = carAngle#the angle the car is facing(relative to the world) +# var directionUnitVector = Vector2(cos(directionAngle),sin(directionAngle)).normalized()#the direction the car is facing +# if velocity > 10: +# if Input.is_action_pressed("grip"):##if pressing shift +# gripDelay = tractionDelay +# elif (gripDelay <= 0 && abs(movementUnitVector.dot(directionUnitVector)) > 0.4):#if not drifting but past steering point +# gripDelay = tractionDelay +# elif gripDelay > 0 && abs(movementUnitVector.dot(directionUnitVector)) > 0.3:#if drifting and past steering point +# gripDelay = tractionDelay +# elif gripDelay > 0:#if at recovery point and drifting +# gripDelay -= delta +# else: +# gripDelay = 0 +# return gripDelay + +func isSlip(time): + if (wheelSlip.length() > 0.6): + if(elapsed/4 > time): + emit_signal("end") + elapsed = 0 + else: + emit_signal("slip") + else: + elapsed += time + +func measure_velocity(): + return floor(sqrt(get_linear_velocity().dot(get_linear_velocity()))/12) + +func is_forward():#determines if the car is driving forward, or backward + 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 + +func steer_angle(): + #returns the angle the car is facing, relative to the direcction it is moving + if isForward: + return carAngle + (PI/2.0) + else: + return carAngle - (PI/2.0) + +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) diff --git a/Racing-Game/4WheelCar/SkidController.gd b/Racing-Game/4WheelCar/SkidController.gd index e7dad54..105eb3b 100644 --- a/Racing-Game/4WheelCar/SkidController.gd +++ b/Racing-Game/4WheelCar/SkidController.gd @@ -35,4 +35,4 @@ func _process(delta): skidRecent.skidDraw = false skidSwitch = false pass -
\ No newline at end of file + |
