is my air resistance calculation wrong? Here original: float fSpeedMagnitude = m_vecMoveSpeed.Magnitude() * m_fAirResistance;
if (CCullZones::DoExtraAirResistanceForPlayer())
{
if (IsVehicle())
{
CVehicle* vehicle = this->AsVehicle();
if (vehicle->IsAutomobile() || vehicle->IsSubBike())
fSpeedMagnitude = CVehicle::m_fAirResistanceMult * fSpeedMagnitude;
}
}
m_vecMoveSpeed *= pow(1.0f - fSpeedMagnitude, CTimer::GetTimeStep());
m_vecTurnSpeed *= 0.99f; and here my:
float magnitudeSqr = glm::length2(m_moveSpeed);
float f = powf(1.0f / abs(m_fAirResistance * 0.5f * magnitudeSqr + 1.0f), CTimer::GetTimeStep());
m_moveSpeed *= f; (ignore vehicle check)
Assistant
Your air resistance calculation has some notable differences compared to the original implementation, which could lead to incorrect behavior depending on the intended physics model. Let's break down both versions to identify potential issues and understand how they differ.
The air resistance (fSpeedMagnitude) is calculated as the product of the speed magnitude and a constant (m_fAirResistance), meaning it scales linearly with speed.
Conditional Scaling for Vehicles:
Additional scaling is applied for certain vehicle types using CVehicle::m_fAirResistanceMult.
Multiplicative Damping:
The move speed is scaled by (1.0f - fSpeedMagnitude) raised to the power of the timestep, effectively reducing the speed based on air resistance each frame.
Turn Speed Damping:
Independently, turn speed is damped by a fixed factor (0.99f).
You're using the squared magnitude of the speed (magnitudeSqr = glm::length2(m_moveSpeed)) and scaling it with 0.5f * m_fAirResistance.
Air resistance in your model scales quadratically with speed, which is more physically accurate for many real-world scenarios but differs from the original implementation.
Inverse Relationship:
The damping factor f is calculated as 1.0f / (|0.5f * m_fAirResistance * magnitudeSqr + 1.0f|) raised to the timestep.
This introduces an inverse relationship, meaning as speed increases, the damping factor decreases, which might not behave as intended in every timestep.
No Vehicle Checks:
You've omitted the vehicle-specific adjustments present in the original code, which might be critical depending on the context of your application.
Potential Issues with Your Approach
Inconsistent Air Resistance Model:
The original code uses a linear model for air resistance, while your version uses a quadratic model. If the rest of your system is calibrated for linear air resistance, this discrepancy can lead to unexpected behavior.
Damping Behavior:
The multiplicative factor in the original (1.0f - fSpeedMagnitude) is straightforward and ensures that speed decreases consistently proportionally to the air resistance.
Your factor 1.0f / (1.0f + 0.5f * m_fAirResistance * magnitudeSqr) could lead to non-linear damping that might be too aggressive or insufficient, especially at different speed magnitudes.
Potential Division by Zero:
Although unlikely due to the +1.0f, using division introduces a different mathematical behavior compared to the original subtraction method.
Abs Function Use:
The abs function ensures the denominator is positive, but depending on the context, this might mask issues where m_fAirResistance could negatively affect the calculation.
Recommendations
Align with the Original Model:
If the intention is to replicate or build upon the original behavior, you should use a linear dependence on speed. Modify your calculation to use magnitude instead of magnitudeSqr.
Decide whether you need linear or quadratic air resistance based on your simulation's requirements. If quadratic air resistance is desired, ensure that all parts of your system are calibrated accordingly.
Incorporate Vehicle-Specific Adjustments:
If applicable, reintroduce the vehicle checks to maintain the intended behavior for different entity types.
Validate Stability:
Test your implementation across a range of speeds to ensure that the damping behaves as expected without introducing instability or excessive damping.
Consider Using Force-Based Air Resistance:
Alternatively, you might model air resistance as a force applied against the direction of motion, which can be more intuitive and easier to manage in some physical simulations.
Example of a Quadratic Air Resistance Implementation
If you intend to use a quadratic model, here's an example approach:
cpp
// Calculate the drag force magnitude (F_d = 0.5 * C_d * rho * A * v^2)
float magnitudeSqr = glm::length2(m_moveSpeed);
float dragFactor = 0.5f * m_fAirResistance * magnitudeSqr;
// Calculate the damping factor based on drag
float f = powf(1.0f - dragFactor, CTimer::GetTimeStep());
// Ensure the damping factor doesn't become negative
f = glm::max(f, 0.0f);
// Apply damping to move speed
m_moveSpeed *= f;
Ensure that m_fAirResistance is appropriately scaled to prevent dragFactor from becoming too large, which could result in negative damping factors.
Conclusion
Your current implementation introduces a quadratic dependence with an inverse relationship, differing significantly from the original linear model. Depending on your simulation's requirements, this could be incorrect or lead to unintended behaviors. It's essential to align your air resistance calculation with the intended physics model and ensure consistency across your system.