Class PhysicsGm
- Namespace
- Gamem
- Assembly
- Gamem.dll
Provides static mathematical methods for basic physics calculations like gravity, friction, and movement interpolation.
public static class PhysicsGm
- Inheritance
-
PhysicsGm
- Inherited Members
Methods
AddForce(double, double, double, double)
Applies a continuous force over a specified time duration to update the velocity, based on Newton's second law.
public static double AddForce(double v, double F, double t, double m)
Parameters
vdoubleThe initial velocity before the force is applied.
FdoubleThe force magnitude.
tdoubleThe duration of time over which the force acts.
mdoubleThe mass of the object. If mass is 0, the velocity remains unchanged to avoid division by zero.
Returns
- double
The updated velocity after applying the force.
AddForce(float, float, float, float)
Applies a continuous force over a specified time duration to update the velocity, based on Newton's second law.
public static float AddForce(float v, float F, float t, float m)
Parameters
vfloatThe initial velocity before the force is applied.
FfloatThe force magnitude.
tfloatThe duration of time over which the force acts.
mfloatThe mass of the object. If mass is 0, the velocity remains unchanged to avoid division by zero.
Returns
- float
The updated velocity after applying the force.
AddImpulse(double, double, double)
Applies an instantaneous impulse to update the velocity.
public static double AddImpulse(double vOld, double J, double m)
Parameters
vOlddoubleThe pre-impulse velocity.
JdoubleThe impulse magnitude (change in momentum).
mdoubleThe mass of the object. If mass is 0, the velocity remains unchanged to avoid division by zero.
Returns
- double
The updated velocity after the impulse is applied.
AddImpulse(float, float, float)
Applies an instantaneous impulse to update the velocity.
public static float AddImpulse(float vOld, float J, float m)
Parameters
vOldfloatThe pre-impulse velocity.
JfloatThe impulse magnitude (change in momentum).
mfloatThe mass of the object. If mass is 0, the velocity remains unchanged to avoid division by zero.
Returns
- float
The updated velocity after the impulse is applied.
ApplyFriction(double, double, double)
Applies friction to reduce velocity towards zero over a specified time step.
public static double ApplyFriction(double velocity, double frictionCoeff, double deltaTime)
Parameters
velocitydoubleThe current velocity.
frictionCoeffdoubleThe friction coefficient representing deceleration per second.
deltaTimedoubleThe time elapsed since the last frame in seconds.
Returns
- double
The updated velocity after friction is applied, clamping to 0.0 if it changes direction.
ApplyFriction(float, float, float)
Applies friction to reduce velocity towards zero over a specified time step.
public static float ApplyFriction(float velocity, float frictionCoeff, float deltaTime)
Parameters
velocityfloatThe current velocity.
frictionCoefffloatThe friction coefficient representing deceleration per second.
deltaTimefloatThe time elapsed since the last frame in seconds.
Returns
- float
The updated velocity after friction is applied, clamping to 0.0 if it changes direction.
ApplyGravity(double, double, double)
Applies gravity to the current vertical velocity over a specified time step.
public static double ApplyGravity(double velocity, double gravity, double deltaTime)
Parameters
velocitydoubleThe current velocity.
gravitydoubleThe acceleration due to gravity.
deltaTimedoubleThe time elapsed since the last frame in seconds.
Returns
- double
The updated velocity after applying gravity.
ApplyGravity(float, float, float)
Applies gravity to the current vertical velocity over a specified time step.
public static float ApplyGravity(float velocity, float gravity, float deltaTime)
Parameters
velocityfloatThe current velocity.
gravityfloatThe acceleration due to gravity.
deltaTimefloatThe time elapsed since the last frame in seconds.
Returns
- float
The updated velocity after applying gravity.
ApplyQuadraticDrag(double, double, double)
Applies quadratic drag (air resistance) to a velocity value over a given time step using Euler integration.
public static double ApplyQuadraticDrag(double v, double k, double t)
Parameters
vdoubleThe current velocity.
kdoubleThe quadratic drag coefficient (typically dependent on fluid density, drag coefficient, and cross-sectional area).
tdoubleThe time step (delta time) in seconds.
Returns
- double
The updated velocity after quadratic drag has been applied.
ApplyQuadraticDrag(float, float, float)
Applies quadratic drag (air resistance) to a velocity value over a given time step using Euler integration.
public static float ApplyQuadraticDrag(float v, float k, float t)
Parameters
vfloatThe current velocity.
kfloatThe quadratic drag coefficient (typically dependent on fluid density, drag coefficient, and cross-sectional area).
tfloatThe time step (delta time) in seconds.
Returns
- float
The updated velocity after quadratic drag has been applied.
Bounce(double, double, double)
Calculates the velocity after a bounce collision, reversing direction and applying a coefficient of restitution.
public static double Bounce(double vOld, double bounciness, double minBounceThreshold = 0.1)
Parameters
vOlddoubleThe pre-collision velocity.
bouncinessdoubleThe bounciness coefficient (restitution), clamped between 0.0 and 1.0.
minBounceThresholddoubleThe minimum velocity required to sustain a bounce. Below this magnitude, the velocity is clamped to zero to prevent endless micro-bouncing.
Returns
- double
The updated velocity after the bounce, or 0.0 if the resulting speed falls below the threshold.
Bounce(float, float, float)
Calculates the velocity after a bounce collision, reversing direction and applying a coefficient of restitution.
public static float Bounce(float vOld, float bounciness, float minBounceThreshold = 0.1)
Parameters
vOldfloatThe pre-collision velocity.
bouncinessfloatThe bounciness coefficient (restitution), clamped between 0.0 and 1.0.
minBounceThresholdfloatThe minimum velocity required to sustain a bounce. Below this magnitude, the velocity is clamped to zero to prevent endless micro-bouncing.
Returns
- float
The updated velocity after the bounce, or 0.0 if the resulting speed falls below the threshold.
CalculateCentripetalAcceleration(double, double)
Calculates the centripetal acceleration of an object moving along a circular path.
public static double CalculateCentripetalAcceleration(double speed, double radius)
Parameters
Returns
- double
The centripetal acceleration, or zero if
radiusis zero.
CalculateCentripetalAcceleration(float, float)
Calculates the centripetal acceleration of an object moving along a circular path.
public static float CalculateCentripetalAcceleration(float speed, float radius)
Parameters
Returns
- float
The centripetal acceleration, or zero if
radiusis zero.
CalculateJumpVelocity(double, double)
Calculates the initial upward velocity required to reach a specific jump height under a given gravity.
public static double CalculateJumpVelocity(double h, double g)
Parameters
hdoubleThe target maximum jump height.
gdoubleThe gravity value. Its absolute value is used to ensure mathematical stability.
Returns
- double
The calculated initial jump velocity required to reach the target height.
CalculateJumpVelocity(float, float)
Calculates the initial upward velocity required to reach a specific jump height under a given gravity.
public static float CalculateJumpVelocity(float h, float g)
Parameters
hfloatThe target maximum jump height.
gfloatThe gravity value. Its absolute value is used to ensure mathematical stability.
Returns
- float
The calculated initial jump velocity required to reach the target height.
CalculateLaunchVelocity(double, double, double, double)
Calculates the required initial velocity along a single axis to reach a target displacement in a given time under constant acceleration.
public static double CalculateLaunchVelocity(double target, double start, double g, double t)
Parameters
targetdoubleThe target position to reach.
startdoubleThe starting position.
gdoubleThe constant acceleration along this axis (e.g., gravity).
tdoubleThe desired time to reach the target in seconds.
Returns
- double
The required initial velocity, or zero if
tis zero.
CalculateLaunchVelocity(float, float, float, float)
Calculates the required initial velocity along a single axis to reach a target displacement in a given time under constant acceleration.
public static float CalculateLaunchVelocity(float target, float start, float g, float t)
Parameters
targetfloatThe target position to reach.
startfloatThe starting position.
gfloatThe constant acceleration along this axis (e.g., gravity).
tfloatThe desired time to reach the target in seconds.
Returns
- float
The required initial velocity, or zero if
tis zero.
CalculateTimeToApex(double, double)
Calculates the time required for a projectile to reach the apex (highest point) of its trajectory under constant gravity.
public static double CalculateTimeToApex(double velocityY, double gravity)
Parameters
velocityYdoubleThe initial vertical velocity vector component.
gravitydoubleThe acceleration due to gravity. Its absolute value is used to prevent negative time results.
Returns
- double
The calculated time to reach the maximum height in seconds.
CalculateTimeToApex(float, float)
Calculates the time required for a projectile to reach the apex (highest point) of its trajectory under constant gravity.
public static float CalculateTimeToApex(float velocityY, float gravity)
Parameters
velocityYfloatThe initial vertical velocity vector component.
gravityfloatThe acceleration due to gravity. Its absolute value is used to prevent negative time results.
Returns
- float
The calculated time to reach the maximum height in seconds.
ClampVelocity(double, double)
Clamps a velocity value to be within a symmetric range defined by a maximum speed limit.
public static double ClampVelocity(double v, double max)
Parameters
vdoubleThe current velocity to clamp.
maxdoubleThe maximum allowed speed (magnitude), which will be used to define both upper and lower bounds.
Returns
- double
The clamped velocity, constrained between -abs(max) and abs(max).
ClampVelocity(float, float)
Clamps a velocity value to be within a symmetric range defined by a maximum speed limit.
public static float ClampVelocity(float v, float max)
Parameters
vfloatThe current velocity to clamp.
maxfloatThe maximum allowed speed (magnitude), which will be used to define both upper and lower bounds.
Returns
- float
The clamped velocity, constrained between -abs(max) and abs(max).
Drag(double, double, double, double, double)
Applies linear drag to 3D velocity components over a given time step, stopping the movement along XY if the 3D velocity magnitude drops below a small threshold.
public static (double x, double y, double z) Drag(double velocityX, double velocityY, double velocityZ, double drag, double deltaTime)
Parameters
velocityXdoubleThe X component of the velocity.
velocityYdoubleThe Y component of the velocity.
velocityZdoubleThe Z component of the velocity.
dragdoubleThe drag coefficient influencing the exponential decay speed.
deltaTimedoubleThe time elapsed since the last frame in seconds.
Returns
- (double x, double y, double z)
A tuple containing the updated X and Y velocity components, or zeros if the total 3D speed is below the threshold.
Drag(Vector3, float, float)
Applies linear drag to a 3D velocity vector over a given time step, stopping the object completely if its speed drops below a small threshold.
public static Vector3 Drag(Vector3 velocity, float drag, float deltaTime)
Parameters
velocityVector3The current velocity vector.
dragfloatThe drag coefficient (typically negative to slow the object down, or positive depending on the expected math convention).
deltaTimefloatThe time elapsed since the last frame in seconds.
Returns
- Vector3
The updated velocity vector after drag has been applied, or Zero if the squared velocity is below 0.0001f.
Drag(float, float, float, float, float)
Applies linear drag to 3D velocity components over a given time step, stopping the movement along XY if the 3D velocity magnitude drops below a small threshold.
public static (float x, float y, float z) Drag(float velocityX, float velocityY, float velocityZ, float drag, float deltaTime)
Parameters
velocityXfloatThe X component of the velocity.
velocityYfloatThe Y component of the velocity.
velocityZfloatThe Z component of the velocity.
dragfloatThe drag coefficient influencing the exponential decay speed.
deltaTimefloatThe time elapsed since the last frame in seconds.
Returns
- (float x, float y, float z)
A tuple containing the updated X and Y velocity components, or zeros if the total 3D speed is below the threshold.
GetStoppingDistance(double, double)
Calculates the stopping distance required for an object to come to a complete stop from a given velocity under constant deceleration.
public static double GetStoppingDistance(double v, double a)
Parameters
vdoubleThe current velocity of the object.
adoubleThe constant deceleration rate (magnitude of acceleration slowing the object down).
Returns
- double
The calculated stopping distance, or zero if the deceleration is zero.
GetStoppingDistance(float, float)
Calculates the stopping distance required for an object to come to a complete stop from a given velocity under constant deceleration.
public static float GetStoppingDistance(float v, float a)
Parameters
vfloatThe current velocity of the object.
afloatThe constant deceleration rate (magnitude of acceleration slowing the object down).
Returns
- float
The calculated stopping distance, or zero if the deceleration is zero.
JumpCut(double, double)
Reduces upward velocity when a jump button is released early, commonly used for variable jump heights in platformers.
public static double JumpCut(double v, double multiplier)
Parameters
vdoubleThe current vertical velocity.
multiplierdoubleThe factor by which to multiply the velocity (usually between 0.0 and 1.0).
Returns
- double
The modified velocity if moving upward (greater than 0), otherwise the original velocity.
JumpCut(float, float)
Reduces upward velocity when a jump button is released early, commonly used for variable jump heights in platformers.
public static float JumpCut(float v, float multiplier)
Parameters
vfloatThe current vertical velocity.
multiplierfloatThe factor by which to multiply the velocity (usually between 0.0 and 1.0).
Returns
- float
The modified velocity if moving upward (greater than 0), otherwise the original velocity.
MoveTowards(double, double, double)
Moves a value toward a target value by a maximum specified delta.
public static double MoveTowards(double current, double target, double maxDelta)
Parameters
currentdoubleThe current value.
targetdoubleThe value to move towards.
maxDeltadoubleThe maximum amount by which the value can change.
Returns
- double
The new value closer to the target, or the target itself if it is within range.
MoveTowards(float, float, float)
Moves a value toward a target value by a maximum specified delta.
public static float MoveTowards(float current, float target, float maxDelta)
Parameters
currentfloatThe current value.
targetfloatThe value to move towards.
maxDeltafloatThe maximum amount by which the value can change.
Returns
- float
The new value closer to the target, or the target itself if it is within range.
PredictTrajectory(Vector2, Vector2, Vector2, float)
Predicts the 2D position of a projectile at a given time under constant acceleration (gravity).
public static Vector2 PredictTrajectory(Vector2 startPos, Vector2 startVelocity, Vector2 gravity, float t)
Parameters
startPosVector2The initial position vector.
startVelocityVector2The initial velocity vector.
gravityVector2The acceleration vector (such as gravity).
tfloatThe time elapsed since the start of the trajectory in seconds.
Returns
PredictTrajectory(float, float, float, float, float, float, float)
Predicts the 2D position coordinates of a projectile at a given time under constant acceleration (gravity).
public static (float x, float y) PredictTrajectory(float startPosX, float startPosY, float startVelocityX, float startVelocityY, float gravityX, float gravityY, float t)
Parameters
startPosXfloatThe initial X-coordinate position.
startPosYfloatThe initial Y-coordinate position.
startVelocityXfloatThe initial velocity along the X-axis.
startVelocityYfloatThe initial velocity along the Y-axis.
gravityXfloatThe acceleration along the X-axis.
gravityYfloatThe acceleration along the Y-axis.
tfloatThe time elapsed since the start of the trajectory in seconds.
Returns
TerminalVelocity(double, double)
Caps falling velocity to prevent an object from exceeding a maximum downward terminal velocity limit.
public static double TerminalVelocity(double v, double vlimit)
Parameters
vdoubleThe current vertical velocity (negative values represent falling).
vlimitdoubleThe maximum allowed falling speed magnitude (should be positive).
Returns
- double
The clamped velocity, restricted so that it does not drop below -vlimit.
TerminalVelocity(float, float)
Caps falling velocity to prevent an object from exceeding a maximum downward terminal velocity limit.
public static float TerminalVelocity(float v, float vlimit)
Parameters
vfloatThe current vertical velocity (negative values represent falling).
vlimitfloatThe maximum allowed falling speed magnitude (should be positive).
Returns
- float
The clamped velocity, restricted so that it does not drop below -vlimit.