look at my replication of gta player physics, hwy it fall so slow? I use same timer and gravity value is correct:
void Player::Update(float deltaTime)
{
float timestep = CTimer::GetTimeStep();
//deltaTime *= 30;
code
switch (m_state)
{
case PlayerState::STANDING:
HandleStanding(timestep);
break;
case PlayerState::JUMPING:
HandleJumping(timestep);
break;
case PlayerState::FALLING:
HandleFalling(timestep);
break;
case PlayerState::LANDING:
HandleStanding(timestep);
break;
default:
break;
}
if (isPlayerInAir())
{
if (m_state != PlayerState::JUMPING && m_state != PlayerState::FALLING)
{
SetState(PlayerState::FALLING);
std::cout << "Transitioning to FALLING state in Update." << std::endl;
}
}
if (isPlayerInAir() && m_moveSpeed.y > 0.25f)
{
float airResistance = powf(0.95f, CTimer::GetTimeStep());
m_moveSpeed *= airResistance;
std::cout << "Applied air resistance. New m_moveSpeed: " << m_moveSpeed.y << std::endl;
}
if (isPlayerInAir())
{
ApplyGravity(timestep);
ApplyAirResistance(timestep);
}
glm::vec3 newPosition = GetPosition();
newPosition += m_moveSpeed * CTimer::GetTimeStep();
SetPosition(newPosition);
//CTimer::Update();
The issue of your player falling too slowly in your GTA physics replication can stem from several factors within your implementation. Let's break down potential causes and provide actionable solutions to address them.
Gravity Magnitude: The value 0.008f might be too small, especially if your timestep is in seconds. In most physics simulations, gravity is around 9.81 m/s². If your game's unit system aligns with real-world measurements, this value is significantly underestimated.
Timestep Scaling: Ensure that CTimer::GetTimeStep() returns the timestep in seconds. If it's in milliseconds, the gravity application becomes even smaller (e.g., 0.008f * 0.016f ≈ 0.000128f per frame at 60 FPS), which is insufficient.
Recommended Solutions:
Increase Gravity Value: Adjust the gravity to a higher value that aligns with the desired fall speed. For example:
cpp
void Player::ApplyGravity(float dt)
{
constexpr float gravity = 9.81f; // Adjust based on your unit system
m_moveSpeed.y -= gravity * dt;
}
Verify Timestep Units: Ensure that CTimer::GetTimeStep() provides the timestep in seconds. If it's in milliseconds, convert it:
cpp
float timestep = CTimer::GetTimeStep() / 1000.0f; // Convert ms to seconds
Uniform Air Resistance: Applying air resistance uniformly to all components of m_moveSpeed (including the vertical y component) can excessively dampen the fall speed.
High Air Resistance Coefficient: A high air resistance value (0.95f per timestep) can significantly reduce the velocity each frame, making the fall appear sluggish.
Recommended Solutions:
Separate Air Resistance for Vertical Movement:
Apply air resistance only to the horizontal (x and z) components, preserving the vertical velocity influenced by gravity.
cpp
void Player::ApplyAirResistance(float dt)
{
if (m_fAirResistance > 0.1f) {
float f = powf(0.95f, dt);
glm::vec3 horizontalSpeed = glm::vec3(m_moveSpeed.x, 0.0f, m_moveSpeed.z) * f;
m_moveSpeed.x = horizontalSpeed.x;
m_moveSpeed.z = horizontalSpeed.z;
// Optionally, handle separate air resistance for vertical speed if desired
}
else {
// Handle low air resistance case similarly
}
}
Adjust Air Resistance Coefficient:
Lower the air resistance coefficient to reduce its damping effect on the velocity.
cpp
float f = powf(0.99f, dt); // Less damping per frame
3. Multiple Applications of Air Resistance and Gravity
Redundant Air Resistance: Air resistance is being applied twice per frame when m_moveSpeed.y > 0.25f and again unconditionally. This doubles the effect, potentially making the fall too slow.
Recommended Solutions:
Consolidate Air Resistance Application:
Ensure that air resistance is applied only once per frame to avoid excessive damping.
cpp
if (isPlayerInAir())
{
ApplyGravity(timestep);
ApplyAirResistance(timestep);
}
Remove the earlier conditional air resistance block unless it's specifically needed for certain scenarios.
Inconsistent Use of deltaTime: Although Update receives deltaTime as a parameter, it uses CTimer::GetTimeStep() internally. If these two values are different or if there's a synchronization issue, it could affect the physics calculations.
Recommended Solutions:
Use deltaTime Consistently:
Replace CTimer::GetTimeStep() with deltaTime to ensure consistency.
cpp
void Player::Update(float deltaTime)
{
float timestep = deltaTime;
//...
// In ApplyGravity and ApplyAirResistance, pass timestep
}
Ensure that wherever Update is called, deltaTime accurately represents the time elapsed since the last frame in seconds.
5. Example Revised Code Snippet
Here's a revised version of your ApplyGravity and ApplyAirResistance functions incorporating the recommendations:
cpp
void Player::ApplyGravity(float dt)
{
constexpr float gravity = 9.81f; // Adjust based on your unit system
m_moveSpeed.y -= gravity * dt;
}
void Player::ApplyAirResistance(float dt)
{
if (m_fAirResistance > 0.1f) {
float airResistanceFactor = powf(0.99f, dt); // Less damping
// Apply only to horizontal components
m_moveSpeed.x *= airResistanceFactor;
m_moveSpeed.z *= airResistanceFactor;
}
else {
// Handle lower air resistance if needed
}
}
And ensure your Update function uses deltaTime consistently:
cpp
void Player::Update(float deltaTime)
{
float timestep = deltaTime;
switch (m_state)
{
case PlayerState::STANDING:
HandleStanding(timestep);
break;
case PlayerState::JUMPING:
HandleJumping(timestep);
break;
case PlayerState::FALLING:
HandleFalling(timestep);
break;
case PlayerState::LANDING:
HandleStanding(timestep);
break;
default:
break;
}
if (isPlayerInAir())
{
if (m_state != PlayerState::JUMPING && m_state != PlayerState::FALLING)
{
SetState(PlayerState::FALLING);
std::cout << "Transitioning to FALLING state in Update." << std::endl;
}
}
if (isPlayerInAir())
{
ApplyGravity(timestep);
ApplyAirResistance(timestep);
}
glm::vec3 newPosition = GetPosition();
newPosition += m_moveSpeed * timestep;
SetPosition(newPosition);
}
6. Additional Recommendations
Debugging Steps:
Visualize Velocity: Print out or visualize m_moveSpeed.y each frame to ensure gravity is affecting it as expected.
Check Timestep Values: Log the timestep value to confirm it's in the expected range (e.g., ~0.016 for 60 FPS).
Physics Tuning:
Adjust Gravity and Air Resistance: Fine-tune the gravity and air resistance values based on gameplay feel rather than exact real-world physics, especially if your game uses a unique unit system.
Unit Consistency:
Maintain Consistent Units: Ensure all physics-related calculations use consistent units (e.g., meters, seconds) to avoid scaling issues.
By carefully examining and adjusting the gravity magnitude, air resistance application, and timestep consistency, you should achieve a more realistic falling behavior for your player character.