Originally Posted by
aybe
The drag-related topic if I'm correct would require writing physics from scratch, right ? If yes then it is beyond my competence at all as I can't even get a simple formula to code
No you don't need to do it. You must set just the physics engine own drag to zero, and then for every time step as first step:
- Get the current world space linear velocity of the current rigid body
- Transform the world space linear velocity into local body space
- Calculate the drag force vector with the drag equation formula Fd = 0.5 * p * u² * Cd * A on basis of this local body space linear velocity
- Transform the local body space drag force vector into world space back
- Apply the now world space drag force to the rigid body
For example, in my own physics engine called Kraft the code (where x is side, y is up, z is forward) looks like this:
Code:
const FluidMassDensity = 1.29; // Air density in kg/m³
DragCoefficient : TKraftVector3 = (x : 0.01; y : 0.005; z : 0.005);
ReferenceArea : TKraftVector3 = (x : 1.5; // Side area in m²
y : 1.5; // Top-view area in m²
z : 1.5); // Frontal area in m²
procedure DragEquation;
var LocalFlowVelocity, LocalFlowVelocitySignedSquared, DragForce : TKraftVector3;
begin
// Code for the drag equation formula: Fd = 0.5 * p * u² * Cd * A (is actually dimensionless, but it is applied dimension-axis-wise here in this case)
LocalFlowVelocity := Vector3TermMatrixMulTransposedBasis(PhysicsRigidBody.LinearVelocity, PhysicsRigidBody.WorldTransform);
LocalFlowVelocitySignedSquared.x := LocalFlowVelocity.x * abs(LocalFlowVelocity.x);
LocalFlowVelocitySignedSquared.y := LocalFlowVelocity.y * abs(LocalFlowVelocity.y);
LocalFlowVelocitySignedSquared.z := LocalFlowVelocity.z * abs(LocalFlowVelocity.z);
DragForce := Vector3ScalarMul(Vector3Mul(LocalFlowVelocitySignedSquared, Vector3Mul(DragCoefficient, ReferenceArea)), -(0.5 * FluidMassDensity));
PhysicsRigidBody.AddBodyForce(DragForce, kfmAcceleration); // incl. back-transform from local body space into world space
// so it equals to: PhysicsRigidBody.AddWorldForce(Vector3TermMatrixMulBasis(PhysicsRigidBody.LinearVelocity, PhysicsRigidBody.WorldTransform), kfmAcceleration);
end;
// Vector3TermMatrixMulBasis is a vector-matrix-multiplication of a 3D vector and the 3x3 sub-basis-matrix (rotation part) of a 4x4 matrix
// Vector3TermMatrixMulTransposedBasis is a vector-matrix-multiplication of a 3D vector and the transposed 3x3 sub-basis-matrix (rotation part) of a 4x4 matrix (faster than to inverse the matrix first, but it will work only for normalized orthogonal matrices without scaling (so rotation+translation only))
Originally Posted by
aybe
BTW why do suggest a different drag for forward, is it for coping with high speeds ?
Because the handling of the ship will be then more natural, especially the acceleration and braking, without affecting steering+rolling and pitch.