Added engine force, air resistance, and rolling resistance
also added debug draw 3d addon
This commit is contained in:
35
car.gd
35
car.gd
@@ -1,6 +1,10 @@
|
||||
class_name Car
|
||||
extends RigidBody3D
|
||||
|
||||
## X axis is RPM, Y axis is torque in NM
|
||||
@export var torque_curve: Curve
|
||||
@export var air_resistance: float = .3
|
||||
|
||||
@export_group("Steering")
|
||||
## Maximum angle of imaginary wheel at center front of the car
|
||||
@export var max_steer_angle: float = 30
|
||||
@@ -18,11 +22,15 @@ extends RigidBody3D
|
||||
@onready var track: float = front_right.position.distance_to(front_left.position)
|
||||
|
||||
var current_steer_angle: float = 0
|
||||
var rpm: float = 0
|
||||
|
||||
func _ready() -> void:
|
||||
for wheel: Wheel in wheels:
|
||||
wheel.set_new_car(self)
|
||||
|
||||
func _physics_process(delta: float) -> void:
|
||||
steer(delta)
|
||||
for wheel: Wheel in wheels:
|
||||
wheel.apply_forces(self)
|
||||
apply_air_resistance()
|
||||
|
||||
func steer(delta: float) -> void:
|
||||
var steer_input = Input.get_axis("Steer Right", "Steer Left")
|
||||
@@ -44,6 +52,25 @@ func steer(delta: float) -> void:
|
||||
front_right.rotation.y = -inner_angle
|
||||
front_left.rotation.y = -outer_angle
|
||||
|
||||
##Point argument is in local space
|
||||
func apply_air_resistance() -> void:
|
||||
var force: Vector3 = air_resistance * linear_velocity * linear_velocity.length()
|
||||
apply_force(force, center_of_mass)
|
||||
DebugDraw3D.draw_arrow(
|
||||
to_global(center_of_mass),
|
||||
to_global(center_of_mass) + force,
|
||||
Color.YELLOW,
|
||||
.2
|
||||
)
|
||||
|
||||
##Point argument is in global space
|
||||
func get_velocity_at_point(point: Vector3) -> Vector3:
|
||||
return linear_velocity + angular_velocity.cross(point - center_of_mass)
|
||||
return linear_velocity + angular_velocity.cross(to_local(point) - center_of_mass)
|
||||
|
||||
## Returns torque in NM, per wheel on the drive train
|
||||
func get_torque_per_wheel() -> float:
|
||||
var is_powered: Callable = func (wheel: Wheel) -> bool: return wheel.on_drivetrain
|
||||
var powered_wheel_count: int = wheels.filter(is_powered).size()
|
||||
if 0 < powered_wheel_count:
|
||||
var engine_torque: float = Input.get_action_strength("Accelerate") * torque_curve.sample_baked(rpm)
|
||||
return engine_torque / powered_wheel_count
|
||||
return 0.0
|
||||
|
||||
Reference in New Issue
Block a user