feat: first attempt at car physics integrator

This commit is contained in:
Sara 2024-05-16 09:35:28 +02:00
parent c6bdd35af9
commit 5c728b1b39

View file

@ -14,6 +14,28 @@ void CarPhysics::_enter_tree() {
}
void CarPhysics::_integrate_forces(PhysicsDirectBodyState3D *state) {
Basis const basis = this->get_global_basis();
this->local_velocity.z = Math::move_toward(this->local_velocity.z, this->target_speed, float(ACCELERATION * state->get_step()));
if(this->local_velocity.z != 0.f) {
state->set_angular_velocity(basis.get_column(1) * -this->current_steering * Math::sign(this->local_velocity.z));
this->local_velocity.x = -this->current_steering * 0.1f;
} else {
state->set_angular_velocity(Vector3(0,0,0));
this->local_velocity.x = 0;
}
Vector3 world_velocity{
this->local_velocity.x * basis.get_column(0) +
this->local_velocity.y * basis.get_column(1) +
this->local_velocity.z * basis.get_column(2)
};
world_velocity.y = 0;
if(state->get_linear_velocity().y > this->gravity_velocity && state->get_linear_velocity().y <= 0.f)
this->gravity_velocity = 0.f;
else
this->gravity_velocity -= 15.f * state->get_step();
state->set_linear_velocity(world_velocity + Vector3{0, this->gravity_velocity, 0});
}
void CarPhysics::set_target_speed(float target) {
this->target_speed = target;