# Rally Rush: src/car_physics.hpp Source File

Loading...
Searching...
No Matches
car_physics.hpp
Go to the documentation of this file.
#ifndef CAR_PHYSICS_HPP
#define CAR_PHYSICS_HPP
#include "godot_cpp/classes/collision_shape3d.hpp"
#include "godot_cpp/templates/hash_set.hpp"
#include "godot_cpp/variant/rid.hpp"
#include <godot_cpp/classes/curve.hpp>
#include <godot_cpp/classes/physics_direct_body_state3d.hpp>
#include <godot_cpp/classes/rigid_body3d.hpp>
namespace godot {
class CarPhysics : public RigidBody3D {
GDCLASS(CarPhysics, RigidBody3D);
static void _bind_methods();
public:
virtual void _enter_tree() override;
virtual void _physics_process(double delta_time) override;
void process_oversteer(double delta_time);
void process_understeer(double delta_time);
virtual void _integrate_forces(PhysicsDirectBodyState3D *state) override;
protected:
void integrate_steering(PhysicsDirectBodyState3D *state);
void integrate_engine_acceleration(PhysicsDirectBodyState3D *state);
void integrate_oversteer(PhysicsDirectBodyState3D *state);
void on_body_shape_entered(RID body_rid, Node *node, int body_shape_index, int local_shape_index);
void on_body_shape_exited(RID body_rid, Node *node, int body_shape_index, int local_shape_index);
float evaluate_oversteer_curve(float speed) const;
float evaluate_understeer_curve(float speed) const;
float get_true_target_speed() const;
bool is_grounded() const;
float get_current_acceleration() const;
public:
Vector3 get_local_velocity() const;
Vector3 local_to_world_velocity() const;
Vector3 world_to_local_velocity() const;
float get_current_speed() const;
void set_target_speed(float target);
float get_target_speed() const;
void set_current_steering(float steering);
float get_current_steering() const;
void set_brake(bool value);
bool get_brake() const;
void set_oversteer_curve(Ref<Curve> curve);
Ref<Curve> get_oversteer_curve() const;
void set_oversteer_curve_x_scale(float scale);
float get_oversteer_curve_x_scale() const;
void set_understeer_curve(Ref<Curve> curve);
Ref<Curve> get_understeer_curve() const;
void set_understeer_curve_x_scale(float scale);
float get_understeer_curve_x_scale() const;
void set_acceleration(float value);
float get_acceleration() const;
void set_engine_brake_force(float value);
float get_engine_brake_force() const;
void set_handbrake_force(float value);
float get_handbrake_force() const;
void set_handbrake_oversteer(float value);
float get_handbrake_oversteer() const;
void set_traction_recovery_speed(float value);
float get_traction_recovery_speed() const;
void set_max_slide_speed(float value);
float get_max_slide_speed() const;
void set_slide_speed_acceleration(float value);
float get_slide_speed_acceleration() const;
void set_oversteer_speed_penalty(float value);
float get_oversteer_speed_penalty() const;
void set_oversteer_brake_penalty(float value);
float get_oversteer_brake_penalty() const;
void set_oversteer_steering_speed(float value);
float get_oversteer_steering_speed() const;
void set_slide_resistance(float value);
float get_slide_resistance() const;
void set_steering_inward_speed(float value);
float get_steering_inward_speed() const;
private:
Vector3 last_velocity{0.f, 0.f, 0.f};
Vector3 local_velocity{0.f, 0.f, 0.f};
float target_speed{0.f};
float current_steering{0.f};
bool brake{false};
Ref<Curve> oversteer_curve{};
float oversteer_curve_x_scale{50.f};
Ref<Curve> understeer_curve{};
float understeer_curve_x_scale{50.f};
float acceleration{20.f};
float engine_brake_force{30.f};
float handbrake_force{40.f};
float handbrake_oversteer{1.f};
float traction_recovery_speed{1.f};
float max_slide_speed{20.f};
float slide_speed_acceleration{0.05f};
float oversteer_speed_penalty{5.f};
float oversteer_brake_penalty{0.25f};
float oversteer_steering_speed{0.7f};
float understeer_speed_penalty{3.f};
float slide_resistance{20.f};
float steering_inward_speed{0.1f};
HashSet<Node*> grounded_objects{};
CollisionShape3D *front_wheels{nullptr};
CollisionShape3D *back_wheels{nullptr};
float current_understeer{0.f};
float current_oversteer{0.f};
};
}
#endif // !CAR_PHYSICS_HPP
godot::CarPhysics
Subclass Sandbox for car physics. Uses _integrate_forces and local_velocity to abstract the behaviour...
Definition car_physics.hpp:13
godot::CarPhysics::set_current_steering
void set_current_steering(float steering)
The current steering input.
Definition car_physics.cpp:204
godot::CarPhysics::oversteer_speed_penalty
float oversteer_speed_penalty
Definition car_physics.hpp:159
godot::CarPhysics::get_understeer_curve_x_scale
float get_understeer_curve_x_scale() const
The amount of speed represented by x=1 on the understeer curve.
Definition car_physics.cpp:248
godot::CarPhysics::brake
bool brake
While true, the handbrake is on.
Definition car_physics.hpp:146
godot::CarPhysics::acceleration
float acceleration
Definition car_physics.hpp:152
godot::CarPhysics::get_acceleration
float get_acceleration() const
The base engine acceleration of this car.
Definition car_physics.cpp:256
godot::CarPhysics::get_oversteer_steering_speed
float get_oversteer_steering_speed() const
Base amount of steering applied while oversteering.
Definition car_physics.cpp:328
godot::CarPhysics::back_wheels
CollisionShape3D * back_wheels
Definition car_physics.hpp:169
godot::CarPhysics::set_acceleration
void set_acceleration(float value)
The base engine acceleration of this car.
Definition car_physics.cpp:252
godot::CarPhysics::set_traction_recovery_speed
void set_traction_recovery_speed(float value)
The speed at which the car will return to regular traction after under- or oversteering.
Definition car_physics.cpp:284
godot::CarPhysics::get_slide_resistance
float get_slide_resistance() const
Deceleration applied to sideways velocity.
Definition car_physics.cpp:336
godot::CarPhysics::set_slide_resistance
void set_slide_resistance(float value)
Deceleration applied to sideways velocity.
Definition car_physics.cpp:332
godot::CarPhysics::set_oversteer_steering_speed
void set_oversteer_steering_speed(float value)
Modifier applied to brake force when oversteering.
Definition car_physics.cpp:324
godot::CarPhysics::target_speed
float target_speed
Target value for local_velocity.z.
Definition car_physics.hpp:142
godot::CarPhysics::set_steering_inward_speed
void set_steering_inward_speed(float value)
Target amount of velocity towards the centre of a turn while steering.
Definition car_physics.cpp:340
godot::CarPhysics::integrate_steering
void integrate_steering(PhysicsDirectBodyState3D *state)
Integrate steering into angular and local x velocities. As a side-effect this also applies sliding re...
Definition car_physics.cpp:90
godot::CarPhysics::integrate_engine_acceleration
void integrate_engine_acceleration(PhysicsDirectBodyState3D *state)
Apply velocity along local z. Accelerates towards value returned by get_true_target_speed at get_curr...
Definition car_physics.cpp:105
godot::CarPhysics::steering_inward_speed
float steering_inward_speed
Definition car_physics.hpp:164
godot::CarPhysics::slide_resistance
float slide_resistance
Definition car_physics.hpp:163
godot::CarPhysics::get_current_acceleration
float get_current_acceleration() const
Returns acceleration, taking into account braking, throttle, and handbrake.
Definition car_physics.cpp:160
godot::CarPhysics::slide_speed_acceleration
float slide_speed_acceleration
Definition car_physics.hpp:158
godot::CarPhysics::process_understeer
void process_understeer(double delta_time)
Process the current_understeer member variable.
Definition car_physics.cpp:66
godot::CarPhysics::get_target_speed
float get_target_speed() const
The target speed.
Definition car_physics.cpp:200
godot::CarPhysics::get_max_slide_speed
float get_max_slide_speed() const
The maximum sideways speed that can be reached while drifting.
Definition car_physics.cpp:296
godot::CarPhysics::get_engine_brake_force
float get_engine_brake_force() const
The base engine braking force of this car.
Definition car_physics.cpp:264
godot::CarPhysics::handbrake_oversteer
float handbrake_oversteer
Definition car_physics.hpp:155
godot::CarPhysics::set_slide_speed_acceleration
void set_slide_speed_acceleration(float value)
The sideways acceleration applied when drifting.
Definition car_physics.cpp:300
godot::CarPhysics::get_current_speed
float get_current_speed() const
The current forward velocity (local_velocity.z)
Definition car_physics.cpp:192
godot::CarPhysics::get_oversteer_brake_penalty
float get_oversteer_brake_penalty() const
Modifier applied to brake force when oversteering.
Definition car_physics.cpp:320
godot::CarPhysics::oversteer_curve
Ref< Curve > oversteer_curve
Definition car_physics.hpp:148
godot::CarPhysics::get_traction_recovery_speed
float get_traction_recovery_speed() const
The speed at which the car will return to regular traction after under- or oversteering.
Definition car_physics.cpp:288
godot::CarPhysics::set_oversteer_speed_penalty
void set_oversteer_speed_penalty(float value)
Deceleration applied to forward speed when oversteering.
Definition car_physics.cpp:308
godot::CarPhysics::get_current_steering
float get_current_steering() const
The current steering input.
Definition car_physics.cpp:208
godot::CarPhysics::set_max_slide_speed
void set_max_slide_speed(float value)
The maximum sideways speed that can be reached while drifting.
Definition car_physics.cpp:292
godot::CarPhysics::world_to_local_velocity
Vector3 world_to_local_velocity() const
transform the current world velocity to local coordinates
Definition car_physics.cpp:182
godot::CarPhysics::set_target_speed
void set_target_speed(float target)
The target speed.
Definition car_physics.cpp:196
godot::CarPhysics::get_oversteer_curve_x_scale
float get_oversteer_curve_x_scale() const
The amount of speed represented by x=1 on the oversteer curve.
Definition car_physics.cpp:232
godot::CarPhysics::understeer_curve
Ref< Curve > understeer_curve
Definition car_physics.hpp:150
godot::CarPhysics::process_oversteer
void process_oversteer(double delta_time)
Process the current_oversteer member variable.
Definition car_physics.cpp:53
godot::CarPhysics::last_velocity
Vector3 last_velocity
Acceleration at the end of the integration step of the last frame.
Definition car_physics.hpp:138
godot::CarPhysics::integrate_oversteer
void integrate_oversteer(PhysicsDirectBodyState3D *state)
Integrate oversteering to local_velocity to allow drifting.
Definition car_physics.cpp:116
godot::CarPhysics::_integrate_forces
virtual void _integrate_forces(PhysicsDirectBodyState3D *state) override
Custom force integrator. Split into integrate_steering, integrate_engine_acceleration and integrate_o...
Definition car_physics.cpp:77
godot::CarPhysics::local_velocity
Vector3 local_velocity
Velocity relative to the local transform.
Definition car_physics.hpp:140
godot::CarPhysics::current_understeer
float current_understeer
Definition car_physics.hpp:171
godot::CarPhysics::set_understeer_curve_x_scale
void set_understeer_curve_x_scale(float scale)
The amount of speed represented by x=1 on the understeer curve.
Definition car_physics.cpp:244
godot::CarPhysics::engine_brake_force
float engine_brake_force
Definition car_physics.hpp:153
godot::CarPhysics::grounded_objects
HashSet< Node * > grounded_objects
Definition car_physics.hpp:166
godot::CarPhysics::get_handbrake_oversteer
float get_handbrake_oversteer() const
The modifier applied to oversteering when the handbrake is active.
Definition car_physics.cpp:280
godot::CarPhysics::set_understeer_curve
void set_understeer_curve(Ref< Curve > curve)
The base curve used for understeering.
Definition car_physics.cpp:236
godot::CarPhysics::get_slide_speed_acceleration
float get_slide_speed_acceleration() const
The sideways acceleration applied when drifting.
Definition car_physics.cpp:304
godot::CarPhysics::traction_recovery_speed
float traction_recovery_speed
Definition car_physics.hpp:156
godot::CarPhysics::set_brake
void set_brake(bool value)
True if the hand-brake is currently active.
Definition car_physics.cpp:212
godot::CarPhysics::set_engine_brake_force
void set_engine_brake_force(float value)
The base engine braking force of this car.
Definition car_physics.cpp:260
godot::CarPhysics::get_steering_inward_speed
float get_steering_inward_speed() const
Target amount of velocity towards the centre of a turn while steering.
Definition car_physics.cpp:344
godot::CarPhysics::understeer_speed_penalty
float understeer_speed_penalty
Definition car_physics.hpp:162
godot::CarPhysics::get_oversteer_curve
Ref< Curve > get_oversteer_curve() const
The base curve used for oversteering.
Definition car_physics.cpp:224
godot::CarPhysics::get_local_velocity
Vector3 get_local_velocity() const
Public getter for current local velocity.
Definition car_physics.cpp:168
godot::CarPhysics::set_oversteer_curve
void set_oversteer_curve(Ref< Curve > curve)
The base curve used for oversteering.
Definition car_physics.cpp:220
godot::CarPhysics::set_handbrake_oversteer
void set_handbrake_oversteer(float value)
The modifier applied to oversteering when the handbrake is active.
Definition car_physics.cpp:276
godot::CarPhysics::get_oversteer_speed_penalty
float get_oversteer_speed_penalty() const
Deceleration applied to forward speed when oversteering.
Definition car_physics.cpp:312
godot::CarPhysics::max_slide_speed
float max_slide_speed
Definition car_physics.hpp:157
godot::CarPhysics::_enter_tree
virtual void _enter_tree() override
Enable _integrate_forces, fetch child nodes, and setup contact monitoring.
Definition car_physics.cpp:28
godot::CarPhysics::current_steering
float current_steering
Current steering input.
Definition car_physics.hpp:144
godot::CarPhysics::oversteer_brake_penalty
float oversteer_brake_penalty
Definition car_physics.hpp:160
godot::CarPhysics::front_wheels
CollisionShape3D * front_wheels
Definition car_physics.hpp:168
godot::CarPhysics::set_oversteer_curve_x_scale
void set_oversteer_curve_x_scale(float scale)
The amount of speed represented by x=1 on the oversteer curve.
Definition car_physics.cpp:228
godot::CarPhysics::understeer_curve_x_scale
float understeer_curve_x_scale
Definition car_physics.hpp:151
godot::CarPhysics::GDCLASS
GDCLASS(CarPhysics, RigidBody3D)
godot::CarPhysics::on_body_shape_exited
void on_body_shape_exited(RID body_rid, Node *node, int body_shape_index, int local_shape_index)
Detect that an object lost contact with the wheels.
Definition car_physics.cpp:131
godot::CarPhysics::get_understeer_curve
Ref< Curve > get_understeer_curve() const
The base curve used for understeering.
Definition car_physics.cpp:240
godot::CarPhysics::_physics_process
virtual void _physics_process(double delta_time) override
Fixed-interval process function. Split into process_oversteer and process_understeer.
Definition car_physics.cpp:40
godot::CarPhysics::handbrake_force
float handbrake_force
Definition car_physics.hpp:154
godot::CarPhysics::get_handbrake_force
float get_handbrake_force() const
The amount of braking force applied by the handbrake.
Definition car_physics.cpp:272
godot::CarPhysics::evaluate_oversteer_curve
float evaluate_oversteer_curve(float speed) const
evaluate the oversteer curve with a speed, taking *_x_scale into account
Definition car_physics.cpp:136
godot::CarPhysics::evaluate_understeer_curve
float evaluate_understeer_curve(float speed) const
evaluate the understeer curve with a speed, taking *_x_scale into account
Definition car_physics.cpp:142
godot::CarPhysics::current_oversteer
float current_oversteer
Definition car_physics.hpp:172
godot::CarPhysics::get_brake
bool get_brake() const
True if the hand-brake is currently active.
Definition car_physics.cpp:216
godot::CarPhysics::on_body_shape_entered
void on_body_shape_entered(RID body_rid, Node *node, int body_shape_index, int local_shape_index)
Detect that an object started colliding with the wheels.
Definition car_physics.cpp:121
godot::CarPhysics::local_to_world_velocity
Vector3 local_to_world_velocity() const
transform the current local_velocity to world coordinates
Definition car_physics.cpp:172
godot::CarPhysics::is_grounded
bool is_grounded() const
Returns true if there is at least one contact with either of the wheels.
Definition car_physics.cpp:156
godot::CarPhysics::set_handbrake_force
void set_handbrake_force(float value)
The amount of braking force applied by the handbrake.
Definition car_physics.cpp:268
godot::CarPhysics::oversteer_steering_speed
float oversteer_steering_speed
Definition car_physics.hpp:161
godot::CarPhysics::get_true_target_speed
float get_true_target_speed() const
Returns the maximum speed modified by under/oversteer.
Definition car_physics.cpp:148
godot::CarPhysics::_bind_methods
static void _bind_methods()
Registers functions and properties.
Definition car_physics.cpp:8
godot::CarPhysics::oversteer_curve_x_scale
float oversteer_curve_x_scale
Definition car_physics.hpp:149
godot::CarPhysics::set_oversteer_brake_penalty
void set_oversteer_brake_penalty(float value)
Modifier applied to brake force when oversteering.
Definition car_physics.cpp:316
godot
Definition beacon_powerup.cpp:6