diff options
Diffstat (limited to 'Racing-Game/4WheelCar/Wheels')
| -rw-r--r-- | Racing-Game/4WheelCar/Wheels/BackWheel.gd | 50 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Wheels/BackWheel.tscn | 29 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Wheels/FrontWheel.gd | 84 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Wheels/FrontWheel.tscn | 29 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Wheels/Skid/SkidController.gd | 36 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Wheels/Skid/Skidmark.tscn | 12 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Wheels/Skid/Skidmarks.gd | 15 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Wheels/Wheel.gd | 63 | ||||
| -rw-r--r-- | Racing-Game/4WheelCar/Wheels/WheelCreation.gd | 35 |
9 files changed, 353 insertions, 0 deletions
diff --git a/Racing-Game/4WheelCar/Wheels/BackWheel.gd b/Racing-Game/4WheelCar/Wheels/BackWheel.gd new file mode 100644 index 0000000..88df113 --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/BackWheel.gd @@ -0,0 +1,50 @@ +extends "res://4WheelCar/Wheels/Wheel.gd" + +#Accelerating +var hp = 4.0 +var acceleration = 5 + +var gripDelay = 0 + +func _ready(): + pass + +func _process(delta): + #Variable Setup + #---null_slide vars + nullStrength = 0 + carAngle = get_node("../../../CarBody").get_transform().get_rotation() + velVector = get_node("../../../CarBody").get_linear_velocity() + velocity = measure_velocity() + #--- + set_rotation(carAngle) + + isSkid = Input.is_action_pressed("grip") + + 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) + + + if Input.is_action_pressed("forward"): + if !Input.is_action_pressed("brake"): + 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("brake"): + apply_central_impulse(Vector2(0,(1)).rotated(carAngle)*delta*5000) + else: + pass + +func measure_velocity(): + return sqrt(get_linear_velocity().dot(get_linear_velocity()))/12 + + +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/Wheels/BackWheel.tscn b/Racing-Game/4WheelCar/Wheels/BackWheel.tscn new file mode 100644 index 0000000..c09eaca --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/BackWheel.tscn @@ -0,0 +1,29 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://4WheelCar/Wheels/Skid/SkidController.gd" type="Script" id=1] +[ext_resource path="res://4WheelCar/Wheels/BackWheel.gd" type="Script" id=2] +[ext_resource path="res://Art(non-orig)/crosshair(isorig).png" type="Texture" id=3] + + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 7.07375, 14.3787 ) + +[node name="BackWheel" type="RigidBody2D"] +z_index = 2 +mass = 15.0 +linear_damp = 0.4 +angular_damp = 7.04 +script = ExtResource( 2 ) +__meta__ = { +"_edit_group_": true +} + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) +disabled = true + +[node name="SkidController2" type="Node" parent="."] +script = ExtResource( 1 ) + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 3 ) diff --git a/Racing-Game/4WheelCar/Wheels/FrontWheel.gd b/Racing-Game/4WheelCar/Wheels/FrontWheel.gd new file mode 100644 index 0000000..7a05522 --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/FrontWheel.gd @@ -0,0 +1,84 @@ +extends "res://4WheelCar/Wheels/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 diff --git a/Racing-Game/4WheelCar/Wheels/FrontWheel.tscn b/Racing-Game/4WheelCar/Wheels/FrontWheel.tscn new file mode 100644 index 0000000..25644a6 --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/FrontWheel.tscn @@ -0,0 +1,29 @@ +[gd_scene load_steps=5 format=2] + +[ext_resource path="res://4WheelCar/Wheels/Skid/SkidController.gd" type="Script" id=1] +[ext_resource path="res://4WheelCar/Wheels/FrontWheel.gd" type="Script" id=2] +[ext_resource path="res://Art(non-orig)/crosshair(isorig).png" type="Texture" id=3] + + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 6.93714, 14.1584 ) + +[node name="FrontWheel" type="RigidBody2D"] +z_index = 2 +mass = 15.0 +linear_damp = 0.4 +angular_damp = 7.04 +script = ExtResource( 2 ) +__meta__ = { +"_edit_group_": true +} + +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) +disabled = true + +[node name="SkidController4" type="Node" parent="."] +script = ExtResource( 1 ) + +[node name="Sprite" type="Sprite" parent="."] +texture = ExtResource( 3 ) diff --git a/Racing-Game/4WheelCar/Wheels/Skid/SkidController.gd b/Racing-Game/4WheelCar/Wheels/Skid/SkidController.gd new file mode 100644 index 0000000..df1e054 --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/Skid/SkidController.gd @@ -0,0 +1,36 @@ +extends Node + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" +var skidSwitch = false # Is the wheel currantly skidding? +var skidFile # Stores the skid file +var skidRecent # Holds the most recent skidmark + +var deleteme = 0; + +# Called when the node enters the scene tree for the first time. +func _ready(): + skidFile = load("res://4WheelCar/Wheels/Skid/Skidmark.tscn") + skidRecent = skidFile.instance() + add_child(skidRecent) + +# 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/Wheels/Skid/Skidmark.tscn") + skidRecent = skidFile.instance() + add_child(skidRecent) + #set to run + pass + elif(!get_parent().isSkid): + #stop child from skidding and store + skidRecent.skidDraw = false + skidSwitch = false + pass + diff --git a/Racing-Game/4WheelCar/Wheels/Skid/Skidmark.tscn b/Racing-Game/4WheelCar/Wheels/Skid/Skidmark.tscn new file mode 100644 index 0000000..865add0 --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/Skid/Skidmark.tscn @@ -0,0 +1,12 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://4WheelCar/Wheels/Skid/Skidmarks.gd" type="Script" id=1] +[ext_resource path="res://Art(non-orig)/PNG/Objects/skidmark_short_1.png" type="Texture" id=2] + + +[node name="Skidmark" type="Line2D"] +position = Vector2( -21.2326, -38.4176 ) +default_color = Color( 0.4, 0.501961, 1, 1 ) +texture = ExtResource( 2 ) +texture_mode = 192263440 +script = ExtResource( 1 ) diff --git a/Racing-Game/4WheelCar/Wheels/Skid/Skidmarks.gd b/Racing-Game/4WheelCar/Wheels/Skid/Skidmarks.gd new file mode 100644 index 0000000..07bf622 --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/Skid/Skidmarks.gd @@ -0,0 +1,15 @@ +extends Line2D + +var skidDraw = true +var wheel +var carBody + +func _ready(): + wheel = get_parent().get_parent() + carBody = get_parent().get_parent().get_parent().get_parent() + +func _process(_delta): + global_position = Vector2(0,0) + global_rotation = 0 + if(skidDraw): + add_point(wheel.get_global_position()) diff --git a/Racing-Game/4WheelCar/Wheels/Wheel.gd b/Racing-Game/4WheelCar/Wheels/Wheel.gd new file mode 100644 index 0000000..49024fb --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/Wheel.gd @@ -0,0 +1,63 @@ +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 initVars(dataArray): +# #---Creating and attaching pinjoint to wheel + var tempA = PinJoint2D.new() + tempA.set_position(Vector2(dataArray[0],dataArray[1])) + 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 = 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/4WheelCar/Wheels/WheelCreation.gd b/Racing-Game/4WheelCar/Wheels/WheelCreation.gd new file mode 100644 index 0000000..db09f4b --- /dev/null +++ b/Racing-Game/4WheelCar/Wheels/WheelCreation.gd @@ -0,0 +1,35 @@ +extends Node + + +# This script creates and attaches all the wheels. Depending on the variables +# given to this script, it can generate different type of vehicles + + +# Called when the node enters the scene tree for the first time. +func _ready(): + createWheel("front", [17,-27]) + createWheel("front", [-17,-27]) + createWheel("back", [20,36]) + createWheel("back", [-20,36]) + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +#func _process(delta): +# pass + +func createWheel(type,dataArray): + if(type == "front"): + var wheeltemp_resource = load("res://4WheelCar/Wheels/FrontWheel.tscn") + var wheeltemp = wheeltemp_resource.instance() + wheeltemp.set_position(Vector2(dataArray[0],dataArray[1])) + add_child(wheeltemp) + wheeltemp.initVars(dataArray) + +# add_child(tempA) + elif(type == "back"): + var wheeltemp_resource = load("res://4WheelCar/Wheels/BackWheel.tscn") + var wheeltemp = wheeltemp_resource.instance() + wheeltemp.set_position(Vector2(dataArray[0],dataArray[1])) + add_child(wheeltemp) + wheeltemp.initVars(dataArray) |
