Nudge Physics
A single file, header-only 3D physics library
Loading...
Searching...
No Matches
Math Functions

General-Math Functions. Functions without the nm_ prefix take as arguments nudge structures (e.g. nudge::Transform), and some of them were present in the original nudge release. The other functions, the ones with the nm_ prefix, have been added to ease user experience, or to support some extended feature, and are more generic. More...

Functions

Transformnudge::Mat4WithoutScalingToTransform (Transform *Tout, const float *matrix16WithoutScaling)
 Converts a column-major 16-floats matrix without any scaling applied to a nudge::Transform.
 
Transform nudge::Mat4WithoutScalingToTransform (const float *matrix16WithoutScaling)
 Converts a column-major 16-floats matrix without any scaling applied to a nudge::Transform.
 
float * nudge::TransformToMat4 (float *matrix16Out, const Transform *T)
 Converts a nudge::Transform to a column-major 16-floats matrix.
 
void nudge::TransformAssignToBody (context_t *c, unsigned body, Transform newT, float deltaTime, int16_t aux_body=-1)
 Assigns a new Transform to a body, and sets its linear and angular velocities based on the differences between the new transform and the old one: this is essential when manually moving bodies by changing their transform.
 
void nudge::TransformAdvanceBodyFromVelocities (context_t *c, unsigned body, float deltaTime)
 Advances the body's transform based on its linear and angular velocities.
 
Transform nudge::TransformSlerp (Transform T0, Transform T1, float time)
 Applies (spherical) lerp between T0 and T1.
 
Transform nudge::TransformMul (Transform T0, Transform T1)
 Multiplies two transforms.
 
void nudge::nm_QuatAdvance (float *__restrict qOut4, const float *__restrict q4, const float *__restrict angVel3, float halfTimeStep)
 Advances a quaternion given an angular velocity and a (small) time step.
 
float * nudge::nm_QuatFromMat4 (float *__restrict result4, const float *__restrict m16)
 Turns the 3x3 submatrix of a 16-floats column-major matrix (without scaling applied) into a quaternion.
 
float * nudge::nm_Mat4SetRotationFromQuat (float *__restrict result16, const float *__restrict q4)
 Replaces the 3x3 submatrix of a 16-floats column-major matrix with the 3x3 matrix representing the given quaternion.
 
float * nudge::nm_QuatFromMat3 (float *__restrict result4, const float *__restrict m9)
 Turns the 3x3 9-floats column-major rotation matrix (without scaling applied) into a quaternion.
 
float * nudge::nm_Mat3FromQuat (float *__restrict result9, const float *__restrict q4)
 Converts the given quaternion the a 3x3 9-floats column-major rotation matrix.
 
void nudge::nm_QuatGetAngularVelocity (float *__restrict angVel3, const float *newQuat4, const float *oldQuat4, float halfTimeStep)
 Given an old and a new quaternion and a small time step, it calculates the angular velocities.
 
float * nudge::nm_QuatSlerp (float *__restrict result4, const float *__restrict a4, const float *__restrict b4, float slerpTime_In_0_1, int normalizeResult4AfterLerp)
 Performs a spherical lerp between two quaternions (in 4-floats format)
 
float * nudge::nm_QuatMul (float *qOut4, const float *a4, const float *b4)
 Multiplies two 4-floats quaternions.
 
void nudge::nm_QuatNormalize (float *__restrict q4)
 normalizes a 4-floats quaternion in place
 
float * nudge::nm_QuatFromAngleAxis (float *__restrict qOut4, float rfAngle, float rkAxisX, float rkAxisY, float rkAxisZ)
 Generates a 4-floats quaternion based on an orientation arouns an axis.
 
void nudge::nm_QuatToAngleAxis (const float *__restrict q4, float *__restrict rfAngleOut1, float *__restrict rkAxisOut3)
 Calculates the angle-axis representation of the given 4-float quaternion.
 
float nudge::nm_Vec3Normalize (float *__restrict v3)
 Normalizes a 3-floats vector in place.
 
float nudge::nm_Vec3Normalized (float *__restrict v3Out, const float *__restrict v3)
 Get a normalizes copy of an input 3-floats vector.
 
float nudge::nm_Vec3Dot (const float *__restrict a3, const float *__restrict b3)
 Computes the dot product between two 3-floats vectors.
 
float * nudge::nm_Vec3Cross (float *__restrict vOut3, const float *__restrict a3, const float *__restrict b3)
 Computes the cross product between two 3-floats vectors.
 
float * nudge::nm_QuatMulVec3 (float *__restrict vOut3, const float *__restrict q4, const float *__restrict vIn3)
 Transforms a 3-floats vector by a 4-floats quaternion.
 
float * nudge::nm_QuatGetAxis (float *__restrict vOut3, const float *__restrict q4, float axisX, float axisY, float axisZ)
 Transforms a particular axis from the input quaternion space to word space.
 
float * nudge::nm_QuatGetAxisX (float *__restrict axisOut3, const float *__restrict q4)
 
float * nudge::nm_QuatGetAxisY (float *__restrict axisOut3, const float *__restrict q4)
 
float * nudge::nm_QuatGetAxisZ (float *__restrict axisOut3, const float *__restrict q4)
 
float * nudge::nm_QuatRotate (float *__restrict qInOut4, float angle, float axisX, float axisY, float axisZ)
 Rotates an input 4-floats unit quaternion by an angle in radians around a specified axis.
 
float * nudge::nm_Mat4Mul (float *result16, const float *ml16, const float *mr16)
 Multiplies two column-major 16-floats 4x4 matrices.
 

Detailed Description

General-Math Functions. Functions without the nm_ prefix take as arguments nudge structures (e.g. nudge::Transform), and some of them were present in the original nudge release. The other functions, the ones with the nm_ prefix, have been added to ease user experience, or to support some extended feature, and are more generic.

Function Documentation

◆ Mat4WithoutScalingToTransform() [1/2]

Transform * nudge::Mat4WithoutScalingToTransform ( Transform Tout,
const float *  matrix16WithoutScaling 
)

Converts a column-major 16-floats matrix without any scaling applied to a nudge::Transform.

Parameters
Toutthe output nudge::Transform
matrix16WithoutScalingthe input column-major 16-floats matrix without any scaling applied
Returns
the same as Tout (for chaining the call only)

◆ Mat4WithoutScalingToTransform() [2/2]

Transform nudge::Mat4WithoutScalingToTransform ( const float *  matrix16WithoutScaling)

Converts a column-major 16-floats matrix without any scaling applied to a nudge::Transform.

Parameters
matrix16WithoutScalingthe input column-major 16-floats matrix without any scaling applied
Returns
the output nudge::Transform (by value)

◆ TransformToMat4()

float * nudge::TransformToMat4 ( float *  matrix16Out,
const Transform T 
)

Converts a nudge::Transform to a column-major 16-floats matrix.

Parameters
matrix16Outthe output column-major 16-floats matrix
Tthe input nudge::Transform
Returns
the same as matrix16Out (for chaining the call only)

◆ TransformAssignToBody()

void nudge::TransformAssignToBody ( context_t c,
unsigned  body,
Transform  newT,
float  deltaTime,
int16_t  aux_body = -1 
)

Assigns a new Transform to a body, and sets its linear and angular velocities based on the differences between the new transform and the old one: this is essential when manually moving bodies by changing their transform.

Parameters
cthe nudge context
bodythe target body index
newTthe new transform
deltaTimethe (small) time difference in seconds (used to calculate linear and angular velocities: v = space/time)
aux_body[experimental] if the aux_body index is >=0, then additional linear and angular velocities are calculated and summed, so that the aux_body 'should' generate a 'dragging' effect on 'body' (the idea was to experiment with a kind of platform-convey effect that does not depend on friction only)

◆ TransformAdvanceBodyFromVelocities()

void nudge::TransformAdvanceBodyFromVelocities ( context_t c,
unsigned  body,
float  deltaTime 
)

Advances the body's transform based on its linear and angular velocities.

Parameters
cthe nudge context
bodythe target body index
deltaTimethe (small) time difference in seconds

◆ TransformSlerp()

Transform nudge::TransformSlerp ( Transform  T0,
Transform  T1,
float  time 
)

Applies (spherical) lerp between T0 and T1.

Parameters
T0
T1
timemust be in the [0,1] interval
Returns
the resulting transform

◆ TransformMul()

Transform nudge::TransformMul ( Transform  T0,
Transform  T1 
)

Multiplies two transforms.

Parameters
T0
T1
Returns
the result of the multiplication

◆ nm_QuatAdvance()

void nudge::nm_QuatAdvance ( float *__restrict  qOut4,
const float *__restrict  q4,
const float *__restrict  angVel3,
float  halfTimeStep 
)

Advances a quaternion given an angular velocity and a (small) time step.

Parameters
qOut4the resulting quaternion in 4-floats format
q4the input quaternion in 4-floats format
angVel3the angular velocity in 3-floats format
halfTimeStepthe half of the delta time: must be small for this function to work
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_QuatFromMat4()

float * nudge::nm_QuatFromMat4 ( float *__restrict  result4,
const float *__restrict  m16 
)

Turns the 3x3 submatrix of a 16-floats column-major matrix (without scaling applied) into a quaternion.

Parameters
result4the resulting quaternion in 4-floats format
m16the input 16-floats column-major matrix (without scaling applied)
Returns
same as result4 (for chaining the call only)
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_Mat4SetRotationFromQuat()

float * nudge::nm_Mat4SetRotationFromQuat ( float *__restrict  result16,
const float *__restrict  q4 
)

Replaces the 3x3 submatrix of a 16-floats column-major matrix with the 3x3 matrix representing the given quaternion.

Parameters
result16the input/output 16-floats column-major matrix
q4the input quaternion in 4-floats format
Returns
same as result16 (for chaining the call only)
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_QuatFromMat3()

float * nudge::nm_QuatFromMat3 ( float *__restrict  result4,
const float *__restrict  m9 
)

Turns the 3x3 9-floats column-major rotation matrix (without scaling applied) into a quaternion.

Parameters
result4the resulting quaternion in 4-floats format
m9the input 9-floats column-major rotation matrix (without scaling applied)
Returns
same as result4 (for chaining the call only)
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_Mat3FromQuat()

float * nudge::nm_Mat3FromQuat ( float *__restrict  result9,
const float *__restrict  q4 
)

Converts the given quaternion the a 3x3 9-floats column-major rotation matrix.

Parameters
result9the input/output 9-floats column-major rotation matrix
q4the input quaternion in 4-floats format
Returns
same as result9 (for chaining the call only)
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_QuatGetAngularVelocity()

void nudge::nm_QuatGetAngularVelocity ( float *__restrict  angVel3,
const float *  newQuat4,
const float *  oldQuat4,
float  halfTimeStep 
)

Given an old and a new quaternion and a small time step, it calculates the angular velocities.

Parameters
angVel3the resulting angular velocity between the two quaternions
newQuat4the new quaternion
oldQuat4the old quaternion
halfTimeStephalf time step: must be small for this function to work
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_QuatSlerp()

float * nudge::nm_QuatSlerp ( float *__restrict  result4,
const float *__restrict  a4,
const float *__restrict  b4,
float  slerpTime_In_0_1,
int  normalizeResult4AfterLerp 
)

Performs a spherical lerp between two quaternions (in 4-floats format)

Parameters
result4the resulting quaternion
a4the first quaternion
b4the second quaternion
slerpTime_In_0_1time in [0,1] interval
normalizeResult4AfterLerpif not zero, when the function performs a lerp (because a4 and b4 are close enough) the resulting quaternion is normalized (default: 1)
Returns
same as result4 (for chaining the call only)
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_QuatMul()

float * nudge::nm_QuatMul ( float *  qOut4,
const float *  a4,
const float *  b4 
)

Multiplies two 4-floats quaternions.

Parameters
qOut4the result
a4the first input 4-floats quaternion
b4the second input 4-floats quaternion
Returns
same as qOut4 (for chaining the call only)
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_QuatNormalize()

void nudge::nm_QuatNormalize ( float *__restrict  q4)

normalizes a 4-floats quaternion in place

Parameters
q4the input/output 4-floats quaternion
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_QuatFromAngleAxis()

float * nudge::nm_QuatFromAngleAxis ( float *__restrict  qOut4,
float  rfAngle,
float  rkAxisX,
float  rkAxisY,
float  rkAxisZ 
)

Generates a 4-floats quaternion based on an orientation arouns an axis.

Parameters
qOut4the result
rfAnglethe input angle in radians
rkAxisXthe first component of the (normalized) axis
rkAxisYthe second component of the (normalized) axis
rkAxisZthe third component of the (normalized) axis
Returns
same as qOut4 (for chaining the call only)
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_QuatToAngleAxis()

void nudge::nm_QuatToAngleAxis ( const float *__restrict  q4,
float *__restrict  rfAngleOut1,
float *__restrict  rkAxisOut3 
)

Calculates the angle-axis representation of the given 4-float quaternion.

Parameters
q4input quaternion
rfAngleOut1output angle in radians
rkAxisOut3output 3-floats vector
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_Vec3Normalize()

float nudge::nm_Vec3Normalize ( float *__restrict  v3)

Normalizes a 3-floats vector in place.

Parameters
v3the input/output 3-floats vector
Returns
the length of the input 3-floats vector
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_Vec3Normalized()

float nudge::nm_Vec3Normalized ( float *__restrict  v3Out,
const float *__restrict  v3 
)

Get a normalizes copy of an input 3-floats vector.

Parameters
v3Outthe output 3-floats vector
v3the input 3-floats vector
Returns
the length of the input 3-floats vector (v3)
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_Vec3Dot()

float nudge::nm_Vec3Dot ( const float *__restrict  a3,
const float *__restrict  b3 
)

Computes the dot product between two 3-floats vectors.

Parameters
a3the first input 3-floats vector
b3the second input 3-floats vector
Returns
the resulting dot product

◆ nm_Vec3Cross()

float * nudge::nm_Vec3Cross ( float *__restrict  vOut3,
const float *__restrict  a3,
const float *__restrict  b3 
)

Computes the cross product between two 3-floats vectors.

Parameters
vOut3the output 3-floats vector
a3the first input 3-floats vector
b3the second input 3-floats vector
Returns
same as vOut3 (for chaining the call only)
Note
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_QuatMulVec3()

float * nudge::nm_QuatMulVec3 ( float *__restrict  vOut3,
const float *__restrict  q4,
const float *__restrict  vIn3 
)

Transforms a 3-floats vector by a 4-floats quaternion.

Parameters
vOut3the output 3-floats vector
q4the input 4-floats unit quaternion
vIn3the input 3-floats vector
Returns
same as vOut3 (for chaining the call only)
Note
it should be equivalent to: nm_QuatMul(vOut4,q4,vOut4);nm_QuatMul(vOut4,vOut4,qc4); where qc is q conjugate and vOut4 is {vOut3,0}
This function is never used in nudge.h
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_QuatGetAxis()

float * nudge::nm_QuatGetAxis ( float *__restrict  vOut3,
const float *__restrict  q4,
float  axisX,
float  axisY,
float  axisZ 
)

Transforms a particular axis from the input quaternion space to word space.

Parameters
vOut3the output 3-floats vector
q4the first input 4-floats unit quaternion
axisXthe first component of the input axis
axisYthe second component of the input axis
axisZthe third component of the input axis
Returns
same as vOut3 (for chaining the call only)
Note
if the input axis is not normalized, then user should call nm_Vec3Normalize(vOut3);
it should be equivalent to: nm_QuatMul(vOut4,q4,vOut4);nm_QuatMul(vOut4,vOut4,qc4); where qc is q conjugate and vOut4 is {vOut3,0}
This function is never used in nudge.h
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_QuatGetAxisX()

float * nudge::nm_QuatGetAxisX ( float *__restrict  axisOut3,
const float *__restrict  q4 
)
inline

◆ nm_QuatGetAxisY()

float * nudge::nm_QuatGetAxisY ( float *__restrict  axisOut3,
const float *__restrict  q4 
)
inline

◆ nm_QuatGetAxisZ()

float * nudge::nm_QuatGetAxisZ ( float *__restrict  axisOut3,
const float *__restrict  q4 
)
inline

◆ nm_QuatRotate()

float * nudge::nm_QuatRotate ( float *__restrict  qInOut4,
float  angle,
float  axisX,
float  axisY,
float  axisZ 
)

Rotates an input 4-floats unit quaternion by an angle in radians around a specified axis.

Parameters
qInOut4the input/output 4-floats unit quaternion
anglethe input angle in radians
axisXthe first component of the input axis
axisYthe second component of the input axis
axisZthe third component of the input axis
Returns
same as qInOut4 (for chaining the call only)
Note
This function is never used in nudge.h
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future

◆ nm_Mat4Mul()

float * nudge::nm_Mat4Mul ( float *  result16,
const float *  ml16,
const float *  mr16 
)

Multiplies two column-major 16-floats 4x4 matrices.

Parameters
result16the resulting column-major 16-floats 4x4 matrix (can be the same as one of the other arguments)
ml16the first column-major 16-floats 4x4 matrix (can be the same as one of the other arguments)
mr16the second column-major 16-floats 4x4 matrix (can be the same as one of the other arguments)
Returns
the same as result16 (for chaining effect only)
Note
This function is never used in nudge.h
Functions with the nm_ prefix have been imported by the minimath library (MIT license). These functions might be renamed, removed or hidden in the future