Untitled
Loading...
Searching...
No Matches
Physics System

All of the required structures for the Physics system. More...

Data Structures

struct  CollisionPoints
 Points that track collision data such as direction, penetration depth, and more to generate Manifolds and resolve impulses. More...
struct  Manifold
 Persistent Manifolds that track collisions and how to resolve them. More...
struct  ConvexHull
 Convex Hulls that are generated via incremental hull for non-static meshes. More...
struct  TriangleGroup
 Groups of contiguous triangles with the same plane that are combined via jarvis marching. More...
struct  CapsuleData
 The capsule data for collisions. More...
struct  HullData
 Hull data that refers to ConvexHulls of Meshes. More...
struct  CollisionData
 The tagged union for collision type, stored in PhysicsComponent. More...
union  CollisionData::[union].data
struct  PhysicsComponent
 Entity Component used for physics resolution. More...

Macros

#define MAX_PLAYER_CONTACTS   10
 Maximum amount of CollisionPoints to consider per frame.

Enumerations

enum  CollisionType { COL_TRIANGLE , COL_HULL , COL_CAPSULE }
 Defines collision types with different behaviors and mesh setup and validation. More...
enum  PhysicsType { PHYSICS_NONE , PHYSICS_FULL , PHYSICS_NO_ROT , PHYSICS_GRAVITY_ONLY }
 Defines physics types with different behaviors in impulse resolution. More...

Functions

void EPA_check_face_visibility (vec3 p, EPAWorkspace *EW)
 Checks what faces are visible from given point (by dotting the normal).
void EPA_check_horizon_edges (MeshFace *face, EPAWorkspace *EW)
 Counts edges of each visible face and tests to see if they have matched already within the horizon_edges arraw.
bool EPA_add_horizon_face_to_hull_faces (size_t p_idx, HorizonEdge e, EPAWorkspace *EW, vec3 center)
 Adds a horizon face to to the main faces array, while making sure the normal is facing outward.
void EPA_expand_hull (vec3 p, int index, EPAWorkspace *EW, vec3 centroid)
 Conducts EPA on a given hull with a support point.
vec3 support_capsule (CapsuleData *cap, vec3 dir, mat4 inv)
 The support function for a COL_CAPSULE.
void support (vec3 *v, size_t *index, vec3 *verts, size_t cnt, mat4 inv, vec3 dir)
 The support function for a ConvexHull or triangle Mesh.
SupportVertex support_diff (CollisionData a_cd, CollisionData b_cd, mat4 a_mat, mat4 b_mat, mat4 a_inv, mat4 b_inv, vec3 dir)
 Finds support points for A and B and then finds the difference.
CollisionPoints epa (CollisionData a_cd, CollisionData b_cd, mat4 a_mat, mat4 b_mat, mat4 a_inv, mat4 b_inv, Simplex *s, EPAWorkspace *EW)
 The Expanding Polytope Algorithm - expands a polytope in a loop as it becomes a polyhedron.
void add_point_to_simplex (Simplex *s, SupportVertex p)
 Adds a point to the simplex by placing it at the bottom and shifting the other points forward.
void remove_point_from_simplex (Simplex *s, int i)
 Adds a point to the simplex by placing it at the bottom and shifting the other points forward.
void replace_simplex_points (Simplex *s, SupportVertex abcd[4], int count)
 Replaces Simplex with another array of SupportVertex.
vec3 find_next_direction (Simplex *sn)
 Finds next direction in GJK() by pointing away from current points.
bool GJK (CollisionData a_cd, CollisionData b_cd, mat4 a_mat, mat4 b_mat, mat4 a_inv, mat4 b_inv, CollisionPoints *collision_points, EPAWorkspace *EW)
 GJK polytope construction based on A and B (which can be any CollisionType), calls EPA internally.
void PC_apply_velocity (PhysicsComponent *pc, float dt)
 Applies gravity to a PhysicsComponent.
int generate_capsule_hull_manifold (CapsuleData *cap, mat4 cap_transform, vec3 hull_normal, float hull_plane_dist, Manifold *manifold, PhysicsComponent *pcA, PhysicsComponent *pcB)
 Generates a manifold between a capsule and a hull plane.
void clip_manifold (Manifold *manifold, CollisionPoints *p, CollisionData a_cd, mat4 modelA, CollisionData b_cd, mat4 modelB)
 Clips a manifold based on two different sets of points and faces with separate transforms.
Manifold * find_manifold_for_pair (int mn0, int mn1)
 Finds a current Manifold if there is one from MANIFOLDS for a given pair of MeshNodes.
Manifold * create_new_manifold (PhysicsComponent *a, int mn0, PhysicsComponent *b, int mn1)
 Finds a free Manifold and initializes it between two MeshNodes.
bool PC_process_collision_node (MeshNode *mn, PhysicsComponent *pc, Mesh *m0, MeshNode *mn0, BVHNode *node, CollisionData cd, mat4 mat, mat4 mat0, mat4 mat_inv, mat4 mat0_inv, Manifold *manifold)
 Processes a collision against one BVHNode inside of a COL_TRIANGLE static MeshNode.
bool PC_traverse_collision_bvh (MeshNode *mn, PhysicsComponent *pc, Mesh *m0, MeshNode *mn0, BVHNode *nodes, CollisionData cd, mat4 mat, mat4 mat0, mat4 mat_inv, mat4 mat0_inv, AABB tr_aabb, Manifold *manifold)
 Traverses the BVH for a static MeshNode with a PhysicsComponent to detect collisions.
void PC_traverse_scene_bvh (MeshNode *mn, PhysicsComponent *pc, CollisionData cd, SceneData *sd, mat4 mat, mat4 mat_inv, AABB aabb)
 Traverses the scene BVH to detect collisions from a PhysicsComponent.
void search_collisions_from_pc (MeshNode *mn, PhysicsComponent *pc)
 Searches for collisions from a PhysicsComponent that has no corresponding Mesh.
void search_collisions_from_hull (MeshNode *mn, PhysicsComponent *pc)
 Searches for collisions from a PhysicsComponent that has a corresponding Mesh.
void PC_find_collision (MeshNode *mn, PhysicsComponent *pc)
 Searches for collisions from a PhysicsComponent and determines how based on if its corresponding Mesh exists or not.
void player_resolve_impulse_static (PhysicsComponent *pcA, CollisionPoints *p)
 Solves an impulse for a Player PhysicsComponent against a static Mesh.
void player_resolve_impulse (PhysicsComponent *pcA, PhysicsComponent *pcB, CollisionPoints *p)
 Solves an impulse for a Player PhysicsComponent against another PhysicsComponent.
void PC_resolve_impulse_static (PhysicsComponent *pcA, CollisionPoints *p)
 Solves an impulse for a PhysicsComponent against a static Mesh.
void PC_resolve_impulse (PhysicsComponent *pcA, PhysicsComponent *pcB, CollisionPoints *p)
 Solves an impulse for a PhysicsComponent against another.
void update_physics_components ()
 The main PhysicsComponent loop that determines velocity, solves collisions, then integrates position.
mat3 calc_inverse_inertia (PhysicsComponent *pc)
 Calculates the mat3 IIA for a PhysicsComponent, using AABB bounds and mass.
void init_PC (int mesh_node, PhysicsComponent *pc, AABB ai_aabb, CollisionType collision_type, PhysicsType physics_type)
 Initializes a PhysicsComponent.

Variables

float normal_impulse0 = 0.0f
float normal_impulse1 = 0.0f
float normal_impulse2 = 0.0f
float normal_impulse3 = 0.0f
vec3 torque_sum = (vec3){0,0,0}
vec3 friction_sum = (vec3){0,0,0}
vec3 impulse_sum = (vec3){0,0,0}
vec3 ang_impulse_sum = (vec3){0,0,0}

Detailed Description

All of the required structures for the Physics system.

The structures, functions, constants, and variables within the physics system.


Data Structure Documentation

◆ CollisionPoints

struct CollisionPoints

Points that track collision data such as direction, penetration depth, and more to generate Manifolds and resolve impulses.

See also
Manifold
MANIFOLDS
physics/physics.c

Definition at line 985 of file engine_types.h.

Data Fields
vec3 a Furthest point of A into B.
vec3 b Furthest point of B into A.
size_t index_a
size_t index_b
vec3 normal The collision normal from B into A.
vec3 tangent The tangent used for frictional impulse resolution.
float d Length of B-A.
bool collision
PhysicsComponent * pcA
int mnA
PhysicsComponent * pcB
int mnB
int incident_face
int reference_face
int edge_a
int edge_b
float normal_impulse
float bias_impulse
float tangent_impulse

◆ Manifold

struct Manifold

Persistent Manifolds that track collisions and how to resolve them.

Remarks
Stored in MANIFOLDS.
See also
physics/physics.c

Definition at line 1043 of file engine_types.h.

Data Fields
PhysicsComponent * a
int mnA
PhysicsComponent * b
int mnB
CollisionPoints contacts[4] The collision contacts.
int count The amount of valid contacts.
bool processed Tracks if a Manifold has been processed this frame yet.
bool free Tracks if a Manifold can be replaced.
float deepest Current deepest contact for a search.

◆ ConvexHull

struct ConvexHull

Convex Hulls that are generated via incremental hull for non-static meshes.

Used for collision.

See also
Mesh
init_mesh()

Definition at line 1107 of file engine_types.h.

Data Fields
Vector vertices Points that initially copy the Mesh vertices but then are deduplicated and modified in incremental_hull().
Vector faces MeshFaces that initially copy the Mesh faces but then are modified in incremental_hull().
Vector hull_faces HullFaces that are generated after incremental_hull() that combine contiguous triangles into singular faces.

Used for manifold clipping.

vec3 centroid The center of the hull.
AABB aabb The bounding box of the hull.

◆ TriangleGroup

struct TriangleGroup

Groups of contiguous triangles with the same plane that are combined via jarvis marching.

See also
compute_hull_faces()
compute_mesh_hulls()

Definition at line 1124 of file engine_types.h.

Data Fields
vec3 normal The group's normal.
vec3 ref_point A point on the plane.
Vector indices size_t indices of each MeshFace that indexes into ConvexHull.faces.

◆ CapsuleData

struct CapsuleData

The capsule data for collisions.

See also
PhysicsComponent
physics/physics.c
core/camera.c
init_PC()

Definition at line 2092 of file engine_types.h.

Data Fields
vec3 a The bottom of the capsule (based on AABB).
vec3 b The top of the capsule (based on AABB).
float radius The radius of the capsule.

◆ HullData

struct HullData

Hull data that refers to ConvexHulls of Meshes.

Populated during traversal and passed into GJK.

See also
PhysicsComponent
physics/physics.c
physics/GJK.c
core/camera.c
init_PC()

Definition at line 2110 of file engine_types.h.

Data Fields
vec3 * pts Points to ConvexHull.vertices.
size_t count Stored from ConvexHull.vertices.count.
HullFace * mfs Points to ConvexHull.hull_faces.
size_t face_count Stored from ConvexHull.hull_faces.count.

◆ CollisionData

struct CollisionData

The tagged union for collision type, stored in PhysicsComponent.

See also
PhysicsComponent
physics/physics.c
physics/GJK.c
core/camera.c
init_PC()

Definition at line 2129 of file engine_types.h.

Data Fields
CollisionType type
union data

◆ CollisionData::[union].data

union CollisionData::[union].data

Definition at line 2132 of file engine_types.h.

Data Fields
HullData hull
CapsuleData capsule

◆ PhysicsComponent

struct PhysicsComponent

Entity Component used for physics resolution.

See also
PHYSICS_COMPONENTS
physics/physics.c
physics/GJK.c
core/camera.c
init_PC()
update_physics_components()
Entity

Definition at line 2150 of file engine_types.h.

Data Fields
size_t id The ID in PHYSICS_COMPONENTS.
bool free If this is an empty component.
bool player_pc If this is the Player PhysicsComponent.
bool at_rest If this component is at rest it does not update.
int rest_counter The counter that decreases until at rest.
float mass Mass of the component.
float restitution The restitution (bounce).
float mu The friction constant for the component.
vec3 center_of_mass COM based on the AABB.
mat3 inverse_inertia IIA (currently just a calculated box).
vec3 vel Velocity.
vec3 old_vel Last step's velocity.
vec3 accel Acceleration from this step + acceleration from last step * ACCEL_BETA.
vec3 angular_velocity Rotational velocity.
vec3 old_angular Last step's angular velocity.
mat3 inverse_world PhysicsComponent.inverse_inertia transformed by rotation into world space for the current step.
AABB aabb Bounding box of the PC, determined based on Mesh or MeshNode.
CollisionData cd Tagged union for collision options.
PhysicsType physics_type Impulse resolution options.
Entity * ent Which Entity this component belongs to.

Macro Definition Documentation

◆ MAX_PLAYER_CONTACTS

#define MAX_PLAYER_CONTACTS   10

Maximum amount of CollisionPoints to consider per frame.

Definition at line 160 of file engine_types.h.

Referenced by player_find_collision(), and resolve_collisions().

Enumeration Type Documentation

◆ CollisionType

Defines collision types with different behaviors and mesh setup and validation.

See also
PhysicsComponent
physics/physics.c
core/camera.c
Enumerator
COL_TRIANGLE 

The triangle BVH is generated for this mesh and triangle tests are performed on it.

COL_HULL 

Incremental hull generates a ConvexHull which is used for collision tests.

COL_CAPSULE 

A capsule is constructed via AABB dimensions and is used for collision tests.

Definition at line 2057 of file engine_types.h.

◆ PhysicsType

Defines physics types with different behaviors in impulse resolution.

See also
PhysicsComponent
physics/physics.c
core/camera.c
Enumerator
PHYSICS_NONE 

Unused.

PHYSICS_FULL 

All impulse resolution options applied.

PHYSICS_NO_ROT 

Angular velocity is disabled.

PHYSICS_GRAVITY_ONLY 

Only gravity is applied and cannot be moved via external forces.

Definition at line 2073 of file engine_types.h.

Function Documentation

◆ EPA_check_face_visibility()

void EPA_check_face_visibility ( vec3 p,
EPAWorkspace * EW )

Checks what faces are visible from given point (by dotting the normal).

\((p - a) \cdot \vec n\)

Parameters
pThe point to check.
EWThe EPA workspace to store the visible indices into.

Definition at line 226 of file GJK.c.

References EPA_check_face_visibility(), EW, EPAWorkspace::faces, EPAWorkspace::indices, MeshFace::indices, EPAWorkspace::indices_count, MeshFace::normal, SupportVertex::p, vec3_dot(), vec3_sub(), EPAWorkspace::vertices, EPAWorkspace::visible_faces_count, and EPAWorkspace::visible_faces_indices.

Referenced by EPA_check_face_visibility(), and EPA_expand_hull().

◆ EPA_check_horizon_edges()

void EPA_check_horizon_edges ( MeshFace * face,
EPAWorkspace * EW )

Counts edges of each visible face and tests to see if they have matched already within the horizon_edges arraw.

Parameters
faceThe face to add.
EWThe EPA workspace to store the visible indices into.

Definition at line 243 of file GJK.c.

References HorizonEdge::ab, EPA_check_horizon_edges(), EW, EPAWorkspace::horizon_counts, EPAWorkspace::horizon_counts_count, EPAWorkspace::horizon_edges, EPAWorkspace::horizon_edges_count, MeshFace::indices, SupportVertex::p, vec3_equal(), and EPAWorkspace::vertices.

Referenced by EPA_check_horizon_edges(), and EPA_expand_hull().

◆ EPA_add_horizon_face_to_hull_faces()

bool EPA_add_horizon_face_to_hull_faces ( size_t p_idx,
HorizonEdge e,
EPAWorkspace * EW,
vec3 center )

Adds a horizon face to to the main faces array, while making sure the normal is facing outward.

Parameters
p_idxThe original support point index.
eThe horizon visible edge to add.
EWThe EPA workspace to store the visible indices into.
centerThe center of the EPA polyhedron.
Returns
True if the face was added correctly.

Definition at line 283 of file GJK.c.

References A, HorizonEdge::ab, B, C, compute_tri_normal(), EPA_add_horizon_face_to_hull_faces(), EW, EPAWorkspace::faces, EPAWorkspace::faces_count, EPAWorkspace::indices, MeshFace::indices, EPAWorkspace::indices_count, MeshFace::normal, SupportVertex::p, PRINT_VEC3N, vec3_dot(), vec3_length(), vec3_sub(), and EPAWorkspace::vertices.

Referenced by EPA_add_horizon_face_to_hull_faces(), and EPA_expand_hull().

◆ EPA_expand_hull()

void EPA_expand_hull ( vec3 p,
int index,
EPAWorkspace * EW,
vec3 centroid )

Conducts EPA on a given hull with a support point.

Parameters
pThe support point.
indexThe index of the support point.
EWThe EPA workspace to store the visible indices into.
centroidThe center of the polyhedron (hull).

Definition at line 326 of file GJK.c.

References EPA_add_horizon_face_to_hull_faces(), EPA_check_face_visibility(), EPA_check_horizon_edges(), EPA_expand_hull(), EW, EPAWorkspace::faces, EPAWorkspace::horizon_counts, EPAWorkspace::horizon_counts_count, EPAWorkspace::horizon_edges, EPAWorkspace::horizon_edges_count, EPAWorkspace::indices, EPAWorkspace::indices_count, REMOVE_FROM_ARR, EPAWorkspace::visible_faces_count, and EPAWorkspace::visible_faces_indices.

Referenced by epa(), and EPA_expand_hull().

◆ support_capsule()

vec3 support_capsule ( CapsuleData * cap,
vec3 dir,
mat4 inv )

The support function for a COL_CAPSULE.

Parameters
capThe CapsuleData inside of CollisionData.
dirThe support direction.
invThe inv matrix transform of this collision body.
Returns
The support point.

Definition at line 376 of file GJK.c.

References CapsuleData::a, CapsuleData::b, mat4_mul_direction(), CapsuleData::radius, support_capsule(), vec3_add(), vec3_dot(), vec3_length(), vec3_normalize(), and vec3_scale().

Referenced by support_capsule(), and support_diff().

◆ support()

void support ( vec3 * v,
size_t * index,
vec3 * verts,
size_t cnt,
mat4 inv,
vec3 dir )

The support function for a ConvexHull or triangle Mesh.

\begin{equation*} S_A(v) = \left\lbrace \begin{aligned} \vec d &= \begin{bmatrix}\vec{v_x} \\ \vec{v_y} \\ \vec{v_z} \\ 0\end{bmatrix} \\ \vec d\prime &= Md \\ & \text{arg} \max\limits_{p \in A}(p \cdot d\prime) \end{aligned} \right. \text{Where A is the shape whose points, p, are tested against direction d.} \end{equation*}

Parameters
vThe pointer to the resulting support vertex.
indexThe pointer to the resulting index of the support vertex.
vertsThe vertices to consider.
cntThe amount of vertices to consider.
invThe inv matrix transform of this collision body.
dirThe support direction.

Definition at line 393 of file GJK.c.

References mat4_mul_direction(), support(), and vec3_dot().

Referenced by epa(), GJK(), support(), and support_diff().

◆ support_diff()

SupportVertex support_diff ( CollisionData a_cd,
CollisionData b_cd,
mat4 a_mat,
mat4 b_mat,
mat4 a_inv,
mat4 b_inv,
vec3 dir )

Finds support points for A and B and then finds the difference.

\begin{equation*} p = M_a\begin{bmatrix}a_x \\ a_y \\ a_z \\ 1\end{bmatrix} - M_b\begin{bmatrix}b_x \\ b_y \\ b_z \\ 1\end{bmatrix} \end{equation*}

Parameters
a_cdA's CollisionData.
b_cdB's CollisionData.
a_matA's transform.
b_matB's transform.
a_invA's transform.
b_invB's transform.
dirThe support direction.
Returns
The resulting SupportVertex.

Definition at line 418 of file GJK.c.

References SupportVertex::a, SupportVertex::b, COL_CAPSULE, COL_HULL, COL_TRIANGLE, CollisionData::data, SupportVertex::index_a, SupportVertex::index_b, mat4_mul_point(), SupportVertex::p, support(), support_capsule(), support_diff(), CollisionData::type, vec3_scale(), and vec3_sub().

Referenced by epa(), GJK(), and support_diff().

◆ epa()

◆ add_point_to_simplex()

void add_point_to_simplex ( Simplex * s,
SupportVertex p )

Adds a point to the simplex by placing it at the bottom and shifting the other points forward.

Parameters
sThe current Simplex from GJK().
pThe SupportVertex to add.

Definition at line 755 of file GJK.c.

References A, add_point_to_simplex(), Simplex::count, and Simplex::points.

Referenced by add_point_to_simplex(), and GJK().

◆ remove_point_from_simplex()

void remove_point_from_simplex ( Simplex * s,
int i )

Adds a point to the simplex by placing it at the bottom and shifting the other points forward.

Parameters
sThe current Simplex from GJK().
pThe SupportVertex to add.

Definition at line 772 of file GJK.c.

References Simplex::count, Simplex::points, and remove_point_from_simplex().

Referenced by find_next_direction(), and remove_point_from_simplex().

◆ replace_simplex_points()

void replace_simplex_points ( Simplex * s,
SupportVertex abcd[4],
int count )

Replaces Simplex with another array of SupportVertex.

(Used to swap in the final GJK cases.)

Parameters
sThe current Simplex from GJK().
abcdThe SupportVertex array that replaces s.
countThe amount of abcd points.

Definition at line 788 of file GJK.c.

References Simplex::count, Simplex::points, and replace_simplex_points().

Referenced by find_next_direction(), and replace_simplex_points().

◆ find_next_direction()

vec3 find_next_direction ( Simplex * sn)

Finds next direction in GJK() by pointing away from current points.

Todo

Case 2: Notate direction vector crossing with safe axis

Remove simplex centroid and d, unneeded

Redundant code here, test and remove

\begin{equation*} \vec v = \begin{cases} -S_a, & \text{if } C = 1\\ \left. \begin{aligned} \mathit{AB} &= S_b - S_a, \\ \mathit{AO} &= -S_a, \\ & \begin{cases} \left. \begin{aligned} & \text{REMOVE}(B) \\ & \mathit{AO} \end{aligned} \right\}, & \text{if } \mathit{AB} \cdot \mathit{AO} \lt 0 \\ \begin{aligned} & (\mathit{AB} \times \mathit{AO}) \times \mathit{AB}, \\ \end{aligned} & \text{otherwise} \\ \end{cases} \end{aligned} \right\}, & \text{if } C = 2 \\ \left. \begin{aligned} \vec{N} &= (S_B - S_a) \times (S_c - S_a) \\ \hat{N} &= \frac{\vec{N}}{||\vec{N}||} \\ \mathit{AB} &= S_b - S_a \\ \mathit{AC} &= S_c - S_a \\ \mathit{AO} &= -S_a \\ \mathit{AB}_p &= \mathit{AB} \times \hat{N} \\ \mathit{AC}_p &= \hat{N} \times \mathit{AC} \\ \mathit{AC}_d &= \mathit{AC}_p \cdot \mathit{AO} \\ & \left. \begin{aligned} & \begin{cases} & \left. \begin{aligned} & \text{REMOVE}(B) \\ & (\mathit{AC} \times \mathit{AO}) \times \mathit{AC} \\ \end{aligned} \right\}, & \text{if } \mathit{AC} \cdot \mathit{AO} \gt 0 \\ & (\mathit{AB} \times \mathit{AO}) \times \mathit{AB}, & \text{otherwise} \\ \end{cases} \\ \end{aligned} \right\}, & \text{if } \mathit{AC}_d \gt 0 \\ \mathit{AB}_d &= \mathit{AB}_p \cdot \mathit{AO} \\ &\left. \begin{aligned} & \text{REMOVE}(S_c) \\ & (\mathit{AB} \times \mathit{AO}) \times \mathit{AB} \\ \end{aligned} \right\}, & \text{if } \mathit{AB}_d \gt 0 \text{ and } \mathit{AB} \cdot \mathit{AO} \gt 0 \\ & \left\lbrace \begin{aligned} & \hat{N}, & \text{if } \hat{N} \cdot \mathit{AO} \gt 0 \\ & \left. \begin{aligned} & (S_c, S_b) \mapsto (S_b, S_c) \\ & -\hat{N} \\ \end{aligned} \right\}, & \text{otherwise} \\ \end{aligned} \right. \\ \end{aligned} \right\}, & \text{if } C = 3 \\ \left. \begin{aligned} \mathit{AO} &= -S_a \\ \mathit{ABC} &= (S_b - S_a) \times (S_c - S_a) \\ &\left. \begin{aligned} & \text{REMOVE}(D) \\ & \mathit{ABC} \end{aligned} \right\}, & \text{if } \mathit{ABC} \cdot \mathit{AO} \gt 0 \\ \mathit{ACD} &= (S_c - S_a) \times (S_d - S_a) \\ &\left. \begin{aligned} & \text{REMOVE}(B) \\ & \mathit{ACD} \end{aligned} \right\}, & \text{if } \mathit{ACD} \cdot \mathit{AO} \gt 0 \\ \mathit{ADB} &= (S_d - S_a) \times (S_b - S_a) \\ &\left. \begin{aligned} & (S_b, S_c) \mapsto (S_d, S_b) \\ & C = C - 1 \\ & \mathit{ADB} \\ \end{aligned} \right\}, & \text{if } \mathit{ADB} \cdot \mathit{AO} \gt 0 \\ \end{aligned} \right\}, & \text{if } C = 4 \\ \end{cases} \\ \begin{aligned} \vec{v} &= \vec v_\perp, & \text{if } \vec{v} \text{ has not been assigned.} \\ & \text{Where } v_\perp \text{ is the sentinel value that signals a complete simplex.} \\ \end{aligned} \end{equation*}

Parameters
sThe current Simplex from GJK().
Returns
The resulting direction.

Definition at line 799 of file GJK.c.

References A, B, C, Simplex::centroid, compute_tri_normal(), Simplex::count, D, find_next_direction(), SupportVertex::p, Simplex::points, remove_point_from_simplex(), replace_simplex_points(), vec3_add(), vec3_cross(), vec3_dot(), vec3_length(), vec3_scale(), vec3_sub(), vec3::x, vec3::y, and vec3::z.

Referenced by find_next_direction(), and GJK().

◆ GJK()

bool GJK ( CollisionData a_cd,
CollisionData b_cd,
mat4 a_mat,
mat4 b_mat,
mat4 a_inv,
mat4 b_inv,
CollisionPoints * collision_points,
EPAWorkspace * EW )

GJK polytope construction based on A and B (which can be any CollisionType), calls EPA internally.

See also
epa()
support_diff()
find_next_direction()

\begin{align} v &= \begin{bmatrix} 0 \\ 0\\ 1 \end{bmatrix}\\ c &= 0 \\ & \text{while } c = 0 \text{:} \\
& \quad p = S(A, B) \\ & \quad c = \text{DNE}, & \text{if } p \cdot v \leq 0 \text{ or } p \in s.\\ & \quad s \Vert p \\ & \quad v = D(s, v) \\ & \quad c = 1, & \text{if } v = v_\perp \\ \end{align} \(\text{Where } c \text{ is the collision status and s is the simplex.}\)

Parameters
a_cdCollisionData for A.
b_cdCollisionData for B.
a_matTransformation for A.
b_matTransformation for B.
a_invInverse transformation for A.
b_invInverse transformation for B.
collision_pointsPointer to the outgoing collision data.
EWThe workspace to conduct EPA in.
Returns
If collision is detected through EPA.

Definition at line 1052 of file GJK.c.

References add_point_to_simplex(), collision(), CollisionPoints::collision, Simplex::count, CollisionPoints::d, epa(), EW, find_next_direction(), GJK(), CollisionPoints::normal_impulse, SupportVertex::p, Simplex::points, support(), support_diff(), CollisionPoints::tangent_impulse, vec3_dot(), vec3_equal(), vec3::x, vec3::y, and vec3::z.

Referenced by GJK(), PC_process_collision_node(), PC_traverse_scene_bvh(), and player_find_collision().

◆ PC_apply_velocity()

void PC_apply_velocity ( PhysicsComponent * pc,
float dt )

Applies gravity to a PhysicsComponent.

Parameters
pcThe PhysicsComponent to modify.
dtThe physics timestep.

Definition at line 148 of file physics.c.

References PhysicsComponent::angular_velocity, dt, PC_apply_velocity(), vec3_scale(), PhysicsComponent::vel, vec3::x, vec3::y, and vec3::z.

Referenced by PC_apply_velocity(), and update_physics_components().

◆ generate_capsule_hull_manifold()

int generate_capsule_hull_manifold ( CapsuleData * cap,
mat4 cap_transform,
vec3 hull_normal,
float hull_plane_dist,
Manifold * manifold,
PhysicsComponent * pcA,
PhysicsComponent * pcB )

Generates a manifold between a capsule and a hull plane.

Note
Unused, just using single point penetrations for capsules since this was misbehaving.

Definition at line 172 of file physics.c.

References CapsuleData::a, CollisionPoints::a, CapsuleData::b, CollisionPoints::b, Manifold::contacts, Manifold::count, CollisionPoints::d, generate_capsule_hull_manifold(), mat4_mul_point(), CollisionPoints::normal, CollisionPoints::pcA, CollisionPoints::pcB, CapsuleData::radius, vec3_dot(), vec3_scale(), and vec3_sub().

Referenced by generate_capsule_hull_manifold().

◆ clip_manifold()

◆ find_manifold_for_pair()

Manifold * find_manifold_for_pair ( int mn0,
int mn1 )

Finds a current Manifold if there is one from MANIFOLDS for a given pair of MeshNodes.

Parameters
mn0MeshNode A.
mn1MeshNode B.
Returns
The Manifold if found, NULL if not.

Definition at line 594 of file physics.c.

References find_manifold_for_pair(), MANIFOLDS, MAX_MANIFOLDS, Manifold::mnA, and Manifold::mnB.

Referenced by find_manifold_for_pair(), PC_process_collision_node(), and PC_traverse_scene_bvh().

◆ create_new_manifold()

Manifold * create_new_manifold ( PhysicsComponent * a,
int mn0,
PhysicsComponent * b,
int mn1 )

Finds a free Manifold and initializes it between two MeshNodes.

Parameters
aThe PhysicsComponent for A, if there is one.
mn0The MeshNode for A, if there is one.
bThe PhysicsComponent for B, if there is one.
mn1The MeshNode for B, if there is one.
Returns
The Manifold if free slot found, NULL if not.

Definition at line 611 of file physics.c.

References Manifold::a, PhysicsComponent::at_rest, Manifold::b, Manifold::count, create_new_manifold(), Manifold::deepest, Manifold::free, MANIFOLDS, MAX_MANIFOLDS, Manifold::mnA, Manifold::mnB, and Manifold::processed.

Referenced by create_new_manifold(), PC_process_collision_node(), and PC_traverse_scene_bvh().

◆ PC_process_collision_node()

bool PC_process_collision_node ( MeshNode * mn,
PhysicsComponent * pc,
Mesh * m0,
MeshNode * mn0,
BVHNode * node,
CollisionData cd,
mat4 mat,
mat4 mat0,
mat4 mat_inv,
mat4 mat0_inv,
Manifold * manifold )

Processes a collision against one BVHNode inside of a COL_TRIANGLE static MeshNode.

Parameters
mnThe MeshNode that is colliding against the static MeshNode.
pcThe PhysicsComponent that is colliding against the static MeshNode.
m0The static MeshNodes Mesh.
mn0The static MeshNode.
nodeThe current BVHNode.
cdThe colliding shape's CollisionData.
matThe colliding shape's transformation matrix.
mat0The static shape's transformation matrix.
mat_invThe colliding shape's inverse transformation matrix.
mat0_invThe static shape's inverse transformation matrix.
manifoldThe pair's Manifold.
Returns
True if there is a collision detected.

Definition at line 638 of file physics.c.

References A, B, Mesh::bvh_indices, C, CAST_VEC, clip_manifold(), COL_CAPSULE, COL_TRIANGLE, CollisionPoints::collision, Manifold::contacts, BVHNode::count, HullFace::count, Manifold::count, create_new_manifold(), CollisionPoints::d, CollisionData::data, Manifold::deepest, EW, find_manifold_for_pair(), BVHNode::first_index, GJK(), MeshNode::global_matrix, MeshNode::id, HullFace::indices, MeshFace::indices, CollisionPoints::mnA, CollisionPoints::mnB, CollisionPoints::normal, HullFace::normal, MeshFace::normal, CollisionPoints::normal_impulse, PC_process_collision_node(), CollisionPoints::pcA, CollisionPoints::pcB, Point::pos, Manifold::processed, CollisionPoints::tangent_impulse, Mesh::tris, CollisionData::type, and Mesh::vertices.

Referenced by PC_process_collision_node(), and PC_traverse_collision_bvh().

◆ PC_traverse_collision_bvh()

bool PC_traverse_collision_bvh ( MeshNode * mn,
PhysicsComponent * pc,
Mesh * m0,
MeshNode * mn0,
BVHNode * nodes,
CollisionData cd,
mat4 mat,
mat4 mat0,
mat4 mat_inv,
mat4 mat0_inv,
AABB tr_aabb,
Manifold * manifold )

Traverses the BVH for a static MeshNode with a PhysicsComponent to detect collisions.

Parameters
mnThe MeshNode that is detecting against the static MeshNode.
pcThe PhysicsComponent that is detecting against the static MeshNode.
m0The static MeshNodes Mesh.
mn0The static MeshNode.
nodesThe BVHNodes of the static MeshNode.
cdThe detecting shape's CollisionData.
matThe detecting shape's transformation matrix.
mat0The static shape's transformation matrix.
mat_invThe detecting shape's inverse transformation matrix.
mat0_invThe static shape's inverse transformation matrix.
tr_aabbThe world space AABB of the detecting shape to test against BVHNodes.
manifoldThe pair's Manifold.
Returns
True if there is a collision detected.

Definition at line 717 of file physics.c.

References BVHNode::aabb, aabbs_intersect(), BVHNode::left, BVHNode::next, PC_process_collision_node(), PC_traverse_collision_bvh(), and BVHNode::right.

Referenced by PC_traverse_collision_bvh(), and PC_traverse_scene_bvh().

◆ PC_traverse_scene_bvh()

void PC_traverse_scene_bvh ( MeshNode * mn,
PhysicsComponent * pc,
CollisionData cd,
SceneData * sd,
mat4 mat,
mat4 mat_inv,
AABB aabb )

Traverses the scene BVH to detect collisions from a PhysicsComponent.

Parameters
mnThe MeshNode that is detecting.
pcThe PhysicsComponent that is detecting.
cdThe detecting shape's CollisionData.
sdThe SceneData that contains the BVH.
matThe detecting shape's transformation matrix.
mat_invThe detecting shape's inverse transformation matrix.
aabbThe local space AABB of the detecting shape to test against BVHNodes.

Definition at line 752 of file physics.c.

References BVHNode::aabb, aabbs_intersect(), CollisionPoints::bias_impulse, Vector::buffer, SceneData::bvh_indices, SceneData::bvh_nodes, CAST_VEC, PhysicsComponent::cd, clip_manifold(), COL_CAPSULE, COL_HULL, COL_TRIANGLE, CollisionPoints::collision, Manifold::contacts, BVHNode::count, Manifold::count, Vector::count, create_new_manifold(), CollisionPoints::d, CollisionData::data, EW, find_manifold_for_pair(), BVHNode::first_index, MeshNode::free, get_root_node(), GJK(), MeshNode::global_matrix, GLOBALS, ConvexHull::hull_faces, Mesh::hulls, MeshNode::id, BVHNode::left, MeshNode::local_id, m4_inverse(), MeshNode::mesh, Globals::mesh_nodes, Globals::meshes, CollisionPoints::mnA, CollisionPoints::mnB, BVHNode::next, Mesh::nodes, CollisionPoints::normal_impulse, MeshNode::parent, MeshNode::pc, PC_traverse_collision_bvh(), PC_traverse_scene_bvh(), CollisionPoints::pcA, CollisionPoints::pcB, Manifold::processed, BVHNode::right, Mesh::static_mesh, CollisionPoints::tangent_impulse, transform_aabb_by_model_matrix(), CollisionData::type, and ConvexHull::vertices.

Referenced by PC_traverse_scene_bvh(), search_collisions_from_hull(), and search_collisions_from_pc().

◆ search_collisions_from_pc()

void search_collisions_from_pc ( MeshNode * mn,
PhysicsComponent * pc )

◆ search_collisions_from_hull()

◆ PC_find_collision()

void PC_find_collision ( MeshNode * mn,
PhysicsComponent * pc )

Searches for collisions from a PhysicsComponent and determines how based on if its corresponding Mesh exists or not.

Parameters
mnThe MeshNode that is detecting.
pcThe PhysicsComponent that is detecting.

Definition at line 1066 of file physics.c.

References CAST_VEC, PhysicsComponent::cd, GLOBALS, MeshNode::mesh, Globals::mesh_nodes, Globals::meshes, PC_find_collision(), search_collisions_from_hull(), and search_collisions_from_pc().

Referenced by PC_find_collision(), and update_physics_components().

◆ player_resolve_impulse_static()

◆ player_resolve_impulse()

◆ PC_resolve_impulse_static()

◆ PC_resolve_impulse()

◆ update_physics_components()

void update_physics_components ( )

The main PhysicsComponent loop that determines velocity, solves collisions, then integrates position.

See also
PC_traverse_collision_bvh()

\begin{align} & \text{Def:} \\ & \quad v = \text{Velocity} \\ & \quad G = \text{Gravity} \\ & \quad \omega = \text{Angular Velocity} \\ & \quad p = \text{Position} \\ & \quad r = \text{Rotation} \\ & \quad s = \text{Scale} \\ & \quad \mathit{IIA, IIW} = \text{Inverse Inertia} \\ & \quad M = \text{Transformation} \\ & \text{Let } C = \text{ all valid PhysicsComponents and } B = \text{ all valid Manifolds} \\ a(c) &= \begin{cases} v &= (v - G \cdot \Delta t) \cdot \beta \\ \omega &= \omega_{n} \cdot \alpha \end{cases} \\ C\prime &= a(C) = \lbrace a(x) | x \in C\rbrace \\ q(Q) &= \begin{cases} \begin{bmatrix} 1 - 2Q_y^2 - 2Q_z^2 & 2Q_xQ_y - 2Q_wQ_z & 2Q_xQ_z + 2Q_wQ_y \\ 2Q_xQ_y + 2Q_wQ_z & 1 - 2Q_x^2 - 2Q_z^2 & 2Q_yQ_z - 2Q_wQ_x \\ 2Q_xQ_z - 2Q_wQ_y & 2Q_yQ_z + 2Q_wQ_x & 1 - 2Q_x^2 - 2Q_y^2 \end{bmatrix} \end{cases} \\ t(c) &= \begin{cases} p = p + v\Delta t \\ \theta = || \omega || \cdot \Delta t \\ \left. \begin{cases} k = \frac{\omega}{||\omega||}\\ Q = \begin{pmatrix} k_x \cdot \sin \frac{\theta}{2}, k_y \cdot \sin \frac{\theta}{2}, k_z \cdot \sin \frac{\theta}{2}, \cos \frac{\theta}{2} \end{pmatrix}\\ r = \frac{Q r}{||Q r||}\\ \end{cases}\right\rbrace, & \text{if } \theta \gt 0 \\ M = \text{TRS}(p, r, s) \\ R = q(r) \\ \mathit{IIW} = (R \cdot IIA) \cdot R \\
\mathit{AABB}_{tr} = \mathit{AABB} \cdot M \end{cases} \\ t(C\prime) &= \lbrace t(x) | x \in C\prime\rbrace \\ L(c) &= \begin{cases} \text{for } y = 0 \text{ to } C_{\text{max}} - 1: \\ \quad B[c_{i}, t(C\prime)[y]_{i}] = \mathit{AABB}_{tr}(c) \cap \mathit{AABB}_{tr}(t(C\prime)[y]), & \text{if } \mathit{AABB}_{tr}(c) \cap \mathit{AABB}_{tr}(t(C\prime)[y]) \neq \emptyset \\ \end{cases} \\ B &= \lbrace L(x) | x \in t(C\prime)\rbrace \\ f(n, c_a, c_b) &= \begin{cases} R = l \times n \\ I = \mathit{IIW} \cdot R \\ T = I \times l \\ \psi = n \cdot T \\ \frac{1}{m_a} + \frac{1}{m_b} + \psi_a + \psi_b \\ \end{cases} \\ H(b) &= \begin{cases} p_i = a \cap b \\ \text{Where } p_i = \text{ simultaneously the penetration of } b_a \text{ into } b_b \text{ and also } b_b \text{ into } b_a \\ \mathcal P = M \begin{bmatrix} p_{ix} \\ p_{iy} \\ p_{iz} \\ 1\end{bmatrix} \\ \mathcal M = M \begin{bmatrix} c_x \\ c_y \\ c_z \\ 1\end{bmatrix} \\ l = \mathcal P - \mathcal M \\ V = v - \omega \times l \\ \phi = V_a - V_b \\ \mathcal L = \vec \phi \cdot \vec N \\ \text{when } \mathcal L \in (-\infty, 0)\text{:} \\ \quad K = f(\vec N, b_a, b_b) \\ \quad J = \frac{-(1 + \mathcal{R}) \cdot \mathcal L}{K} \\ \quad B_{F}\prime = \max(B_F + J, 0) \\ \quad J_F = B_F\prime - B_F \\ \quad F_\parallel = J_F \vec{N} \\ \quad v = v \pm F_\parallel \frac{1}{m} \\ \quad \omega = \omega + \mathit{IIW} \cdot (l \times \pm F_\parallel) \\ \quad V = V + \omega \times l \\ \quad \phi = V_a - V_b \\ \quad \mathcal L = \vec \phi \cdot \vec N \\ \quad V_t = \phi - \mathcal L \vec N \\ \quad V_l = || V_t || \\ \quad \text{when } V_l \in (0, \infty)\text{:} \\ \qquad \hat t = \frac{V_t}{V_l} \\ \qquad K = f(\hat t, b_a, b_b) \\ \qquad J_t = \frac{-(\phi \cdot \hat t)}{K} \\ \qquad \gamma = \mu_a B_F \\ \qquad B_T\prime = \min(\max(B_T + J_t, -\gamma), \gamma) \\ \qquad J_{tF} = B_T\prime - B_T \\ \qquad F_\perp = J_{tF} \hat t \\ \qquad v = v \pm F_\perp \frac{1}{m} \\ \qquad \omega = \omega + \mathit{IIW} \cdot (l \times \pm F_\perp) \\ \end{cases} \\ & \text {Where } \pm \text{ relates even and opposite forces and where H(b) modifies both respective bodies in } C\prime \\ H(B) &= \lbrace H(x) | x \in B\rbrace \\ C\prime\prime &= t(C\prime) = \lbrace t(x) | x \in C\prime\rbrace \\ S(b) &= \begin{cases} p = M \begin{bmatrix} p_x \\ p_y \\ p_z \\ 1\end{bmatrix} \\ d = p_a - p_b \\ s = d \cdot \vec N \\ u = \begin{cases} \left. \begin{bmatrix} 0 \\ 0 \\ 1 \end{bmatrix}\right\rbrace, & \text{if } \vec{N}_z \lt 0.9\\ \left. \begin{bmatrix} 1 \\ 0 \\ 0 \end{bmatrix}\right\rbrace, & \text{otherwise} \end{cases} \\ T = \frac{u \times \vec N}{|| u \times \vec N||} \\ D = d \cdot T \\ B \setminus \lbrace b \rbrace, & \text{ if } s \gt 0.01 \text{ or } D \gt 0.01 \end{cases} \\ B\prime &= S(B) = \lbrace S(x) | x \in B\rbrace \\ \end{align}

Definition at line 1724 of file physics.c.

References CollisionPoints::a, Manifold::a, MeshNode::aabb, aabb_to_vertices(), PhysicsComponent::accel, ACCEL_BETA, ang_impulse_sum, PhysicsComponent::angular_velocity, PhysicsComponent::at_rest, CollisionPoints::b, Manifold::b, CAST_VEC, PhysicsComponent::cd, COL_CAPSULE, DebugVertex::color, compose_trs_from_mesh_node(), compose_trs_from_vectors(), Manifold::contacts, Manifold::count, Vector::count, CollisionPoints::d, CollisionData::data, DEBUG_LINE_COUNT, DEBUG_LINES, Manifold::deepest, dt, PhysicsComponent::ent, Entity::free, Manifold::free, friction_sum, MeshNode::global_matrix, GLOBALS, impulse_sum, PhysicsComponent::inverse_inertia, PhysicsComponent::inverse_world, Light::light_type, Globals::lights, MANIFOLDS, mat3_mul(), mat3_transpose(), mat4_mul_point(), MAX_LIGHTS, MAX_MANIFOLDS, MAX_PHYSICS_COMPONENTS, MeshNode::mesh, Entity::mesh_node, Globals::mesh_nodes, Globals::meshes, CollisionPoints::mnA, CollisionPoints::mnB, CollisionPoints::normal, normal_impulse0, normal_impulse1, normal_impulse2, normal_impulse3, PhysicsComponent::old_angular, PhysicsComponent::old_vel, PC_apply_velocity(), PC_find_collision(), PC_resolve_impulse(), PC_resolve_impulse_static(), CollisionPoints::pcA, CollisionPoints::pcB, PHYSICS_COMPONENTS, PHYSICS_FULL, PhysicsComponent::physics_type, POINT_LIGHT, DebugVertex::pos, Light::pos, Manifold::processed, quat_from_axis_angle(), quat_mul(), quat_to_mat3(), Light::radius, rand(), render_mesh_aabbs_from_root(), PhysicsComponent::rest_counter, rot(), MeshNode::rotation, MeshNode::scale, Light::sm_dirty, test_sphere_aabb(), torque_sum, transform_aabb_by_model_matrix(), MeshNode::translation, CollisionData::type, update_physics_components(), vec3_add(), vec3_cross(), vec3_dot(), vec3_length(), vec3_normalize(), vec3_scale(), vec3_sub(), vec4_normalize(), PhysicsComponent::vel, vec3::x, vec3::y, and vec3::z.

Referenced by main(), and update_physics_components().

◆ calc_inverse_inertia()

mat3 calc_inverse_inertia ( PhysicsComponent * pc)

Calculates the mat3 IIA for a PhysicsComponent, using AABB bounds and mass.

Returns
The inverse inertia matrix.

Definition at line 2115 of file physics.c.

References mat3::a0, mat3::a1, mat3::a2, PhysicsComponent::aabb, mat3::b0, mat3::b1, mat3::b2, mat3::c0, mat3::c1, mat3::c2, calc_inverse_inertia(), AABB::extents, PhysicsComponent::mass, vec3::x, vec3::y, and vec3::z.

Referenced by calc_inverse_inertia(), init_PC(), and render_entity_menu().

◆ init_PC()

void init_PC ( int mesh_node,
PhysicsComponent * pc,
AABB ai_aabb,
CollisionType collision_type,
PhysicsType physics_type )

Initializes a PhysicsComponent.

Parameters
mesh_nodeThe top-most MeshNode this corresponds to.
pcPointer to the PC to initialize.
ai_aabbThe AABB over the entire AssetInstance (unused).
collision_typeIndicates what to use for collision detection.
physics_typeIndicates what to compute during impulse resolution.
Note
Uses an AABB to determine collision. If the MeshNode refers to a Mesh, then it uses that AABB. Else, it uses the MeshNode AABB, which recursively includes all children MeshNodes.

Definition at line 2141 of file physics.c.

References Mesh::aabb, MeshNode::aabb, PhysicsComponent::aabb, PhysicsComponent::angular_velocity, calc_inverse_inertia(), CAST_VEC, PhysicsComponent::cd, AABB::center, PhysicsComponent::center_of_mass, COL_CAPSULE, COL_HULL, COL_TRIANGLE, CollisionData::data, GLOBALS, init_PC(), PhysicsComponent::inverse_inertia, PhysicsComponent::mass, AABB::max_z, MeshNode::mesh, Globals::mesh_nodes, Globals::meshes, AABB::min_z, PhysicsComponent::mu, PhysicsComponent::physics_type, PhysicsComponent::player_pc, print_aabb(), PhysicsComponent::restitution, CollisionData::type, PhysicsComponent::vel, vec3::x, and vec3::y.

Referenced by create_physics_component(), and init_PC().

Variable Documentation

◆ normal_impulse0

float normal_impulse0 = 0.0f

Definition at line 1088 of file physics.c.

Referenced by update_physics_components().

◆ normal_impulse1

float normal_impulse1 = 0.0f

Definition at line 1089 of file physics.c.

Referenced by update_physics_components().

◆ normal_impulse2

float normal_impulse2 = 0.0f

Definition at line 1090 of file physics.c.

Referenced by update_physics_components().

◆ normal_impulse3

float normal_impulse3 = 0.0f

Definition at line 1091 of file physics.c.

Referenced by update_physics_components().

◆ torque_sum

vec3 torque_sum = (vec3){0,0,0}

Definition at line 1092 of file physics.c.

Referenced by update_physics_components().

◆ friction_sum

vec3 friction_sum = (vec3){0,0,0}

Definition at line 1093 of file physics.c.

Referenced by update_physics_components().

◆ impulse_sum

vec3 impulse_sum = (vec3){0,0,0}

Definition at line 1094 of file physics.c.

Referenced by update_physics_components().

◆ ang_impulse_sum

vec3 ang_impulse_sum = (vec3){0,0,0}

Definition at line 1095 of file physics.c.

Referenced by PC_resolve_impulse_static(), and update_physics_components().