Untitled
Loading...
Searching...
No Matches
camera.c
Go to the documentation of this file.
1
6#include <math.h>
7#include <omp.h>
8#include <assert.h>
9#include "core/acceleration.h"
10#include "audio/audio.h"
11#include "core/engine_types.h"
12#include "mesh.h"
13#include "utilities/types.h"
14#include "utilities/mathf.h"
15#include "core/camera.h"
16#include "load/init.h"
17#include "utilities/utilities.h"
18#include <stdio.h>
19#include "physics/physics.h"
20#include "physics/GJK.h"
21
29static float calculate_stride_governor(vec3 player_pos, LegIK *left_leg, LegIK *right_leg);
30
38static void player_find_collision(vec3 start, vec3 proposed, CollisionPoints p[MAX_PLAYER_CONTACTS], int * contact_count);
39
44static void define_M(vec3 P, vec3 D, vec3 minv[3], vec3 mfwd[3]);
45
50static vec3 rot(vec3 M[3], vec3 a);
55static bool solve_Q(Skeleton * s, vec3 D, vec3 * Q, vec3 * P, vec3 hip_pos);
56
96static bool update_leg(LegIK * leg, LegIK * other_leg, vec3 vel);
97
109static void update_spring(Spring * s);
110
123static void update_angular_spring(Spring * s, float dt);
124
132static void calculate_camera_angles(Camera * cam, Spring* s, vec3 input_acceleration, float dt);
133
134void
136 (
137 Camera * cam,
138 float yaw,
139 float pitch,
140 float roll,
141 vec3 world_up
142 )
143
144{
145
146 float cos_p = cosf(pitch);
147 cam->forward.x = cosf(yaw) * cos_p;
148 cam->forward.y = sinf(yaw) * cos_p;
149 cam->forward.z = sinf(pitch);
151
152 vec3 right = vec3_cross(cam->forward, world_up);
153 right = vec3_normalize(right);
154
155 vec3 up = vec3_cross(right, cam->forward);
156 up = vec3_normalize(up);
157 if (fabsf(roll) > 0.0001f)
158 {
159
160 float cos_r = cosf(roll);
161 float sin_r = sinf(roll);
162
163 vec3 final_rgt = vec3_normalize(vec3_add(vec3_scale(right, cos_r), vec3_scale(up, sin_r)));
164 vec3 final_up = vec3_normalize(vec3_sub(vec3_scale(up, cos_r), vec3_scale(right, sin_r)));
165
166 cam->right = final_rgt;
167 cam->up = final_up;
168 }
169 else
170 {
171 cam->right = right;
172 cam->up = up;
173 }
174
175}
176
177mat4
179 (
180 float left,
181 float right,
182 float bottom,
183 float top,
184 float near,
185 float far
186 )
187
188{
189
190 float rl = right - left;
191 float tb = top - bottom;
192 float fn = far - near;
193
194 mat4 out;
195
196 out.a0 = 2.0f / rl;
197 out.a1 = 0;
198 out.a2 = 0;
199 out.a3 = 0;
200
201 out.b0 = 0;
202 out.b1 = 2.0f / tb;
203 out.b2 = 0;
204 out.b3 = 0;
205
206 out.c0 = 0;
207 out.c1 = 0;
208 out.c2 = -2.0f / fn;
209 out.c3 = 0;
210
211 out.d0 = -(right + left) / rl;
212 out.d1 = -(top + bottom) / tb;
213 out.d2 = -(far + near) / fn;
214 out.d3 = 1.0f;
215 return out;
216}
217
218
219mat4
221 (
222 float fov_deg,
223 float aspect,
224 float near,
225 float far
226 )
227
228{
229
230 float fov_rad = fov_deg * (M_PI / 180.0f);
231 float f = 1.0f / tanf(fov_rad / 2.0f);
232 mat4 out;
233 out.a0 = f / aspect;
234 out.a1 = 0;
235 out.a2 = 0;
236 out.a3 = 0;
237
238 out.b0 = 0;
239 out.b1 = f;
240 out.b2 = 0;
241 out.b3 = 0;
242
243 out.c0 = 0;
244 out.c1 = 0;
245 out.c2 = -((far + near) / (far - near));
246 out.c3 = -1;
247
248 out.d0 = 0;
249 out.d1 = 0;
250 out.d2 = -(2.0f * far * near) / (far - near);
251 out.d3 = 0;
252 return out;
253}
254
255
256mat4
258 (
259 Camera * cam,
260 vec3 eye
261 )
262
263{
264
265 vec3 f = cam->forward;
266 vec3 s = cam->right;
267 vec3 u = cam->up;
268
269 mat4 out;
270 out.a0 = s.x;
271 out.a1 = u.x;
272 out.a2 = -f.x;
273 out.a3 = 0;
274
275 out.b0 = s.y;
276 out.b1 = u.y;
277 out.b2 = -f.y;
278 out.b3 = 0;
279
280 out.c0 = s.z;
281 out.c1 = u.z;
282 out.c2 = -f.z;
283 out.c3 = 0;
284
285
286 out.d0 = -vec3_dot(s, eye);
287 out.d1 = -vec3_dot(u, eye);
288 out.d2 = vec3_dot(f, eye);
289
290 out.d3 = 1;
291 return out;
292}
293
294
295// TODO CLEAN THIS UP
296void
298 (
299 Camera * lcam,
300 Light * light
301 )
302
303{
304
305
306 float bounds = 40.0f;
307 vec3 cam_pos = (player.skeleton.neck.pos);
308
309 vec3 offset = (vec3){0, 0, 0};
310
311 vec3 pos = vec3_add(cam_pos, offset);
312
313 vec3 sd = vec3_normalize((vec3){ light->dir.x, light->dir.y, light->dir.z });
314
315 vec3 negated_sun_dir = {
316 sd.x,
317 sd.y,
318 sd.z
319 };
320
321
322 float fov_rad = FOV * (M_PI / 180.0f);
323 float near_height = tanf((float)fov_rad / 2) * -10.0f;
324 float near_width = near_height * ((float)RENDER_WIDTH / RENDER_HEIGHT);
325 float far_height = tanf((float)fov_rad / 2) * 10.0f;
326 float far_width = far_height * ((float)RENDER_WIDTH / RENDER_HEIGHT);
327 vec3 near_forward = vec3_scale(cam.forward, -10.0f);
328 vec3 far_forward = vec3_scale(cam.forward, 10.0f);
329 vec3 near_right = vec3_scale(cam.right, near_width);
330 vec3 far_right = vec3_scale(cam.right, far_width);
331 vec3 near_up = vec3_scale(cam.up, near_height);
332 vec3 far_up = vec3_scale(cam.up, far_height);
333
334
335 vec3 near_center = vec3_add(cam_pos,
336 vec3_scale(cam.forward, -10.0));
337
338
339 vec3 far_center = vec3_add(cam_pos,
340 vec3_scale(cam.forward, 10.0));
341
342 vec3 near_bl = vec3_sub(
343 vec3_sub(
344 near_center,
345 near_right),
346 near_up);
347 vec3 near_ul = vec3_add(
348 vec3_sub(
349 near_center,
350 near_right),
351 near_up);
352 vec3 near_br = vec3_sub(
353 vec3_add(
354 near_center,
355 near_right),
356 near_up);
357 vec3 near_ur = vec3_add(
358 vec3_add(
359 near_center,
360 near_right),
361 near_up);
362 vec3 far_bl = vec3_sub(
363 vec3_sub(
364 far_center,
365 far_right),
366 far_up);
367 vec3 far_ul = vec3_add(
368 vec3_sub(
369 far_center,
370 far_right),
371 far_up);
372 vec3 far_br = vec3_sub(
373 vec3_add(
374 far_center,
375 far_right),
376 far_up);
377 vec3 far_ur = vec3_add(
378 vec3_add(
379 far_center,
380 far_right),
381 far_up);
382
383 vec3 light_points[8];
384 light_points[0] = mat4_mul_point(lcam->cam_data.view, near_bl);
385 light_points[1] = mat4_mul_point(lcam->cam_data.view, near_ul);
386 light_points[2] = mat4_mul_point(lcam->cam_data.view, near_br);
387 light_points[3] = mat4_mul_point(lcam->cam_data.view, near_ur);
388 light_points[4] = mat4_mul_point(lcam->cam_data.view, far_bl);
389 light_points[5] = mat4_mul_point(lcam->cam_data.view, far_ul);
390 light_points[6] = mat4_mul_point(lcam->cam_data.view, far_br);
391 light_points[7] = mat4_mul_point(lcam->cam_data.view, far_ur);
392
393
394 float slice_center_dist = (20.0f - -20.0f) / 2.0f;
395 vec3 frustum_center = vec3_add(cam_pos, vec3_scale(cam.forward, slice_center_dist));
396
397 vec3 diff = vec3_sub(far_ur, frustum_center);
398 float radius = sqrtf(diff.x*diff.x + diff.y*diff.y + diff.z*diff.z);
399
400 float diameter = radius * 2.0f;
401 float texel_size = diameter / (float)SHADOW_MAP_FBO.w;
402
403 vec3 light_dir = vec3_normalize((vec3){ light->dir.x, light->dir.y, light->dir.z });
404 vec3 pull_back = vec3_scale(light_dir, radius + 10.0f);
405 vec3 light_pos = vec3_sub(frustum_center, pull_back);
406
407
408 vec3 world_up = (vec3){0, 1, 0};
409 if (fabsf(vec3_dot(vec3_scale(light_dir, -1), world_up)) > 0.99f) {
410 world_up = (vec3){0, 0, 1};
411 }
412
413 lcam->forward = vec3_normalize(vec3_scale(sd, -1));
414 lcam->right = vec3_cross(lcam->forward, world_up);
415 lcam->right = vec3_normalize(lcam->right);
416
417 lcam->up = vec3_cross(lcam->right, lcam->forward);
418 lcam->up = vec3_normalize(lcam->up);
419 lcam->target = vec3_add(pos, lcam->forward);
420 lcam->cam_data.view = make_look_at(lcam, pos);
421 vec3 center_light_space = mat4_mul_point(lcam->cam_data.view, frustum_center);
422
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;
425
426 float offset_x = snapped_x - center_light_space.x;
427 float offset_y = snapped_y - center_light_space.y;
428
429 float minx = -radius + offset_x;
430 float maxx = radius + offset_x;
431 float miny = -radius + offset_y;
432 float maxy = radius + offset_y;
433 float minz = -20.0f;
434 float maxz = radius + 20.0f;
435
436 lcam->cam_data.proj = make_ortho(minx, maxx, miny, maxy, minz, maxz);
437
438
439}
440
441
442
443bool collision(vec3 proposed, vec3 pts[3], vec3 normal, CollisionPoints * p)
444{
445
446 float best = INFINITY;
447
448 vec3 pt = closest_point_triangle(proposed, pts[0], pts[1], pts[2]);
449 vec3 delta = vec3_sub(pt, proposed);
450 float pt_dist = vec3_length(delta);
451 if (pt_dist < 0.2f)
452 {
453
454 p->d = -(pt_dist - 0.2f);
455
456 p->a = pt;
457 return true;
458 }
459
460 return false;
461}
462
463
464static const float HALTON_8[8][2] = {
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 }
473};
474
475void
477 (
478 Skeleton * s,
479 Camera * cam
480 )
481
482{
483
485 if (GLOBALS.noclip)
486 {
488 }
489
490 if (GLOBALS.noclip)
491 {
492
493 compute_camera_basis(cam, cam->yaw, cam->pitch, cam->roll, (vec3){0, 0, 1});
494 }
495 else
496 {
497
499 }
500
502 FOV,
503 (float)RENDER_WIDTH / (float)RENDER_HEIGHT,
504 VIEW_NEAR,
506 );
507
508
510 {
511
512 int sample_idx = master_frame % 8;
513 float jitter_x = HALTON_8[sample_idx][0];
514 float jitter_y = HALTON_8[sample_idx][1];
515
516 JITTER_OFFSET.x = (jitter_x / (float)RENDER_WIDTH) * 2.0f;
517 JITTER_OFFSET.y = (jitter_y / (float)RENDER_HEIGHT) * 2.0f;
518
519 mat4 jittered_proj = cam->cam_data.unjittered_proj;
520 jittered_proj.c0 += JITTER_OFFSET.x;
521 jittered_proj.c1 += JITTER_OFFSET.y;
522
523 cam->cam_data.proj = jittered_proj;
524
525 }
526 else
527 {
529 }
530
531
532
534 (vec3){
538 },
539 cam->forward
540 );
541
543 cam,
544 (vec3){
548 }
549 );
550
551 assert(fabsf(vec3_length(cam->right) - 1.0f) < 0.001f);
552 assert(fabsf(vec3_length(cam->up) - 1.0f) < 0.001f);
553 assert(fabsf(vec3_length(cam->forward) - 1.0f) < 0.001f);
554
555 m4_inverse((float*)&cam->cam_data.inv_view, (const float *)&cam->cam_data.view);
556 m4_inverse((float*)&cam->cam_data.inv_proj, (const float *)&cam->cam_data.proj);
558
559}
560
561static float calculate_stride_governor(vec3 player_pos, LegIK *left_leg, LegIK *right_leg)
562{
563 vec3 support_pos;
564 bool both_planted = !left_leg->stepping && !right_leg->stepping;
565
566 if (both_planted) {
567 support_pos = vec3_scale(vec3_add(left_leg->pos, right_leg->pos), 0.5f);
568 } else if (left_leg->stepping) {
569 support_pos = right_leg->pos;
570 } else {
571 support_pos = left_leg->pos;
572 }
573
574 vec2 p2d = { player_pos.x, player_pos.y };
575 vec2 s2d = { support_pos.x, support_pos.y };
576 float stretch_dist = vec2_length(vec2_sub(p2d, s2d));
577
578 const float min_stretch = 0.15f;
579 const float max_stretch = 0.4f;
580
581 if (stretch_dist <= min_stretch) {
582 return 1.0f;
583 }
584
585 if (stretch_dist >= max_stretch) {
586 return 0.0f;
587 }
588
589 float t = (stretch_dist - min_stretch) / (max_stretch - min_stretch);
590 return MIN(1.0f - t + 0.1, 1.0f);
591}
592void
594 (
595 Camera * cam,
596 vec3 move_dir,
597 float dt
598 )
599
600{
601
603 // player.pc->vel.x *= stride;
604 // player.pc->vel.y *= stride;
605 // static const float MAX_SPEED = 30.0f;
606 static const float MAX_SPEED = 150.0f;
607 float ACCEL = player.sprinting ? 31.0f : 11.0f;
608 static const float GRAVITY = 9.8f;
609 float accel_step = ACCEL * dt;
610
611 move_dir.x *= stride;
612 move_dir.y *= stride;
613
614
615 if (cam->on_ground)
616 {
617
618 player.pc->vel = vec3_scale(player.pc->vel, 0.85f);
619 }
620
621 vec3 fwd = {cosf(cam->yaw), sinf(cam->yaw), sinf(cam->pitch)};
622 vec3 rgt = {-sinf(cam->yaw), cosf(cam->yaw), 0.0f};
623 fwd = vec3_normalize(fwd);
624 rgt = vec3_normalize(rgt);
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;
627
628 float cur = player.pc->vel.x * wish_x + player.pc->vel.y * wish_y;
629 if (cam->on_ground)
630 {
631 float add = MAX_SPEED - cur;
632 if (add)
633 {
634 float push = fminf(add, accel_step);
635 player.pc->vel.x += wish_x * push;
636 player.pc->vel.y += wish_y * push;
637 }
638 }
639 else
640 {
641 player.pc->vel.z -= GRAVITY * dt;
642 }
643
644
645 float nlen = sqrtf(player.pc->vel.x * player.pc->vel.x + player.pc->vel.y * player.pc->vel.y);
646 if (nlen > MAX_SPEED)
647 {
648 float scale = MAX_SPEED / nlen;
649 player.pc->vel.x *= scale;
650 player.pc->vel.y *= scale;
651 }
652
653
654}
655
656
657
658void
660 (
661 Camera * cam,
662 vec3 move_dir,
663 float dt
664 )
665
666{
667
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);
672 if (s_len < 0.01f)
673 {
674 player.pc->vel = vec3_scale(player.pc->vel, 0.85f);
675 }
676
677 vec3 fwd = {cosf(cam->yaw), sinf(cam->yaw), sinf(cam->pitch)};
678 vec3 rgt = {-sinf(cam->yaw), cosf(cam->yaw), 0.0f};
679 fwd = vec3_normalize(fwd);
680 rgt = vec3_normalize(rgt);
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;
684
685 float proj_wish = wish_x * rgt.x + wish_y * rgt.y + wish_z * rgt.z;
686 if (s_len > 0.0f)
687 {
688 wish_x /= s_len;
689 wish_y /= s_len;
690 wish_z /= s_len;
691 }
692 float cur = player.pc->vel.x * wish_x + player.pc->vel.y * wish_y + player.pc->vel.z * wish_z;
693 float add = MAX_SPEED - cur;
694 if (add)
695 {
696 float push = fminf(add, accel_step);
697 player.pc->vel.x += wish_x * push;
698 player.pc->vel.y += wish_y * push;
699 player.pc->vel.z += wish_z * push;
700 }
701}
702
703
705
706
707
708
709
710static void player_find_collision(vec3 start, vec3 proposed, CollisionPoints p[MAX_PLAYER_CONTACTS], int * contact_count)
711{
712
713 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
716
718
719 vec3 dir = vec3_sub(proposed, start);
720 vec3_normalize(dir);
721 vec3 inv_dir = {1.0f/dir.x, 1.0f/dir.y, 1.0f/dir.z};
722 CollisionData cd;
723 cd.type = COL_HULL;
724 cd.data.hull.pts = hull->vertices.buffer;
725 cd.data.hull.count = hull->vertices.count;
726 cd.data.hull.mfs = hull->hull_faces.buffer;
727 cd.data.hull.face_count = hull->hull_faces.count;
728 vec3 centered = proposed;
729 centered.z -= 0.2f;
730 mat4 mat = compose_trs_from_vectors(centered, (vec4){0,0,0,1}, (vec3){1,1,1});
731 mat4 mat_inv;
732 m4_inverse((float*)&mat_inv, (float*)&mat);
733 int ni = 0;
734 BVHNode * nodes = CAST_VEC(sd->bvh_nodes, BVHNode);
735 AABB aabb = player.mesh.aabb;
736 aabb = transform_aabb_by_position(aabb, centered);
737
738 while (ni >= 0)
739 {
740
741 BVHNode * node = &nodes[ni];
742
743 if (!aabbs_intersect(&node->aabb, &aabb))
744 {
745 ni = node->next;
746 continue;
747 }
748 if (node->left == -1 && node->right == -1)
749 {
750
751 for (int i = 0; i < node->count; i++)
752 {
753 MeshNode * mn = &mns[sd->bvh_indices[node->first_index + i]];
754 if ((mn->mesh == -1 && mn->parent != -1) || mn->free) continue;
755 if (*contact_count >= MAX_PLAYER_CONTACTS)
756 {
757 printf("MAX\n");
758 return;
759 }
760 if (mn->pc)
761 {
762 bool already_pushed = false;
763 for (int j = 0; j < *contact_count; j++)
764 {
765 if (mn->pc == p[j].pcB) already_pushed = true;
766 }
767 if (already_pushed) continue;
768 switch(mn->pc->cd.type)
769 {
770 case COL_TRIANGLE:
771 {
772 if (mn->mesh == -1) continue;
773 Mesh * m = &meshes[mn->mesh];
774 ConvexHull * hulls = CAST_VEC(m->hulls, ConvexHull);
775 if (m->bvh_prim)
776 {
777 BVHNode * nodes = CAST_VEC(m->nodes, BVHNode);
778
779
780 mat4 model = mn->global_matrix;
781 mat4 mod;
782 m4_inverse((float*)&mod, (float*)&model);
783 vec3 tr = mat4_mul_point(mod, proposed);
784 vec3 sr = mat4_mul_point(mod, start);
785 vec3 td = mat4_mul_direction(mod, dir);
786 vec3 inv_dir = {1.0f/td.x, 1.0f/td.y, 1.0f/td.z};
787 BVHNode * node = &nodes[0];
788
789 bool col = traverse_collision_bvh(m, mn, nodes, node, proposed, tr, &p[*contact_count]);
790 if (p[*contact_count].collision)
791 {
792
793 p[*contact_count].mnB = mn->id;
794 p[*contact_count].pcB = mn->pc;
795 (*contact_count)++;
796 continue;
797 }
798 }
799 }
800 break;
801 case COL_HULL:
802 {
803
804 if (mn->mesh == -1) continue;
805 Mesh * m = &meshes[mn->mesh];
806 ConvexHull * hulls = CAST_VEC(m->hulls, ConvexHull);
807 for (int j = 0; j < m->hulls.count; j++)
808 {
809
810
811 CollisionData cd0;
812 cd0 = mn->pc->cd;
813
814 cd0.data.hull.pts = hulls[j].vertices.buffer;
815 cd0.data.hull.count = hulls[j].vertices.count;
816 cd0.data.hull.mfs = hulls[j].hull_faces.buffer;
817 cd0.data.hull.face_count = hulls[j].hull_faces.count;
818 mat4 mat0 = mn->global_matrix;
819 mat4 mat0_inv;
820 m4_inverse((float*)&mat0_inv, (float*)&mat0);
821
822
823 if (GJK(cd, cd0, mat, mat0, mat_inv, mat0_inv, &p[*contact_count], EW[0]))
824 {
825
826
827 p[*contact_count].mnB = mn->id;
828 p[*contact_count].pcB = mn->pc;
829
830 (*contact_count)++;
831
832 break;
833
834
835 }
836 }
837
838 }
839 break;
840 case COL_CAPSULE:
841 CollisionData cd0 = mn->pc->cd;
842 mat4 mat0 = mn->global_matrix;
843 mat4 mat0_inv;
844 m4_inverse((float*)&mat0_inv, (float*)&mat0);
845
846
847 if (GJK(cd, cd0, mat, mat0, mat_inv, mat0_inv, &p[*contact_count], EW[0]))
848 {
849 printf("CC: %d\n", *contact_count);
850
851
852 p[*contact_count].mnB = mn->id;
853 p[*contact_count].pcB = mn->pc;
854
855 (*contact_count)++;
856
857 break;
858
859
860 }
861
862 break;
863 }
864 }
865 else
866 {
867 Mesh * m = &meshes[mn->mesh];
868 ConvexHull * hulls = CAST_VEC(m->hulls, ConvexHull);
869 if (m->bvh_prim)
870 {
871 BVHNode * nodes = CAST_VEC(m->nodes, BVHNode);
872
873
874 mat4 model = mn->global_matrix;
875 mat4 mod;
876 m4_inverse((float*)&mod, (float*)&model);
877 vec3 tr = mat4_mul_point(mod, proposed);
878 vec3 sr = mat4_mul_point(mod, start);
879 vec3 td = mat4_mul_direction(mod, dir);
880 vec3 inv_dir = {1.0f/td.x, 1.0f/td.y, 1.0f/td.z};
881 BVHNode * node = &nodes[0];
882
883 bool col = traverse_collision_bvh(m, mn, nodes, node, proposed, tr, &p[*contact_count]);
884 if (p[*contact_count].collision)
885 {
886
887 p[*contact_count].mnB = mn->id;
888 p[*contact_count].pcB = mn->pc;
889 (*contact_count)++;
890 continue;
891 }
892
893 }
894 else
895 {
896 for (int j = 0; j < m->hulls.count; j++)
897 {
898
899
900 CollisionData cd0;
901 if (mn->pc)
902 {
903 cd0 = mn->pc->cd;
904 if (cd0.type == COL_HULL)
905 {
906
907 cd0.data.hull.pts = hulls[j].vertices.buffer;
908 cd0.data.hull.count = hulls[j].vertices.count;
909 cd0.data.hull.mfs = hulls[j].hull_faces.buffer;
910 cd0.data.hull.face_count = hulls[j].hull_faces.count;
911 }
912 }
913 else
914 {
915
916 cd0.type = COL_HULL;
917 cd0.data.hull.pts = hulls[j].vertices.buffer;
918 cd0.data.hull.count = hulls[j].vertices.count;
919 cd0.data.hull.mfs = hulls[j].hull_faces.buffer;
920 cd0.data.hull.face_count = hulls[j].hull_faces.count;
921 }
922
923 mat4 mat0 = mn->global_matrix;
924 mat4 mat0_inv;
925 m4_inverse((float*)&mat0_inv, (float*)&mat0);
926
927
928 if (GJK(cd, cd0, mat, mat0, mat_inv, mat0_inv, &p[*contact_count], EW[0]))
929 {
930
931
932 p[*contact_count].mnB = mn->id;
933 p[*contact_count].pcB = mn->pc;
934
935 (*contact_count)++;
936
937 break;
938
939
940 }
941 }
942
943 }
944
945 }
946 }
947
948 ni = node->next;
949 }
950 else
951 {
952 ni = node->left;
953 }
954 }
955
956}
957
958
959
960
961
962
964{
965
966
967 s->pelvis.pos = pos;
968 s->pelvis.pos.z += s->pelvis_height;
969 s->pelvis.velocity = (vec3){0};
970 s->neck.pos = pos;
971 s->neck.pos.z += s->neck_height;
972 s->neck.velocity = (vec3){0};
973 s->neck.angular_vel = (vec3){0};
974 s->left_leg.stepping = false;
975 s->left_leg.pos = pos;
976 s->right_leg.pos = pos;
977 s->right_leg.stepping = false;
978
979}
980
981
982static void define_M(vec3 P, vec3 D, vec3 minv[3], vec3 mfwd[3])
983{
984
985 minv[0] = vec3_normalize(P);
986
987 float dot_dx = vec3_dot(D, minv[0]);
988 minv[1] = vec3_normalize(vec3_sub(D, vec3_scale(minv[0], dot_dx)));
989
990
991 minv[2] = vec3_cross(minv[0], minv[1]);
992
993
994 for (int i = 0; i < 3; i++)
995 {
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};
999 }
1000
1001}
1002
1003
1004static vec3 rot(vec3 M[3], vec3 a)
1005{
1006 vec3 b;
1007
1008 b.x = vec3_dot(M[0], a);
1009 b.y = vec3_dot(M[1], a);
1010 b.z = vec3_dot(M[2], a);
1011
1012 return b;
1013}
1014
1015// From Perlin's 2 joint IK implementation
1016static bool solve_Q(Skeleton * s, vec3 D, vec3 * Q, vec3 * P, vec3 hip_pos)
1017{
1018
1019 vec3 HIP_POS = hip_pos;
1020
1021 *P = vec3_sub(*P, HIP_POS);
1022
1023 float A = s->upper_leg_length;
1024 float B = s->lower_leg_length;
1025
1026 vec3 MINV[3], MFWD[3];
1027
1028 define_M(*P, D, MINV, MFWD);
1029
1030 vec3 R = rot(MINV, *P);
1031
1032
1033 float C = sqrtf(vec3_dot(R, R));
1034 float d = MAX(0, MIN(A, (C + (A * A - B * B) / C) / 2));
1035
1036 float E = sqrtf(A * A - d * d);
1037
1038 vec3 S = {d, E, 0};
1039
1040 *Q = rot(MFWD, S);
1041
1042 *P = vec3_add(*P, HIP_POS);
1043 *Q = vec3_add(*Q, HIP_POS);
1044
1045 return (d > 0 && d < A);
1046
1047}
1048
1049
1050
1051// Calculates physical body velocity derived entirely from leg positions
1052void update_body_from_legs(LegIK *left_leg, LegIK *right_leg, vec3 intended_vel, float dt)
1053{
1054 vec3 support_pos;
1055 bool left_planted = !left_leg->stepping;
1056 bool right_planted = !right_leg->stepping;
1057
1058 vec3 fwd = {cosf(cam.yaw), sinf(cam.yaw), sinf(cam.pitch)};
1059 vec3 rgt = {-sinf(cam.yaw), cosf(cam.yaw), 0.0f};
1060 fwd = vec3_normalize(fwd);
1061 rgt = vec3_normalize(rgt);
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;
1064 vec3 fw = vec3_normalize((vec3){wish_x,wish_y, 0.0});
1065
1066
1067 if (left_leg->stepping_off_ledge && right_leg->stepping_off_ledge)
1068 {
1069
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};
1073
1074 RayHit hit = raycast_scene(player.pos, down_dir, INFINITY, false);
1075
1076 if (!hit.hit)
1077 {
1078 printf("NO HIT\n");
1079 }
1080 vec3 leg_pos;
1081 if (left_leg->stepping_off_ledge)
1082 {
1083 leg_pos = left_leg->pos;
1084 }
1085 else
1086 {
1087
1088 leg_pos = right_leg->pos;
1089 }
1090 if (!hit.hit || player.pos.z - hit.pos.z > 1.5f)
1091 {
1092 player.pc->vel.z -= 9.8f * dt;
1093 left_leg->pos = player.pos;
1094 right_leg->pos = player.pos;
1095
1096 return;
1097
1098 }
1099
1100 }
1101 if (left_planted && right_planted)
1102 {
1103 support_pos = vec3_scale(vec3_add(left_leg->pos, right_leg->pos), 0.5f);
1104 }
1105 else if (left_planted)
1106 {
1107 support_pos = left_leg->pos;
1108 }
1109 else if (right_planted)
1110 {
1111 support_pos = right_leg->pos;
1112 }
1113 else
1114 {
1115 support_pos = player.pos;
1116 }
1117 vec3 ideal_body_pos = support_pos;
1118
1119
1120 vec3 displacement = vec3_sub(ideal_body_pos, player.pos);
1121
1122 float stiffness = 50.0f;
1123 float damping = 2.0f * sqrtf(stiffness);
1124
1125 vec3 spring_force = vec3_scale(displacement, stiffness);
1126 vec3 damping_force = vec3_scale(player.pc->vel, -damping);
1127
1128 vec3 acceleration = vec3_add(spring_force, damping_force);
1129
1130 player.pc->vel = vec3_add(player.pc->vel, vec3_scale(acceleration, dt));
1131
1132}
1133
1134
1135static bool update_leg(LegIK * leg, LegIK * other_leg, vec3 vel)
1136{
1137
1138
1139
1140 const float step_height = 0.3f;
1141 const float raycast_height = 0.4f;
1142
1143 float len = MAX(vec3_length(vel), 0.1f);
1144
1145 float min_speed = 0.1f;
1146 float current_speed = MAX(len, min_speed);
1147
1148 float desired_stride = 0.6f;
1149
1150 vec3 fwd = {cosf(cam.yaw), sinf(cam.yaw), sinf(cam.pitch)};
1151 vec3 rgt = {-sinf(cam.yaw), cosf(cam.yaw), 0.0f};
1152 fwd = vec3_normalize(fwd);
1153 rgt = vec3_normalize(rgt);
1154 float wish_x = vel.x * fwd.x + vel.y * rgt.x;
1155 float wish_y = vel.x * fwd.y + vel.y * rgt.y;
1156 vec2 fw = vec2_normalize((vec2){wish_x,wish_y});
1157 vec3 fw_vel = {fw.x, fw.y, 0.0};
1158 vec3 forward_lead = vec3_scale(fw_vel, desired_stride);
1159
1160 vec3 ideal_target = vec3_add(other_leg->pos, forward_lead);
1161
1162 vec3 sweep_start = leg->pos;
1163 sweep_start.z += step_height;
1164 vec3 sweep_end = ideal_target;
1165 sweep_end.z += step_height;
1166
1167 vec3 horiz_dir = vec3_sub(sweep_end, sweep_start);
1168 float horiz_dist = vec3_length(horiz_dir);
1169 if (horiz_dist > 0.001f) horiz_dir = vec3_scale(horiz_dir, 1.0f / horiz_dist);
1170 RayHit hit = raycast_scene(sweep_start, horiz_dir, horiz_dist, false);
1171
1172 vec3 drop_start = (hit.hit) ? hit.pos : sweep_end;
1173
1174 if (hit.hit)
1175 {
1176 drop_start = vec3_sub(drop_start, vec3_scale(horiz_dir, 0.05f));
1177 }
1178
1179 vec3 down_dir = {0,0,-1};
1180
1181
1182 hit = raycast_scene(drop_start, down_dir, INFINITY, false);
1183
1184 vec3 final_target = hit.pos;
1185 if (!hit.hit || vec3_dist(hit.pos, leg->pos) > 3.0f)
1186 {
1187 leg->stepping_off_ledge = true;
1188 final_target = sweep_end;
1189 final_target.z -= step_height;
1190 }
1191 else
1192 {
1193 leg->stepping_off_ledge = false;
1194 }
1195
1196 if (leg->stepping)
1197 {
1198
1199 leg->time += dt;
1200 float t = MIN(leg->time / leg->duration, 1.0f);
1201 vec3 current = vec3_lerp(leg->start_pos, leg->target_pos, t);
1202
1203 float height_offset = 4.0f * step_height * t * (1.0f - t);
1204 leg->pos = current;
1205 leg->pos.z += height_offset;
1206
1207
1208 if (leg->time > leg->duration)
1209 {
1210 leg->stepping = false;
1211 leg->time = 0;
1213 return true;
1214 }
1215 }
1216 else
1217 {
1218 float dist = vec3_length(vec3_sub(final_target, leg->pos));
1219
1220 if (dist > desired_stride * 0.4f && !(other_leg->stepping))
1221 {
1222
1223
1224 leg->stepping = true;
1225 leg->time = 0;
1226 leg->duration = clamp(dist / current_speed, 0.2f, 0.6f);
1227 leg->start_pos = leg->pos;
1228 leg->target_pos = final_target;
1229
1230 }
1231
1232 }
1233
1234 return false;
1235}
1236
1237
1238
1239static void update_spring(Spring * s)
1240{
1241
1242 vec3 diff = vec3_sub(s->pos, s->target);
1243 vec3 F = vec3_scale(diff, -s->stiff);
1244 vec3 damp = vec3_scale(s->velocity, -s->damp);
1245
1246 vec3 acc = vec3_add(F, damp);
1247
1248 s->velocity = vec3_add(s->velocity, vec3_scale(acc, dt));
1249 s->pos = vec3_add(s->pos, vec3_scale(s->velocity, dt));
1250
1251}
1252
1253static void update_angular_spring(Spring * s, float dt) {
1254
1255 vec3 displacement = vec3_sub(s->rotation, s->target_rot);
1256
1257 displacement.x = wrap_angle_pi(displacement.x);
1258 displacement.y = wrap_angle_pi(displacement.y);
1259 displacement.z = wrap_angle_pi(displacement.z);
1260
1261 vec3 spring_torque = vec3_scale(displacement, -s->stiff_rot);
1262 vec3 damping_torque = vec3_scale(s->angular_vel, -s->damp_rot);
1263
1264 vec3 total_torque = vec3_add(spring_torque, damping_torque);
1265
1266 s->angular_vel = vec3_add(s->angular_vel, vec3_scale(total_torque, dt));
1268
1269}
1270
1271
1272static void calculate_camera_angles(Camera * cam, Spring* s, vec3 input_acceleration, float dt) {
1273
1274 vec3 base_look_rot = (vec3){cam->yaw, cam->pitch, cam->roll};
1275
1276 float right_accel = vec3_dot(cam->right, input_acceleration);
1277
1278 float target_roll = right_accel * 0.01f;
1279
1280 s->target_rot = base_look_rot;
1281 s->target_rot.z = target_roll;
1282
1284
1285}
1286
1288{
1289
1290 mat3 rot;
1291 rot.a0 = cam->forward.x;
1292 rot.a1 = cam->forward.y;
1293 rot.a2 = cam->forward.z;
1294 rot.b0 = cam->right.x;
1295 rot.b1 = cam->right.y;
1296 rot.b2 = cam->right.z;
1297 rot.c0 = cam->up.x;
1298 rot.c1 = cam->up.y;
1299 rot.c2 = cam->up.z;
1300
1303
1304 s->left_hip_pos = vec3_add(s->pelvis.pos, left_tr);
1305 s->left_hip_pos.z -= 0.02f;
1306
1307 s->right_hip_pos = vec3_add(s->pelvis.pos, right_tr);
1308 s->right_hip_pos.z -= 0.02f;
1309
1310 bool hit_floor = false;
1311
1312 bool left_hit_floor = update_leg(&s->left_leg, &s->right_leg, vel);
1313 bool right_hit_floor = update_leg(&s->right_leg, &s->left_leg, vel);
1314
1315 s->p_left = s->left_leg.pos;
1316 s->p_left = vec3_add(s->p_left, left_tr);
1317 s->p_right = s->right_leg.pos;
1318 s->p_right = vec3_add(s->p_right, right_tr);
1319
1321 bool solved = solve_Q(&player.skeleton, D, &s->q_left, &s->p_left, s->left_hip_pos);
1322 bool solved2 = solve_Q(&player.skeleton, D, &s->q_right, &s->p_right, s->right_hip_pos);
1323
1324 float leg_length = player.skeleton.hip_height * 0.95f;
1325
1326 float left_length;
1327 float right_length;
1328
1329 left_length = vec3_length(vec3_sub(s->p_left, s->left_hip_pos));
1330 right_length = vec3_length(vec3_sub(s->p_right, s->right_hip_pos));
1331
1332 float left_drop = MAX(left_length - leg_length, 0);
1333
1334 float right_drop = MAX(right_length - leg_length, 0);
1335
1336 float vel_len = vec3_length(player.pc->vel);
1337
1338 float drop = MAX(left_drop, right_drop);
1339
1340 bool left_drop_higher = left_drop > right_drop;
1341
1342 vec3 base_pos = vec3_scale(vec3_add(s->left_leg.pos, s->right_leg.pos), 0.5);
1343 s->pelvis.target = base_pos;
1344 s->pelvis.target.z -= drop;
1345 s->pelvis.target.z += s->pelvis_height;
1346 s->pelvis.target.z = MAX(s->pelvis.target.z, base_pos.z);
1347 update_spring(&s->pelvis);
1348
1349 float pelvis_drop_delta = (base_pos.z + s->pelvis_height) - s->pelvis.pos.z;
1350 vec3 neck_target = s->pelvis.pos;
1351 neck_target.z += s->neck_pelvis_diff + (pelvis_drop_delta * 0.5f);
1352 s->neck.target = neck_target;
1353 update_spring(&s->neck);
1354
1355 vec3 current_accel;
1356 current_accel.x = (player.pc->vel.x - player.pc->old_vel.x) / dt;
1357 current_accel.y = (player.pc->vel.y - player.pc->old_vel.y) / dt;
1358 current_accel.z = (player.pc->vel.z - player.pc->old_vel.z) / dt;
1359
1361
1363
1364
1365}
1366
1367
1368
1369
1370
1371#define MIN_WALKABLE_SURFACE 0.5f
1372#define STEP_HEIGHT 0.6f
1373#define RAYCAST_HEIGHT 0.6f
1374
1375
1376void
1378 (
1379 Camera * cam
1380 )
1381
1382{
1383
1384 ground_hit = false;
1385
1386 Mesh * meshes = CAST_VEC(GLOBALS.meshes, Mesh);
1388
1389
1390 vec3 cam_pos = player.pos;
1391 vec3 cam_vel = player.pc->vel;
1392 vec3 proposed = vec3_add(cam_pos, vec3_scale(cam_vel, dt));
1393 vec3 raycast_proposed = cam_pos;
1394 raycast_proposed.z += RAYCAST_HEIGHT;
1395
1396 vec3 dir = {0,0,-1};
1397 vec3 inv_dir= {1.0f/dir.x, 1.0f/dir.y, 1.0f/dir.z};
1399
1400 Mesh * m = &meshes[0];
1401 MeshNode * mn = &mns[0];
1402
1403 MeshFace * mfs = CAST_VEC(m->tris, MeshFace);
1404 Point * pts = CAST_VEC(m->vertices, Point);
1405
1406 RayHit hit = raycast_scene(raycast_proposed, dir, INFINITY, true);
1407
1409 memset(p, 0, sizeof(CollisionPoints) * MAX_PLAYER_CONTACTS);
1410 for (int i = 0; i < MAX_PLAYER_CONTACTS; i++)
1411 {
1412
1413 p[i].d = -INFINITY;
1414
1415 }
1416 CollisionPoints * current_p = p;
1417 int current_count = 0;
1418
1419
1420 int contact_count = 0;
1421 player_find_collision(cam_pos, proposed, p, &contact_count);
1422
1423 bool all_walkable = true;
1424 float current_height = 0.0f;
1425 for (int i = 0; i < contact_count; i++)
1426 {
1427 CollisionPoints * pt = &p[i];
1428 MeshNode * mn = &mns[pt->mnB];
1429 if (pt->normal.z <= MIN_WALKABLE_SURFACE)
1430 {
1431 all_walkable = false;
1432
1433 }
1434 else
1435 {
1436 current_height = pt->a.z;
1437 }
1438 }
1439
1440
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++)
1445 {
1446 CollisionPoints * pt = &p[i];
1447 MeshNode * mn = &mns[pt->mnB];
1448 if (pt->normal.z > MIN_WALKABLE_SURFACE)
1449 {
1450 on_ground = true;
1451 ground_hit = true;
1452 player.cam->on_ground = true;
1453 player.pc->vel.z = 0;
1454 }
1455
1456 if (mn->pc != NULL)
1457 {
1458
1459 mn->pc->at_rest = false;
1460 }
1461 int parent = mn->parent;
1462 bool impulse = false;
1463 while (parent != -1)
1464 {
1465 MeshNode * mn0 = &mns[parent];
1466 if (mn0->pc)
1467 {
1468
1469 mn0->pc->at_rest = false;
1470
1471 pt->b = mat4_mul_point(mn->local_matrix, pt->b);
1472
1473 pt->mnB = mn0->id;
1474 break;
1475 }
1476 parent = mn0->parent;
1477 }
1478 if (impulse == true) break;
1479 }
1480 for (int i = 0; i < 12; i++)
1481 {
1482
1483 for (int j = 0; j < contact_count; j++)
1484 {
1485 CollisionPoints * pt = &p[j];
1486 MeshNode * mn = &mns[pt->mnB];
1487 if (mn->static_mesh)
1488 {
1490
1491 }
1492 else
1493 {
1495 }
1496
1497 }
1498 }
1499
1500
1501
1502 player.pos = vec3_add(cam_pos, vec3_scale(player.pc->vel, dt));
1503 if (!on_ground) player.cam->on_ground = false;
1504
1505
1506}
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.
RayHit hit
bool ground_hit
Definition camera.c:704
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 ...
Definition camera.c:1135
void update_light_camera(Camera *lcam, Light *light)
Updates a directional light camera and locks it to texel size.
Definition camera.c:298
bool collision(vec3 proposed, vec3 pts[3], vec3 normal, CollisionPoints *p)
Definition camera.c:443
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.
Definition camera.c:1272
#define RAYCAST_HEIGHT
How high legs are raised to detect for steps.
Definition camera.c:1373
mat4 make_ortho(float left, float right, float bottom, float top, float near, float far)
Makes an 4x4 orthographic matrix.
Definition camera.c:179
void reset_skeleton(Skeleton *s, vec3 pos)
Resets a skeleton to be relative to a new position.
Definition camera.c:963
void update_spring(Spring *s)
Updates a Spring's velocity and position based on the difference between its current position and tar...
Definition camera.c:1239
void define_M(vec3 P, vec3 D, vec3 minv[3], vec3 mfwd[3])
Refer to Ken Perlin's 2 joint IK implmentation.
Definition camera.c:982
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.
Definition camera.c:561
vec3 rot(vec3 M[3], vec3 a)
Refer to Ken Perlin's 2 joint IK implmentation.
Definition camera.c:1004
void update_skeleton(Skeleton *s, Camera *cam, vec3 vel)
Moves feet, then solves a skeleton's leg IKs and updates the springs accordingly.
Definition camera.c:1287
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.
Definition camera.c:710
void resolve_collisions(Camera *cam)
Resolves collisions between camera and geometry, uses player.
Definition camera.c:1378
void apply_velocity(Camera *cam, vec3 move_dir, float dt)
Applies no-clip mode velocity to player with no gravity applied.
Definition camera.c:660
#define MIN_WALKABLE_SURFACE
How steep the surface is for it to be walkable.
Definition camera.c:1371
void compute_camera_basis(Camera *cam, float yaw, float pitch, float roll, vec3 world_up)
Computes camera vectors based on yaw, pitch, and roll.
Definition camera.c:136
const float HALTON_8[8][2]
Definition camera.c:464
mat4 make_look_at(Camera *cam, vec3 eye)
Makes a 4x4 view matrix using camera vectors.
Definition camera.c:258
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.
Definition camera.c:1052
void update_camera(Skeleton *s, Camera *cam)
Updates a camera based on a skeleton's current neck and body position.
Definition camera.c:477
mat4 make_projection(float fov_deg, float aspect, float near, float far)
Makes an 4x4 projection matrix.
Definition camera.c:221
bool solve_Q(Skeleton *s, vec3 D, vec3 *Q, vec3 *P, vec3 hip_pos)
Refer to Ken Perlin's 2 joint IK implmentation.
Definition camera.c:1016
void update_angular_spring(Spring *s, float dt)
Updates a Spring's angular velocity and rotation based on the difference between its current rotation...
Definition camera.c:1253
void apply_planet_velocity(Camera *cam, vec3 move_dir, float dt)
Applies normal velocity to player with gravity and friction applied.
Definition camera.c:594
Functions for camera.
Camera cam
This is currently the player cam.
Definition main.c:322
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.
float roll
bool on_ground
If the camera is on the ground.
bool noclip
If collision is enabled.
Vector mesh_nodes
MeshNodes.
float stiff
Stiffness.
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.
Vector bvh_nodes
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.
Vector meshes
Meshes.
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.
Definition main.c:310
float damp_rot
Dampening for rotation.
float damp
Dampening.
mat4 inv_view
The inverse view matrix.
vec3 pos
Position of the foot.
vec3 forward
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.
vec3 target
bool stepping_off_ledge
If the leg is over air.
mat4 view
The view matrix.
float pitch
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.
LegIK left_leg
Left leg.
vec3 pos
Position of the spring.
float yaw
mat4 proj
The projection matrix.
Camera * cam
Player Camera.
CamData cam_data
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.
size_t * bvh_indices
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.
Definition audio.c:853
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 vel
Velocity.
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.
float d
Length of B-A.
vec3 a
Furthest point of A into B.
CollisionType type
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.
Definition physics.c:1097
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.
Definition GJK.c:1052
void player_resolve_impulse(PhysicsComponent *pcA, PhysicsComponent *pcB, CollisionPoints *p)
Solves an impulse for a Player PhysicsComponent against another PhysicsComponent.
Definition physics.c:1167
@ 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.
mat4 local_matrix
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...
int master_frame
Definition render.c:21
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 nodes
BVHNodes.
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.
EPAWorkspace * EW[8]
Definition main.c:291
int RENDER_HEIGHT
Definition main.c:80
int RENDER_WIDTH
Definition main.c:79
vec2 JITTER_OFFSET
Definition main.c:181
EditorSettings EDITOR_SETTINGS
Definition main.c:330
AudioEmitter * FOOTSTEP_EMITTER
Definition main.c:149
const vec3 right_leg_offset
Definition main.c:392
float dt
The 1.0f/60 simulation timestep.
Definition main.c:319
Framebuffer SHADOW_MAP_FBO
Definition main.c:92
const vec3 left_leg_offset
Definition main.c:391
Player player
The Player data.
Definition main.c:287
vec3 vec3_cross(vec3 a, vec3 b)
Definition mathf.h:80
float vec3_dist(vec3 a, vec3 b)
Definition mathf.h:318
vec3 vec3_lerp(vec3 a, vec3 b, float t)
Definition mathf.h:375
float vec3_dot(vec3 a, vec3 b)
Definition mathf.h:322
#define MIN(a, b)
Definition mathf.h:435
vec2 vec2_normalize(vec2 v)
Definition mathf.h:38
vec3 vec3_normalize(vec3 v)
Definition mathf.h:46
vec3 vec3_scale(vec3 a, float b)
Definition mathf.h:248
vec3 vec3_sub(vec3 a, vec3 b)
Definition mathf.h:266
vec3 vec3_add(vec3 a, vec3 b)
Definition mathf.h:286
vec2 vec2_sub(vec2 a, vec2 b)
Definition mathf.h:270
float clamp(float a, float b, float c)
Definition mathf.h:379
bool m4_inverse(float out[16], const float m[16])
Definition mathf.h:164
float vec2_length(vec2 v)
Definition mathf.h:336
float vec3_length(vec3 v)
Definition mathf.h:328
#define MAX(a, b)
Definition mathf.h:433
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...
int right
Right child.
AABB aabb
The bounding box of all of this node's children.
int left
Left child.
Nodes for the acceleration structures of BVHs.
float x
Definition types.h:11
float a1
Definition types.h:21
float z
Definition types.h:13
float d3
Definition types.h:21
float c2
Definition types.h:21
float b0
Definition types.h:21
float c1
Definition types.h:21
float x
Definition types.h:13
float a2
Definition types.h:21
@ B
Definition types.h:26
@ A
Definition types.h:26
@ C
Definition types.h:26
@ D
Definition types.h:26
float x
Definition types.h:12
float b3
Definition types.h:21
float y
Definition types.h:11
float y
Definition types.h:13
float y
Definition types.h:12
float d1
Definition types.h:21
float a3
Definition types.h:21
float c0
Definition types.h:21
float z
Definition types.h:12
size_t count
Definition types.h:209
float b2
Definition types.h:21
float d0
Definition types.h:21
void * buffer
Definition types.h:210
float a0
Definition types.h:21
float c3
Definition types.h:21
float b1
Definition types.h:21
float d2
Definition types.h:21
Definition types.h:218
Definition types.h:20
Definition types.h:21
Definition types.h:11
Definition types.h:12
Definition types.h:13
bool aabbs_intersect(AABB *a, AABB *b)
Definition utilities.c:276
AABB transform_aabb_by_position(AABB aabb, vec3 pos)
Definition utilities.c:466
vec3 closest_point_triangle(vec3 p, vec3 a, vec3 b, vec3 c)
Definition utilities.c:1327
vec3 mat3_mul_vec3(mat3 m, vec3 v)
Definition utilities.h:498
vec4 vec3_to_vec4(vec3 v)
Definition utilities.h:166
#define CAST_VEC(vec, type)
Definition utilities.h:134
mat4 compose_trs_from_vectors(vec3 pos, vec4 rot, vec3 scale)
Definition utilities.h:481
vec3 mat4_mul_direction(const mat4 m, vec3 v)
Definition utilities.h:323
float wrap_angle_pi(float angle)
Definition utilities.h:57
vec3 mat4_mul_point(mat4 m, vec3 v)
Definition utilities.h:313
#define M_PI
Definition utilities.h:52