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

class_name PlayerCTRL class_name PlayerCTRL
extends Node extends Node


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




var throttle = 0.0 var throttle = 0.0
func _process(delta): func _process(delta):
if parent: if parent:
parent.set_throttle(throttle) parent.set_throttle(throttle)
#parent.breaking(breaking)
parent.set_break(breaking)
parent.set_steering(steering) parent.set_steering(steering)





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

export var camera_enabled : bool = false setget _set_camera_enabled export var camera_enabled : bool = false setget _set_camera_enabled
export var forward_axel : float = 10 setget _set_forward_axel export var forward_axel : float = 10 setget _set_forward_axel
export var rear_axel : float = 10 setget _set_rear_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 (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 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 export (float, 0.0, 1.0, 0.001) var reverse_engine_multiplier = 0.85
var steering_angle = 0.0 var steering_angle = 0.0
var engine_power = 0.0 var engine_power = 0.0
var breaking_power = 0.0 var breaking_power = 0.0
var reverse = false
var reverse = true


onready var camera = $Camera2D onready var camera = $Camera2D


if a > 0: if a > 0:
rear_axel = a 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: func _set_max_steering_angle(a : float) -> void:
max_steering_angle = a max_steering_angle = a
if steering_angle > a: if steering_angle > a:
if Engine.editor_hint: if Engine.editor_hint:
return 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() _apply_friction()
print(acceleration)
_calculate_heading(delta) _calculate_heading(delta)
if reverse:
velocity += -acceleration * delta
else:
velocity += acceleration * delta
velocity += acceleration * delta
velocity = move_and_slide(velocity) velocity = move_and_slide(velocity)


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


if velocity.length() < 5: if velocity.length() < 5:
velocity = Vector2.ZERO velocity = Vector2.ZERO
speed = 0 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 # Public Methods
if reverse: if reverse:
epm = reverse_engine_multiplier epm = reverse_engine_multiplier
engine_power = max_engine_power * epm * v 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 # Event Handlers
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------

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


Loading…
Cancel
Save