Browse Source

Vehicle has most functions added to it for drivability

master
Bryan Miller 3 years ago
parent
commit
f0cb3a911c
No known key found for this signature in database
3 changed files with 210 additions and 177 deletions
  1. +3
    -3
      Objects/PlayerCTRL/PlayerCTRL.gd
  2. +48
    -15
      Objects/Vehicle/Vehicle.gd
  3. +159
    -159
      World.tscn

+ 3
- 3
Objects/PlayerCTRL/PlayerCTRL.gd View File

@@ -1,9 +1,9 @@
class_name PlayerCTRL
extends Node

const MAX_THROTTLE_TIME = 0.25
const MAX_THROTTLE_TIME = 0.1
const MAX_BREAKING_TIME = 0.25
const MAX_STEERING_TIME = 0.25
const MAX_STEERING_TIME = 0.1


var throttle = 0.0
@@ -112,7 +112,7 @@ func _unhandled_input(event):
func _process(delta):
if parent:
parent.set_throttle(throttle)
#parent.breaking(breaking)
parent.set_break(breaking)
parent.set_steering(steering)



+ 48
- 15
Objects/Vehicle/Vehicle.gd View File

@@ -8,6 +8,10 @@ class_name Vehicle
export var camera_enabled : bool = false setget _set_camera_enabled
export var forward_axel : float = 10 setget _set_forward_axel
export var rear_axel : float = 10 setget _set_rear_axel
export (float, 0.0, 1.0, 0.001) var traction_fast = 0.1
export (float, 0.0, 1.0, 0.001) var traction_slow = 0.7
export var traction_slow_speed : float = 100.0 setget _set_traction_slow_speed
export var traction_fast_speed : float = 400.0 setget _set_traction_fast_speed
export (float, 0.1, 360.0, 0.1) var max_steering_angle = 15 setget _set_max_steering_angle
export var max_engine_power : float = 800 setget _set_max_engine_power
export (float, 0.0, 1.0, 0.001) var reverse_engine_multiplier = 0.85
@@ -21,7 +25,7 @@ var acceleration = Vector2.ZERO
var steering_angle = 0.0
var engine_power = 0.0
var breaking_power = 0.0
var reverse = false
var reverse = true

onready var camera = $Camera2D

@@ -42,6 +46,14 @@ func _set_rear_axel(a : float) -> void:
if a > 0:
rear_axel = a

func _set_traction_slow_speed(s : float) -> void:
if s >= 0:
traction_slow_speed = s

func _set_traction_fast_speed(s : float) -> void:
if s >= 0:
traction_fast_speed = s

func _set_max_steering_angle(a : float) -> void:
max_steering_angle = a
if steering_angle > a:
@@ -88,34 +100,49 @@ func _physics_process(delta):
if Engine.editor_hint:
return

# TODO: Reverse engine_power sign when toggling reverse!
acceleration = -transform.y * engine_power
var accel_dir = 1.0
var power = engine_power
if breaking_power > 0:
if not reverse:
accel_dir = -1.0
power = breaking_power
elif reverse:
accel_dir = -1.0
acceleration = -transform.y * power * accel_dir
_apply_friction()
print(acceleration)
_calculate_heading(delta)
if reverse:
velocity += -acceleration * delta
else:
velocity += acceleration * delta
velocity += acceleration * delta
velocity = move_and_slide(velocity)

# -----------------------------------------------------------------------------
# Private "Helper" Methods
# -----------------------------------------------------------------------------

func _generate_traction() -> float:
var traction = 1.0
var speed = velocity.length()
if speed >= traction_fast_speed:
traction = traction_fast
elif speed >= traction_slow_speed:
var d = speed / (traction_fast_speed - traction_slow_speed)
traction = traction_slow + ((traction_fast - traction_slow) * d)
return traction

func _calculate_heading(delta : float) -> void:
var rear_wheel = position + transform.y * rear_axel
var fore_wheel = position - transform.y * forward_axel
rear_wheel += velocity * delta
fore_wheel += velocity.rotated(deg2rad(steering_angle)) * delta
var heading = (fore_wheel - rear_wheel).normalized()
var traction = _generate_traction()
var d = heading.dot(velocity.normalized())
if d > 0:
velocity = heading * velocity.length()
velocity = velocity.linear_interpolate(heading * velocity.length(), traction)
if d < 0:
velocity = -heading * velocity.length()
velocity = velocity.linear_interpolate(-heading * velocity.length(), traction)
#velocity = heading * velocity.length()
rotation = (heading.angle() + deg2rad(90))

@@ -124,9 +151,12 @@ func _apply_friction() -> void:
if velocity.length() < 5:
velocity = Vector2.ZERO
speed = 0
var force_friction = (-friction) * velocity
var force_drag = (-drag) * speed * velocity
acceleration += force_friction + force_drag
if breaking_power > 0 and speed <= 0:
acceleration = Vector2.ZERO
else:
var force_friction = (-friction) * velocity
var force_drag = (-drag) * speed * velocity
acceleration += force_friction + force_drag

# -----------------------------------------------------------------------------
# Public Methods
@@ -161,8 +191,11 @@ func set_throttle(v : float) -> void:
if reverse:
epm = reverse_engine_multiplier
engine_power = max_engine_power * epm * v

func set_break(v : float) -> void:
v = clamp(v, 0.0, 1.0)
breaking_power = max_breaking_power * v

# -----------------------------------------------------------------------------
# Event Handlers
# -----------------------------------------------------------------------------

+ 159
- 159
World.tscn
File diff suppressed because it is too large
View File


Loading…
Cancel
Save