diff options
| author | realtradam <[email protected]> | 2020-03-18 00:58:15 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2020-03-18 00:58:15 -0400 |
| commit | 0fe77a326aec5732920dd3b23b36fd3405ff9e25 (patch) | |
| tree | 2494e1e6c6693c46dab5b2b1f90abc6720d85793 /Racing-Game | |
| parent | 5a33ed9381e43b50d56f2ef7f149baef83edcf7d (diff) | |
| download | optimal-direction-0fe77a326aec5732920dd3b23b36fd3405ff9e25.tar.gz optimal-direction-0fe77a326aec5732920dd3b23b36fd3405ff9e25.zip | |
Removed a lot of useless or commented out code
Diffstat (limited to 'Racing-Game')
| -rw-r--r-- | Racing-Game/4WheelCar/BackWheel.gd | 73 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/CarBody.gd | 12 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/FrontWheel.gd | 119 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Skid/SkidController.gd | 2 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Wheel.gd | 54 | ||||
| -rw-r--r-- | Racing-Game/World.tscn | 10 |
6 files changed, 92 insertions, 178 deletions
diff --git a/Racing-Game/4WheelCar/BackWheel.gd b/Racing-Game/4WheelCar/BackWheel.gd index 9c9adff..1d6ee18 100644 --- a/Racing-Game/4WheelCar/BackWheel.gd +++ b/Racing-Game/4WheelCar/BackWheel.gd @@ -1,48 +1,34 @@ -extends RigidBody2D +extends "res://4WheelCar/Wheel.gd" #Accelerating var hp = 4.0 var acceleration = 5 -var isForward = true - -var velocity - -var isSkid = false; - -var elapsed = 0 var gripDelay = 0 -var carAngle -var carVector -var velVector func _ready(): pass func _process(delta): + #Variable Setup + #---null_slide vars + nullStrength = 0 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) - -# gripDelay = has_grip(0.4,delta) isSkid = Input.is_action_pressed("grip") if Input.is_action_pressed("grip") || Input.is_action_pressed("brake"): - null_slide(max(5,velocity/7),delta) + nullStrength += max(5,velocity/7) else: - null_slide(1,delta) + nullStrength += 1 + #Braking - if Input.is_action_pressed("brake"): - if velocity > 20: - linear_damp = 2 - else: - linear_damp = 5 - else: - linear_damp = 0.01 + setBrake(0) + null_slide(nullStrength, delta) if Input.is_action_pressed("forward"): @@ -56,46 +42,9 @@ func _process(delta): else: pass -#Core physics, stops sliding sideways and make car roll forwards -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) - var 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 measure_velocity(): return sqrt(get_linear_velocity().dot(get_linear_velocity()))/12 - -#determines if the car is driving forward, or backward -func is_forward(): - 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 index be06de1..7a77fd4 100644 --- a/Racing-Game/4WheelCar/CarBody.gd +++ b/Racing-Game/4WheelCar/CarBody.gd @@ -1,6 +1,5 @@ extends RigidBody2D -var angular = 0 var isSkidding = 0 var cameraNode var speedometerNode @@ -9,28 +8,19 @@ var directionAngle var directionUnitVector func _ready(): + angular_damp = 5 pass 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 #Sets forward momentum dampening - angular = 5 #Sets the base angular momentum dampening #HUD Stuff: 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())) - - #Steering strength at different speeds, should probably look at this and remove it(move to wheels) - #See issue #8 - if measure_velocity() < 30: - angular_damp = angular*0.2 - elif measure_velocity() < 85: - angular_damp = angular*0.4 - else: - angular_damp = angular*1 #Total speed of the car func measure_velocity(): diff --git a/Racing-Game/4WheelCar/FrontWheel.gd b/Racing-Game/4WheelCar/FrontWheel.gd index 76eaad0..7731bca 100644 --- a/Racing-Game/4WheelCar/FrontWheel.gd +++ b/Racing-Game/4WheelCar/FrontWheel.gd @@ -1,16 +1,6 @@ -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 to car is facing(relative to world) - -#Skidmarks on floor -onready var skidObj = preload("res://4WheelCar/Skid/Skidmark.tscn") +extends "res://4WheelCar/Wheel.gd" #Reduces steering strength when braking -var steerDampBase = 1 var steerDamp = 1 #Steering Curve Vars @@ -20,57 +10,35 @@ 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 - #--- + #Variable Update + #---null_slide vars + nullStrength = 0 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() + #---Steering Vars + velAngle = atan2(velVector.y,velVector.x) isForward = is_forward() #--- set_rotation(carAngle) -# gripDelay = has_grip(0.4,delta) - isSlip(delta) - if(!isSkidOverride): - isSkid = Input.is_action_pressed("grip") + isSkid = Input.is_action_pressed("grip") #Determines if drifting if Input.is_action_pressed("grip") || Input.is_action_pressed("brake"): - null_slide(max(5,velocity/7),delta) + nullStrength += max(5,velocity/7) else: - null_slide(1,delta) + nullStrength += 1 #Braking - if Input.is_action_pressed("brake"): - if velocity > 20: - linear_damp = 3 - steerDamp = 0.7 - else: - linear_damp = 6 - steerDamp = 0.4 - else: - linear_damp = 0.01 - steerDamp = 1 + setBrake(0) + null_slide(nullStrength,delta) #Steering if Input.is_action_pressed("steer_left"): @@ -78,60 +46,6 @@ func _process(delta): 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 - -#Determines if skidmarks should be creted, or stopped -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) - -#determines if the car is driving forward, or backward -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 - #returns the angle the car is facing, relative to the direction it is moving func steer_angle(): if isForward: @@ -141,7 +55,7 @@ func steer_angle(): #Determines strength of steering as a function of the speed func steer_curve(var splitA, splitB, var height, var limit, var minimum): - #Rules: + #Rules: # splitA < splitB < limit # height > 0, limit >= 0 # --- @@ -159,3 +73,12 @@ func steer_curve(var splitA, splitB, var height, var limit, var minimum): 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 diff --git a/Racing-Game/4WheelCar/Skid/SkidController.gd b/Racing-Game/4WheelCar/Skid/SkidController.gd index 7c633c9..e3b28c3 100644 --- a/Racing-Game/4WheelCar/Skid/SkidController.gd +++ b/Racing-Game/4WheelCar/Skid/SkidController.gd @@ -17,13 +17,11 @@ func _ready(): # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(_delta): - if(skidSwitch && get_parent().isSkid):#if controller believes car is skidding and the wheel is skidding #ignore the status and let the skid keep drawing pass elif(!skidSwitch && get_parent().isSkid):#controller doesnt skid, but wheel is supposed to be skidding # create new skidding child, and set skid to true - skidSwitch = true skidFile = load("res://4WheelCar/Skid/Skidmark.tscn") skidRecent = skidFile.instance() diff --git a/Racing-Game/4WheelCar/Wheel.gd b/Racing-Game/4WheelCar/Wheel.gd new file mode 100644 index 0000000..7d36cd8 --- /dev/null +++ b/Racing-Game/4WheelCar/Wheel.gd @@ -0,0 +1,54 @@ +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 to car 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 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) + +#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) diff --git a/Racing-Game/World.tscn b/Racing-Game/World.tscn index 34bc2c8..8b06088 100644 --- a/Racing-Game/World.tscn +++ b/Racing-Game/World.tscn @@ -52,7 +52,7 @@ radius = 28.3155 size = 200 font_data = ExtResource( 17 ) -[sub_resource type="RectangleShape2D" id=12] +[sub_resource type="RectangleShape2D" id=8] extents = Vector2( 795.282, 251.806 ) [node name="World" type="Node"] @@ -74,7 +74,7 @@ texture = ExtResource( 1 ) [node name="Wall27" type="StaticBody2D" parent=" Obstacles/SolidOuterWalls"] modulate = Color( 0, 0, 0, 1 ) -position = Vector2( -1984, 362 ) +position = Vector2( -1961.79, 360.149 ) __meta__ = { "_edit_group_": true } @@ -93,7 +93,7 @@ __meta__ = { [node name="Wall28" type="StaticBody2D" parent=" Obstacles/SolidOuterWalls"] modulate = Color( 0, 0, 0, 1 ) -position = Vector2( 3168, 677 ) +position = Vector2( 3169.67, 393.07 ) __meta__ = { "_edit_group_": true } @@ -131,7 +131,7 @@ __meta__ = { [node name="Wall30" type="StaticBody2D" parent=" Obstacles/SolidOuterWalls"] modulate = Color( 0, 0, 0, 1 ) -position = Vector2( 544, 2848 ) +position = Vector2( 544, 2802.58 ) __meta__ = { "_edit_group_": true } @@ -3907,4 +3907,4 @@ script = ExtResource( 25 ) [node name="CollisionShape2D" type="CollisionShape2D" parent="Area2D"] position = Vector2( -627.433, 382.855 ) -shape = SubResource( 12 ) +shape = SubResource( 8 ) |
