Rally Rush
Loading...
Searching...
No Matches
car_physics.hpp
Go to the documentation of this file.
1#ifndef CAR_PHYSICS_HPP
2#define CAR_PHYSICS_HPP
3
4#include "godot_cpp/classes/collision_shape3d.hpp"
5#include "godot_cpp/templates/hash_set.hpp"
6#include "godot_cpp/variant/rid.hpp"
7#include <godot_cpp/classes/curve.hpp>
8#include <godot_cpp/classes/physics_direct_body_state3d.hpp>
9#include <godot_cpp/classes/rigid_body3d.hpp>
10
11namespace godot {
13class CarPhysics : public RigidBody3D {
14 GDCLASS(CarPhysics, RigidBody3D);
16 static void _bind_methods();
17public:
19 virtual void _enter_tree() override;
20
22 virtual void _physics_process(double delta_time) override;
24 void process_oversteer(double delta_time);
26 void process_understeer(double delta_time);
28 virtual void _integrate_forces(PhysicsDirectBodyState3D *state) override;
29protected:
31 void integrate_steering(PhysicsDirectBodyState3D *state);
33 void integrate_engine_acceleration(PhysicsDirectBodyState3D *state);
35 void integrate_oversteer(PhysicsDirectBodyState3D *state);
37 void on_body_shape_entered(RID body_rid, Node *node, int body_shape_index, int local_shape_index);
39 void on_body_shape_exited(RID body_rid, Node *node, int body_shape_index, int local_shape_index);
41 float evaluate_oversteer_curve(float speed) const;
43 float evaluate_understeer_curve(float speed) const;
44
46 float get_true_target_speed() const;
48 bool is_grounded() const;
50 float get_current_acceleration() const;
51public:
53 Vector3 get_local_velocity() const;
55 Vector3 local_to_world_velocity() const;
57 Vector3 world_to_local_velocity() const;
59 float get_current_speed() const;
61 void set_target_speed(float target);
63 float get_target_speed() const;
65 void set_current_steering(float steering);
67 float get_current_steering() const;
69 void set_brake(bool value);
71 bool get_brake() const;
73 void set_oversteer_curve(Ref<Curve> curve);
75 Ref<Curve> get_oversteer_curve() const;
77 void set_oversteer_curve_x_scale(float scale);
79 float get_oversteer_curve_x_scale() const;
81 void set_understeer_curve(Ref<Curve> curve);
83 Ref<Curve> get_understeer_curve() const;
85 void set_understeer_curve_x_scale(float scale);
87 float get_understeer_curve_x_scale() const;
89 void set_acceleration(float value);
91 float get_acceleration() const;
93 void set_engine_brake_force(float value);
95 float get_engine_brake_force() const;
97 void set_handbrake_force(float value);
99 float get_handbrake_force() const;
101 void set_handbrake_oversteer(float value);
103 float get_handbrake_oversteer() const;
105 void set_traction_recovery_speed(float value);
107 float get_traction_recovery_speed() const;
109 void set_max_slide_speed(float value);
111 float get_max_slide_speed() const;
113 void set_slide_speed_acceleration(float value);
115 float get_slide_speed_acceleration() const;
117 void set_oversteer_speed_penalty(float value);
119 float get_oversteer_speed_penalty() const;
121 void set_oversteer_brake_penalty(float value);
123 float get_oversteer_brake_penalty() const;
125 void set_oversteer_steering_speed(float value);
127 float get_oversteer_steering_speed() const;
129 void set_slide_resistance(float value);
131 float get_slide_resistance() const;
133 void set_steering_inward_speed(float value);
135 float get_steering_inward_speed() const;
136private:
138 Vector3 last_velocity{0.f, 0.f, 0.f};
140 Vector3 local_velocity{0.f, 0.f, 0.f};
142 float target_speed{0.f};
146 bool brake{false};
147
148 Ref<Curve> oversteer_curve{};
150 Ref<Curve> understeer_curve{};
152 float acceleration{20.f};
154 float handbrake_force{40.f};
157 float max_slide_speed{20.f};
163 float slide_resistance{20.f};
165
166 HashSet<Node*> grounded_objects{};
167
168 CollisionShape3D *front_wheels{nullptr};
169 CollisionShape3D *back_wheels{nullptr};
170
173};
174}
175
176#endif // !CAR_PHYSICS_HPP
Subclass Sandbox for car physics. Uses _integrate_forces and local_velocity to abstract the behaviour...
Definition car_physics.hpp:13
void set_current_steering(float steering)
The current steering input.
Definition car_physics.cpp:204
float oversteer_speed_penalty
Definition car_physics.hpp:159
float get_understeer_curve_x_scale() const
The amount of speed represented by x=1 on the understeer curve.
Definition car_physics.cpp:248
bool brake
While true, the handbrake is on.
Definition car_physics.hpp:146
float acceleration
Definition car_physics.hpp:152
float get_acceleration() const
The base engine acceleration of this car.
Definition car_physics.cpp:256
float get_oversteer_steering_speed() const
Base amount of steering applied while oversteering.
Definition car_physics.cpp:328
CollisionShape3D * back_wheels
Definition car_physics.hpp:169
void set_acceleration(float value)
The base engine acceleration of this car.
Definition car_physics.cpp:252
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
float get_slide_resistance() const
Deceleration applied to sideways velocity.
Definition car_physics.cpp:336
void set_slide_resistance(float value)
Deceleration applied to sideways velocity.
Definition car_physics.cpp:332
void set_oversteer_steering_speed(float value)
Modifier applied to brake force when oversteering.
Definition car_physics.cpp:324
float target_speed
Target value for local_velocity.z.
Definition car_physics.hpp:142
void set_steering_inward_speed(float value)
Target amount of velocity towards the centre of a turn while steering.
Definition car_physics.cpp:340
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
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
float steering_inward_speed
Definition car_physics.hpp:164
float slide_resistance
Definition car_physics.hpp:163
float get_current_acceleration() const
Returns acceleration, taking into account braking, throttle, and handbrake.
Definition car_physics.cpp:160
float slide_speed_acceleration
Definition car_physics.hpp:158
void process_understeer(double delta_time)
Process the current_understeer member variable.
Definition car_physics.cpp:66
float get_target_speed() const
The target speed.
Definition car_physics.cpp:200
float get_max_slide_speed() const
The maximum sideways speed that can be reached while drifting.
Definition car_physics.cpp:296
float get_engine_brake_force() const
The base engine braking force of this car.
Definition car_physics.cpp:264
float handbrake_oversteer
Definition car_physics.hpp:155
void set_slide_speed_acceleration(float value)
The sideways acceleration applied when drifting.
Definition car_physics.cpp:300
float get_current_speed() const
The current forward velocity (local_velocity.z)
Definition car_physics.cpp:192
float get_oversteer_brake_penalty() const
Modifier applied to brake force when oversteering.
Definition car_physics.cpp:320
Ref< Curve > oversteer_curve
Definition car_physics.hpp:148
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
void set_oversteer_speed_penalty(float value)
Deceleration applied to forward speed when oversteering.
Definition car_physics.cpp:308
float get_current_steering() const
The current steering input.
Definition car_physics.cpp:208
void set_max_slide_speed(float value)
The maximum sideways speed that can be reached while drifting.
Definition car_physics.cpp:292
Vector3 world_to_local_velocity() const
transform the current world velocity to local coordinates
Definition car_physics.cpp:182
void set_target_speed(float target)
The target speed.
Definition car_physics.cpp:196
float get_oversteer_curve_x_scale() const
The amount of speed represented by x=1 on the oversteer curve.
Definition car_physics.cpp:232
Ref< Curve > understeer_curve
Definition car_physics.hpp:150
void process_oversteer(double delta_time)
Process the current_oversteer member variable.
Definition car_physics.cpp:53
Vector3 last_velocity
Acceleration at the end of the integration step of the last frame.
Definition car_physics.hpp:138
void integrate_oversteer(PhysicsDirectBodyState3D *state)
Integrate oversteering to local_velocity to allow drifting.
Definition car_physics.cpp:116
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
Vector3 local_velocity
Velocity relative to the local transform.
Definition car_physics.hpp:140
float current_understeer
Definition car_physics.hpp:171
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
float engine_brake_force
Definition car_physics.hpp:153
HashSet< Node * > grounded_objects
Definition car_physics.hpp:166
float get_handbrake_oversteer() const
The modifier applied to oversteering when the handbrake is active.
Definition car_physics.cpp:280
void set_understeer_curve(Ref< Curve > curve)
The base curve used for understeering.
Definition car_physics.cpp:236
float get_slide_speed_acceleration() const
The sideways acceleration applied when drifting.
Definition car_physics.cpp:304
float traction_recovery_speed
Definition car_physics.hpp:156
void set_brake(bool value)
True if the hand-brake is currently active.
Definition car_physics.cpp:212
void set_engine_brake_force(float value)
The base engine braking force of this car.
Definition car_physics.cpp:260
float get_steering_inward_speed() const
Target amount of velocity towards the centre of a turn while steering.
Definition car_physics.cpp:344
float understeer_speed_penalty
Definition car_physics.hpp:162
Ref< Curve > get_oversteer_curve() const
The base curve used for oversteering.
Definition car_physics.cpp:224
Vector3 get_local_velocity() const
Public getter for current local velocity.
Definition car_physics.cpp:168
void set_oversteer_curve(Ref< Curve > curve)
The base curve used for oversteering.
Definition car_physics.cpp:220
void set_handbrake_oversteer(float value)
The modifier applied to oversteering when the handbrake is active.
Definition car_physics.cpp:276
float get_oversteer_speed_penalty() const
Deceleration applied to forward speed when oversteering.
Definition car_physics.cpp:312
float max_slide_speed
Definition car_physics.hpp:157
virtual void _enter_tree() override
Enable _integrate_forces, fetch child nodes, and setup contact monitoring.
Definition car_physics.cpp:28
float current_steering
Current steering input.
Definition car_physics.hpp:144
float oversteer_brake_penalty
Definition car_physics.hpp:160
CollisionShape3D * front_wheels
Definition car_physics.hpp:168
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
float understeer_curve_x_scale
Definition car_physics.hpp:151
GDCLASS(CarPhysics, RigidBody3D)
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
Ref< Curve > get_understeer_curve() const
The base curve used for understeering.
Definition car_physics.cpp:240
virtual void _physics_process(double delta_time) override
Fixed-interval process function. Split into process_oversteer and process_understeer.
Definition car_physics.cpp:40
float handbrake_force
Definition car_physics.hpp:154
float get_handbrake_force() const
The amount of braking force applied by the handbrake.
Definition car_physics.cpp:272
float evaluate_oversteer_curve(float speed) const
evaluate the oversteer curve with a speed, taking *_x_scale into account
Definition car_physics.cpp:136
float evaluate_understeer_curve(float speed) const
evaluate the understeer curve with a speed, taking *_x_scale into account
Definition car_physics.cpp:142
float current_oversteer
Definition car_physics.hpp:172
bool get_brake() const
True if the hand-brake is currently active.
Definition car_physics.cpp:216
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
Vector3 local_to_world_velocity() const
transform the current local_velocity to world coordinates
Definition car_physics.cpp:172
bool is_grounded() const
Returns true if there is at least one contact with either of the wheels.
Definition car_physics.cpp:156
void set_handbrake_force(float value)
The amount of braking force applied by the handbrake.
Definition car_physics.cpp:268
float oversteer_steering_speed
Definition car_physics.hpp:161
float get_true_target_speed() const
Returns the maximum speed modified by under/oversteer.
Definition car_physics.cpp:148
static void _bind_methods()
Registers functions and properties.
Definition car_physics.cpp:8
float oversteer_curve_x_scale
Definition car_physics.hpp:149
void set_oversteer_brake_penalty(float value)
Modifier applied to brake force when oversteering.
Definition car_physics.cpp:316
Definition beacon_powerup.cpp:6