fix: run bob now lerps back to home position

This commit is contained in:
Sara 2025-08-29 18:27:10 +02:00
parent b08b390c6e
commit 793391f0d3
2 changed files with 18 additions and 6 deletions

View file

@ -20,11 +20,20 @@ void PlayerCamera::update_fov() {
}
void PlayerCamera::update_offset() {
Basis const basis{ get_basis() };
double wave{ Math::sin(this->time) };
Vector3 const offset{ (this->body->get_is_running() ? Vector3{ float(wave), float(Math::abs(wave)), 0 } * 0.025 : Vector3{ 0, 0, 0 }) };
bool const is_running{ this->body->get_is_running() };
if (is_running != this->was_running) {
this->was_running = is_running;
this->run_bob_time = 0.0;
}
double wave{ Math::sin(this->run_bob_time) };
Vector3 offset{ Vector3{ float(wave), float(Math::abs(wave)), 0 } * this->run_bob_amplitude };
if (!is_running) {
this->run_bob_time = MIN(this->run_bob_time, this->run_bob_amplitude);
offset = offset.lerp(Vector3(), this->run_bob_time / this->run_bob_amplitude);
}
GETSET(position, {
position = this->home + basis.get_column(0) * offset.x + basis.get_column(1) * offset.y;
Basis const basis{ get_basis() };
position = this->home + basis.get_column(0) * offset.x + basis.get_column(1) * offset.y * 5.0;
});
for (Variant child : get_children()) {
Node3D *child_3d{ cast_to<Node3D>(child) };
@ -43,7 +52,7 @@ void PlayerCamera::ready() {
}
void PlayerCamera::process(double delta) {
this->time += delta * 10.;
this->run_bob_time += delta * this->run_bob_frequency;
update_offset();
this->fov_factor = 1.0;
update_fov();

View file

@ -26,8 +26,11 @@ private:
float base_fov{ 60.f };
float fov_factor{ 1.0f };
Vector3 home{ 0, 0, 0 };
double time{ 0.0 };
PlayerBody *body{ nullptr };
bool was_running{ false };
double run_bob_time{ 0.0 };
double run_bob_amplitude{ 0.025 };
double run_bob_frequency{ 10.0 };
};
#endif // !PLAYER_CAMERA_H