146 float cos_p = cosf(pitch);
157 if (fabsf(roll) > 0.0001f)
160 float cos_r = cosf(roll);
161 float sin_r = sinf(roll);
190 float rl = right - left;
191 float tb = top - bottom;
192 float fn = far - near;
211 out.
d0 = -(right + left) / rl;
212 out.
d1 = -(top + bottom) / tb;
213 out.
d2 = -(far + near) / fn;
230 float fov_rad = fov_deg * (
M_PI / 180.0f);
231 float f = 1.0f / tanf(fov_rad / 2.0f);
245 out.
c2 = -((far + near) / (far - near));
250 out.
d2 = -(2.0f * far * near) / (far - near);
306 float bounds = 40.0f;
315 vec3 negated_sun_dir = {
322 float fov_rad =
FOV * (
M_PI / 180.0f);
323 float near_height = tanf((
float)fov_rad / 2) * -10.0f;
325 float far_height = tanf((
float)fov_rad / 2) * 10.0f;
383 vec3 light_points[8];
394 float slice_center_dist = (20.0f - -20.0f) / 2.0f;
398 float radius = sqrtf(diff.
x*diff.
x + diff.
y*diff.
y + diff.
z*diff.
z);
400 float diameter = radius * 2.0f;
410 world_up = (
vec3){0, 0, 1};
423 float snapped_x = floorf(center_light_space.
x / texel_size) * texel_size;
424 float snapped_y = floorf(center_light_space.
y / texel_size) * texel_size;
426 float offset_x = snapped_x - center_light_space.
x;
427 float offset_y = snapped_y - center_light_space.
y;
429 float minx = -radius + offset_x;
430 float maxx = radius + offset_x;
431 float miny = -radius + offset_y;
432 float maxy = radius + offset_y;
434 float maxz = radius + 20.0f;
446 float best = INFINITY;
454 p->
d = -(pt_dist - 0.2f);
465 { 0.000000f, -0.333333f },
466 {-0.500000f, 0.333333f },
467 { 0.250000f, -0.777778f },
468 {-0.750000f, -0.111111f },
469 { 0.125000f, 0.555556f },
470 {-0.375000f, -0.555556f },
471 { 0.375000f, 0.111111f },
472 {-0.625000f, 0.777778f }
513 float jitter_x =
HALTON_8[sample_idx][0];
514 float jitter_y =
HALTON_8[sample_idx][1];
569 support_pos = right_leg->
pos;
571 support_pos = left_leg->
pos;
574 vec2 p2d = { player_pos.
x, player_pos.
y };
575 vec2 s2d = { support_pos.
x, support_pos.
y };
578 const float min_stretch = 0.15f;
579 const float max_stretch = 0.4f;
581 if (stretch_dist <= min_stretch) {
585 if (stretch_dist >= max_stretch) {
589 float t = (stretch_dist - min_stretch) / (max_stretch - min_stretch);
590 return MIN(1.0f - t + 0.1, 1.0f);
606 static const float MAX_SPEED = 150.0f;
608 static const float GRAVITY = 9.8f;
609 float accel_step = ACCEL *
dt;
611 move_dir.
x *= stride;
612 move_dir.
y *= stride;
625 float wish_x = move_dir.
x * fwd.
x + move_dir.
y * rgt.
x;
626 float wish_y = move_dir.
x * fwd.
y + move_dir.
y * rgt.
y;
631 float add = MAX_SPEED - cur;
634 float push = fminf(add, accel_step);
646 if (nlen > MAX_SPEED)
648 float scale = MAX_SPEED / nlen;
668 static const float MAX_SPEED = 30.0f;
669 static const float ACCEL = 20.0f;
670 float accel_step = ACCEL *
dt;
671 float s_len = sqrtf(move_dir.
x * move_dir.
x + move_dir.
y * move_dir.
y);
681 float wish_x = move_dir.
x * fwd.
x + move_dir.
y * rgt.
x;
682 float wish_y = move_dir.
x * fwd.
y + move_dir.
y * rgt.
y;
683 float wish_z = move_dir.
x * fwd.
z + move_dir.
y * rgt.
z;
685 float proj_wish = wish_x * rgt.
x + wish_y * rgt.
y + wish_z * rgt.
z;
693 float add = MAX_SPEED - cur;
696 float push = fminf(add, accel_step);
721 vec3 inv_dir = {1.0f/dir.
x, 1.0f/dir.
y, 1.0f/dir.
z};
728 vec3 centered = proposed;
748 if (node->
left == -1 && node->
right == -1)
751 for (
int i = 0; i < node->
count; i++)
762 bool already_pushed =
false;
763 for (
int j = 0; j < *contact_count; j++)
765 if (mn->
pc == p[j].pcB) already_pushed =
true;
767 if (already_pushed)
continue;
772 if (mn->
mesh == -1)
continue;
786 vec3 inv_dir = {1.0f/td.
x, 1.0f/td.
y, 1.0f/td.
z};
793 p[*contact_count].mnB = mn->
id;
794 p[*contact_count].pcB = mn->
pc;
804 if (mn->
mesh == -1)
continue;
823 if (
GJK(cd, cd0, mat, mat0, mat_inv, mat0_inv, &p[*contact_count],
EW[0]))
827 p[*contact_count].mnB = mn->
id;
828 p[*contact_count].pcB = mn->
pc;
847 if (
GJK(cd, cd0, mat, mat0, mat_inv, mat0_inv, &p[*contact_count],
EW[0]))
849 printf(
"CC: %d\n", *contact_count);
852 p[*contact_count].mnB = mn->
id;
853 p[*contact_count].pcB = mn->
pc;
880 vec3 inv_dir = {1.0f/td.
x, 1.0f/td.
y, 1.0f/td.
z};
887 p[*contact_count].mnB = mn->
id;
888 p[*contact_count].pcB = mn->
pc;
928 if (
GJK(cd, cd0, mat, mat0, mat_inv, mat0_inv, &p[*contact_count],
EW[0]))
932 p[*contact_count].mnB = mn->
id;
933 p[*contact_count].pcB = mn->
pc;
994 for (
int i = 0; i < 3; i++)
996 mfwd[0] = (
vec3){minv[0].
x, minv[1].
x, minv[2].
x};
997 mfwd[1] = (
vec3){minv[0].
y, minv[1].
y, minv[2].
y};
998 mfwd[2] = (
vec3){minv[0].
z, minv[1].
z, minv[2].
z};
1019 vec3 HIP_POS = hip_pos;
1026 vec3 MINV[3], MFWD[3];
1036 float E = sqrtf(
A *
A - d * d);
1045 return (d > 0 && d <
A);
1055 bool left_planted = !left_leg->
stepping;
1056 bool right_planted = !right_leg->
stepping;
1062 float wish_x = intended_vel.
x * fwd.
x + intended_vel.
y * rgt.
x;
1063 float wish_y = intended_vel.
x * fwd.
y + intended_vel.
y * rgt.
y;
1071 vec3 down_dir = {0,0,-1};
1072 vec3 inv_down_dir = {1.0f/down_dir.
x, 1.0f/down_dir.
y, 1.0f/down_dir.
z};
1083 leg_pos = left_leg->
pos;
1088 leg_pos = right_leg->
pos;
1101 if (left_planted && right_planted)
1105 else if (left_planted)
1107 support_pos = left_leg->
pos;
1109 else if (right_planted)
1111 support_pos = right_leg->
pos;
1117 vec3 ideal_body_pos = support_pos;
1122 float stiffness = 50.0f;
1123 float damping = 2.0f * sqrtf(stiffness);
1128 vec3 acceleration =
vec3_add(spring_force, damping_force);
1140 const float step_height = 0.3f;
1141 const float raycast_height = 0.4f;
1145 float min_speed = 0.1f;
1146 float current_speed =
MAX(len, min_speed);
1148 float desired_stride = 0.6f;
1154 float wish_x = vel.
x * fwd.
x + vel.
y * rgt.
x;
1155 float wish_y = vel.
x * fwd.
y + vel.
y * rgt.
y;
1157 vec3 fw_vel = {fw.
x, fw.
y, 0.0};
1163 sweep_start.
z += step_height;
1164 vec3 sweep_end = ideal_target;
1165 sweep_end.
z += step_height;
1169 if (horiz_dist > 0.001f) horiz_dir =
vec3_scale(horiz_dir, 1.0f / horiz_dist);
1179 vec3 down_dir = {0,0,-1};
1188 final_target = sweep_end;
1189 final_target.
z -= step_height;
1203 float height_offset = 4.0f * step_height * t * (1.0f - t);
1205 leg->
pos.
z += height_offset;
1220 if (dist > desired_stride * 0.4f && !(other_leg->
stepping))
1264 vec3 total_torque =
vec3_add(spring_torque, damping_torque);
1278 float target_roll = right_accel * 0.01f;
1310 bool hit_floor =
false;
1332 float left_drop =
MAX(left_length - leg_length, 0);
1334 float right_drop =
MAX(right_length - leg_length, 0);
1338 float drop =
MAX(left_drop, right_drop);
1340 bool left_drop_higher = left_drop > right_drop;
1371#define MIN_WALKABLE_SURFACE 0.5f
1372#define STEP_HEIGHT 0.6f
1373#define RAYCAST_HEIGHT 0.6f
1393 vec3 raycast_proposed = cam_pos;
1396 vec3 dir = {0,0,-1};
1397 vec3 inv_dir= {1.0f/dir.
x, 1.0f/dir.
y, 1.0f/dir.
z};
1400 Mesh * m = &meshes[0];
1417 int current_count = 0;
1420 int contact_count = 0;
1423 bool all_walkable =
true;
1424 float current_height = 0.0f;
1425 for (
int i = 0; i < contact_count; i++)
1431 all_walkable =
false;
1436 current_height = pt->
a.
z;
1441 bool is_resolved =
false;
1442 bool on_ground =
false;
1443 vec3 last_proposed = cam_pos;
1444 for (
int i = 0; i < contact_count; i++)
1462 bool impulse =
false;
1463 while (parent != -1)
1478 if (impulse ==
true)
break;
1480 for (
int i = 0; i < 12; i++)
1483 for (
int j = 0; j < contact_count; j++)
GJK and EPA collision resolution.
RayHit raycast_scene(vec3 start, vec3 dir, float initial_t, bool check_normal)
Raycasts through the scene and finds closest intersection to meshes.
bool traverse_collision_bvh(Mesh *m, MeshNode *mn, BVHNode *nodes, BVHNode *node, vec3 proposed, vec3 inv_p, CollisionPoints *p)
Traverses the mesh bvh to find collisions based on point vs aabb within a certain epsilon for player ...
Acceleration structure generation and how we traverse them.
Audio probe baking, portal creation, probe volumes, and emitter functions.
bool update_leg(LegIK *leg, LegIK *other_leg, vec3 vel)
Raycasts forward and downward to determine how far a leg can go and also updates the leg position if ...
void update_light_camera(Camera *lcam, Light *light)
Updates a directional light camera and locks it to texel size.
bool collision(vec3 proposed, vec3 pts[3], vec3 normal, CollisionPoints *p)
void calculate_camera_angles(Camera *cam, Spring *s, vec3 input_acceleration, float dt)
Calculates camera vectors and roll based on the angular Spring attached to it.
#define RAYCAST_HEIGHT
How high legs are raised to detect for steps.
mat4 make_ortho(float left, float right, float bottom, float top, float near, float far)
Makes an 4x4 orthographic matrix.
void reset_skeleton(Skeleton *s, vec3 pos)
Resets a skeleton to be relative to a new position.
void update_spring(Spring *s)
Updates a Spring's velocity and position based on the difference between its current position and tar...
void define_M(vec3 P, vec3 D, vec3 minv[3], vec3 mfwd[3])
Refer to Ken Perlin's 2 joint IK implmentation.
float calculate_stride_governor(vec3 player_pos, LegIK *left_leg, LegIK *right_leg)
Calculates a stride based on how stretched the legs are from the center position.
vec3 rot(vec3 M[3], vec3 a)
Refer to Ken Perlin's 2 joint IK implmentation.
void update_skeleton(Skeleton *s, Camera *cam, vec3 vel)
Moves feet, then solves a skeleton's leg IKs and updates the springs accordingly.
void player_find_collision(vec3 start, vec3 proposed, CollisionPoints p[MAX_PLAYER_CONTACTS], int *contact_count)
Finds collisions against the Player's proposed position through a SceneData.
void resolve_collisions(Camera *cam)
Resolves collisions between camera and geometry, uses player.
void apply_velocity(Camera *cam, vec3 move_dir, float dt)
Applies no-clip mode velocity to player with no gravity applied.
#define MIN_WALKABLE_SURFACE
How steep the surface is for it to be walkable.
void compute_camera_basis(Camera *cam, float yaw, float pitch, float roll, vec3 world_up)
Computes camera vectors based on yaw, pitch, and roll.
const float HALTON_8[8][2]
mat4 make_look_at(Camera *cam, vec3 eye)
Makes a 4x4 view matrix using camera vectors.
void update_body_from_legs(LegIK *left_leg, LegIK *right_leg, vec3 intended_vel, float dt)
Calculates velocity based on leg positions and integrates them into the player.
void update_camera(Skeleton *s, Camera *cam)
Updates a camera based on a skeleton's current neck and body position.
mat4 make_projection(float fov_deg, float aspect, float near, float far)
Makes an 4x4 projection matrix.
bool solve_Q(Skeleton *s, vec3 D, vec3 *Q, vec3 *P, vec3 hip_pos)
Refer to Ken Perlin's 2 joint IK implmentation.
void update_angular_spring(Spring *s, float dt)
Updates a Spring's angular velocity and rotation based on the difference between its current rotation...
void apply_planet_velocity(Camera *cam, vec3 move_dir, float dt)
Applies normal velocity to player with gravity and friction applied.
Camera cam
This is currently the player cam.
Engine specific types and defines.
Spring neck
Neck Spring (target based on pelvis).
vec3 pos
Currently anchors the Skeleton.
vec3 p_right
The current right foot position.
bool on_ground
If the camera is on the ground.
bool noclip
If collision is enabled.
Vector mesh_nodes
MeshNodes.
vec3 right_hip_pos
The right hip pos, offset by right_leg_offset.
vec3 start_pos
Starting position of the foot.
SceneData scene_data
SceneData that is more-so scene specific.
vec3 velocity
Velocity of this spring.
Skeleton skeleton
Player Skeleton.
vec3 p_left
The current left foot position.
float neck_pelvis_diff
Height from pelvis to neck.
vec3 angular_vel
Current velocity for the rotation.
PhysicsComponent * pc
PhysicsComponent in PHYSICS_COMPONENTS.
float lower_leg_length
Lower leg length.
Mesh mesh
The Mesh for the ConvexHull used for collisions (an AABB).
float stiff_rot
Stiffness for rotation.
LegIK right_leg
Right leg.
bool sprinting
Currently unused flag for sprinting.
mat4 inv_proj
The inverse projection matrix.
float neck_height
Height from ground to neck.
#define VIEW_NEAR
Near plane for player.
vec3 rotation
Current rotation of the spring.
Globals GLOBALS
Engine Globals.
float damp_rot
Dampening for rotation.
mat4 inv_view
The inverse view matrix.
vec3 pos
Position of the foot.
mat4 unjittered_proj
The projection matrix without TAA jittering.
vec3 q_right
The current knee position for right leg.
float upper_leg_length
Upper leg length.
#define ACCEL_BETA
Friction / air accel decay constant.
vec3 q_left
The current knee position for left leg.
bool stepping
If the leg is currently stepping.
bool stepping_off_ledge
If the leg is over air.
mat4 view
The view matrix.
mat4 unjittered_inv_proj
The inverse projection matrix without TAA jittering.
#define VIEW_FAR
Far plane for player.
float time
The current time to linearly interpolate between start and target pos.
float hip_height
Height from ground to hip.
vec3 target_pos
Target position for the current step.
float pelvis_height
Height from ground to pelvis.
vec3 pos
Position of the spring.
mat4 proj
The projection matrix.
Camera * cam
Player Camera.
float duration
The duration of the entire step.
vec3 left_hip_pos
The left hip pos, offset by left_leg_offset.
vec3 target
Where the spring wants to move to based on Skeleton positions, before velocity is integrated.
vec4 cam_pos
The position of the Camera (padded).
Spring pelvis
Pelvis Spring (target based on leg positions).
#define FOV
FOV constant for player.
vec3 target_rot
Where the spring is facing based on Skeleton positions.
The camera structure for players and entities.
The controller for leg IKs for Skeletons.
The data for the current scene.
The controller for leg IKs for Skeletons.
The spring used for determining Skeleton positions and eventually neck trajectory.
void play_emitter(AudioEmitter *emitter, vec3 pos)
Starts playing an emitter.
Vector hull_faces
HullFaces that are generated after incremental_hull() that combine contiguous triangles into singular...
CollisionData cd
Tagged union for collision options.
vec3 old_vel
Last step's velocity.
Vector vertices
Points that initially copy the Mesh vertices but then are deduplicated and modified in incremental_hu...
vec3 accel
Acceleration from this step + acceleration from last step * ACCEL_BETA.
union CollisionData::@341211070342303276310035141141142214214320110321 data
bool at_rest
If this component is at rest it does not update.
vec3 a
Furthest point of A into B.
vec3 normal
The collision normal from B into A.
vec3 b
Furthest point of B into A.
#define MAX_PLAYER_CONTACTS
Maximum amount of CollisionPoints to consider per frame.
void player_resolve_impulse_static(PhysicsComponent *pcA, CollisionPoints *p)
Solves an impulse for a Player PhysicsComponent against a static Mesh.
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 player_resolve_impulse(PhysicsComponent *pcA, PhysicsComponent *pcB, CollisionPoints *p)
Solves an impulse for a Player PhysicsComponent against another PhysicsComponent.
@ COL_CAPSULE
A capsule is constructed via AABB dimensions and is used for collision tests.
@ COL_HULL
Incremental hull generates a ConvexHull which is used for collision tests.
@ COL_TRIANGLE
The triangle BVH is generated for this mesh and triangle tests are performed on it.
The tagged union for collision type, stored in PhysicsComponent.
Points that track collision data such as direction, penetration depth, and more to generate Manifolds...
Convex Hulls that are generated via incremental hull for non-static meshes.
size_t id
The ID inside of Globals.mesh_nodes.
PhysicsComponent * pc
The physics controller initialized based on data from this MeshNode.
vec3 dir
Direction for lights that are not POINT_LIGHTs.
bool free
If the MeshNode can be replaced.
int mesh
The Mesh it references.
int parent
The parent to this node, a global index into Globals.mesh_nodes.
bool static_mesh
If this mesh is static (DrawArrays) or dynamic (DrawElements), used to indicate Lightmap status.
The CPU side structure for Lights.
MeshNodes that describe the position of Meshes, GLTF nodes for GLTFAnimations, or hold references to ...
Important globals for the entire engine and initialization functions that are mainly called on engine...
Vector hulls
The ConvexHulls generated if this Mesh is dynamic.
AABB aabb
The AABB of the mesh.
bool bvh_prim
This Mesh has a BVH computed.
Vector tris
The MeshFaces.
Vector vertices
The Points.
A Mesh with geometry data that is either initialized as static or dynamic and split into SubMeshes.
EditorSettings EDITOR_SETTINGS
AudioEmitter * FOOTSTEP_EMITTER
const vec3 right_leg_offset
float dt
The 1.0f/60 simulation timestep.
Framebuffer SHADOW_MAP_FBO
const vec3 left_leg_offset
Player player
The Player data.
vec3 vec3_cross(vec3 a, vec3 b)
float vec3_dist(vec3 a, vec3 b)
vec3 vec3_lerp(vec3 a, vec3 b, float t)
float vec3_dot(vec3 a, vec3 b)
vec2 vec2_normalize(vec2 v)
vec3 vec3_normalize(vec3 v)
vec3 vec3_scale(vec3 a, float b)
vec3 vec3_sub(vec3 a, vec3 b)
vec3 vec3_add(vec3 a, vec3 b)
vec2 vec2_sub(vec2 a, vec2 b)
float clamp(float a, float b, float c)
bool m4_inverse(float out[16], const float m[16])
float vec2_length(vec2 v)
float vec3_length(vec3 v)
Mesh functions that modify and initialize Mesh data.
PhysicsComponent impulse resolution and collision detection and acceleration structure traversal.
int next
The pointer to the next node (either right child or parent's right child) for recursion-less traversa...
AABB aabb
The bounding box of all of this node's children.
Nodes for the acceleration structures of BVHs.
bool aabbs_intersect(AABB *a, AABB *b)
AABB transform_aabb_by_position(AABB aabb, vec3 pos)
vec3 closest_point_triangle(vec3 p, vec3 a, vec3 b, vec3 c)
vec3 mat3_mul_vec3(mat3 m, vec3 v)
vec4 vec3_to_vec4(vec3 v)
#define CAST_VEC(vec, type)
mat4 compose_trs_from_vectors(vec3 pos, vec4 rot, vec3 scale)
vec3 mat4_mul_direction(const mat4 m, vec3 v)
float wrap_angle_pi(float angle)
vec3 mat4_mul_point(mat4 m, vec3 v)