asim.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #include "asim.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #define GRAVITY -9.81f
  7. #define MAX_BVH_DEPTH 20
  8. typedef struct {
  9. vec4_t min;
  10. vec4_t max;
  11. } aabb_t;
  12. typedef struct {
  13. vec4_t position;
  14. vec4_t velocity;
  15. float radius;
  16. float mass;
  17. } sphere_t;
  18. typedef struct {
  19. vec4_t v0;
  20. vec4_t v1;
  21. vec4_t v2;
  22. vec4_t normal;
  23. aabb_t bounds;
  24. } triangle_t;
  25. typedef struct bvh_node {
  26. aabb_t bounds;
  27. struct bvh_node *left, *right;
  28. triangle_t *triangles;
  29. int num_tris;
  30. int is_leaf;
  31. } bvh_node_t;
  32. typedef struct {
  33. bvh_node_t *root;
  34. } mesh_t;
  35. sphere_t sphere = {
  36. .position = {0, 0, 0},
  37. .velocity = {0, 0, 0},
  38. // .radius = 1.0f,
  39. .radius = 0.2f,
  40. .mass = 1.0f
  41. };
  42. mesh_t mesh;
  43. aabb_t root_bounds = {
  44. {-10, -10, -10},
  45. {10, 10, 10}
  46. };
  47. physics_pair_t ppair;
  48. static inline aabb_t merge_aabbs(aabb_t a, aabb_t b) {
  49. return (aabb_t){
  50. .min = {fminf(a.min.x, b.min.x), fminf(a.min.y, b.min.y), fminf(a.min.z, b.min.z)},
  51. .max = {fmaxf(a.max.x, b.max.x), fmaxf(a.max.y, b.max.y), fmaxf(a.max.z, b.max.z)}
  52. };
  53. }
  54. static inline int sphere_aabb_intersect(sphere_t *s, aabb_t *a) {
  55. float x = fmaxf(a->min.x, fminf(s->position.x, a->max.x));
  56. float y = fmaxf(a->min.y, fminf(s->position.y, a->max.y));
  57. float z = fmaxf(a->min.z, fminf(s->position.z, a->max.z));
  58. float dx = x - s->position.x, dy = y - s->position.y, dz = z - s->position.z;
  59. return dx * dx + dy * dy + dz * dz <= s->radius * s->radius;
  60. }
  61. // Comparison function for sorting triangles by x-coordinate of centroid
  62. static int compare_triangles(const void *a, const void *b) {
  63. triangle_t *ta = (triangle_t *)a;
  64. triangle_t *tb = (triangle_t *)b;
  65. vec4_t ca = vec4_mult(vec4_add(vec4_add(ta->v0, ta->v1), ta->v2), 1.0f / 3.0f);
  66. vec4_t cb = vec4_mult(vec4_add(vec4_add(tb->v0, tb->v1), tb->v2), 1.0f / 3.0f);
  67. return (ca.x > cb.x) - (ca.x < cb.x);
  68. }
  69. static bvh_node_t *create_bvh_node(triangle_t *tris, int num_tris, int depth) {
  70. bvh_node_t *node = (bvh_node_t *)malloc(sizeof(bvh_node_t));
  71. *node = (bvh_node_t){
  72. .left = NULL,
  73. .right = NULL,
  74. .triangles = NULL,
  75. .num_tris = 0,
  76. .is_leaf = 1
  77. };
  78. if (num_tris <= 1 || depth >= MAX_BVH_DEPTH) {
  79. node->num_tris = num_tris;
  80. if (num_tris) {
  81. node->triangles = (triangle_t *)malloc(num_tris * sizeof(triangle_t));
  82. memcpy(node->triangles, tris, num_tris * sizeof(triangle_t));
  83. node->bounds = tris[0].bounds;
  84. }
  85. else {
  86. node->bounds = root_bounds;
  87. }
  88. return node;
  89. }
  90. qsort(tris, num_tris, sizeof(triangle_t), compare_triangles);
  91. int mid = num_tris / 2;
  92. node->is_leaf = 0;
  93. node->left = create_bvh_node(tris, mid, depth + 1);
  94. node->right = create_bvh_node(tris + mid, num_tris - mid, depth + 1);
  95. node->bounds = merge_aabbs(node->left->bounds, node->right->bounds);
  96. return node;
  97. }
  98. static void collide_sphere_triangle(sphere_t *s, triangle_t *t) {
  99. if (!sphere_aabb_intersect(s, &t->bounds)) {
  100. return;
  101. }
  102. vec4_t to_sphere = vec4_sub(s->position, t->v0);
  103. float dist = vec4_dot(to_sphere, t->normal);
  104. if (fabsf(dist) > s->radius) {
  105. return;
  106. }
  107. vec4_t p = vec4_sub(s->position, vec4_mult(t->normal, dist));
  108. vec4_t e0 = vec4_sub(t->v1, t->v0), e1 = vec4_sub(t->v2, t->v1), e2 = vec4_sub(t->v0, t->v2);
  109. vec4_t c0 = vec4_sub(p, t->v0), c1 = vec4_sub(p, t->v1), c2 = vec4_sub(p, t->v2);
  110. if (vec4_dot(t->normal, vec4_cross(e0, c0)) >= 0 &&
  111. vec4_dot(t->normal, vec4_cross(e1, c1)) >= 0 &&
  112. vec4_dot(t->normal, vec4_cross(e2, c2)) >= 0) {
  113. float penetration = s->radius - dist;
  114. if (dist < 0) {
  115. penetration = -penetration;
  116. }
  117. s->position = vec4_add(s->position, vec4_mult(t->normal, penetration));
  118. float v_dot_n = vec4_dot(s->velocity, t->normal);
  119. vec4_t n_vel = vec4_mult(t->normal, v_dot_n);
  120. s->velocity = vec4_add(vec4_sub(s->velocity, n_vel), vec4_mult(n_vel, -0.01f));
  121. vec4_t contact_point = vec4_sub(s->position, vec4_mult(t->normal, s->radius));
  122. ppair.pos_a_x = contact_point.x;
  123. ppair.pos_a_y = contact_point.y;
  124. ppair.pos_a_z = contact_point.z;
  125. }
  126. }
  127. static void query_bvh(sphere_t *s, bvh_node_t *n) {
  128. if (!n || !sphere_aabb_intersect(s, &n->bounds)) {
  129. return;
  130. }
  131. if (n->is_leaf) {
  132. for (int i = 0; i < n->num_tris; i++) {
  133. collide_sphere_triangle(s, &n->triangles[i]);
  134. }
  135. }
  136. else {
  137. query_bvh(s, n->left);
  138. query_bvh(s, n->right);
  139. }
  140. }
  141. static void free_bvh(bvh_node_t *n) {
  142. if (!n) {
  143. return;
  144. }
  145. if (n->is_leaf) {
  146. free(n->triangles);
  147. }
  148. else {
  149. free_bvh(n->left);
  150. free_bvh(n->right);
  151. }
  152. free(n);
  153. }
  154. void asim_world_create() {
  155. }
  156. void asim_world_destroy() {
  157. free_bvh(mesh.root);
  158. mesh.root = NULL;
  159. }
  160. void asim_world_update(float time_step) {
  161. ppair.pos_a_x = 0;
  162. ppair.pos_a_y = 0;
  163. ppair.pos_a_z = 0;
  164. const int sub_steps = 2;
  165. float dt = time_step / sub_steps;
  166. for (int i = 0; i < sub_steps; i++) {
  167. sphere.velocity.z += GRAVITY * dt;
  168. sphere.position = vec4_add(sphere.position, vec4_mult(sphere.velocity, dt));
  169. query_bvh(&sphere, mesh.root);
  170. }
  171. }
  172. physics_pair_t *asim_world_get_contact() {
  173. return &ppair;
  174. }
  175. void *asim_body_create(int shape, float mass, float dimx, float dimy, float dimz, float x, float y, float z, void *posa, void *inda, float scale_pos) {
  176. if (shape == 1) { // SPHERE
  177. sphere.position.x = x;
  178. sphere.position.y = y;
  179. sphere.position.z = z;
  180. sphere.velocity.x = 0;
  181. sphere.velocity.y = 0;
  182. sphere.velocity.z = 0;
  183. return NULL;
  184. }
  185. i16_array_t *pa = posa;
  186. u32_array_t *ia = inda;
  187. int num_tris = ia->length / 3;
  188. triangle_t *tris = (triangle_t *)malloc(num_tris * sizeof(triangle_t));
  189. float scale = (1.0 / 32767.0) * scale_pos;
  190. for (int i = 0; i < num_tris; i++) {
  191. tris[i].v0 = (vec4_t){pa->buffer[ia->buffer[i * 3 ] * 4 ] * scale,
  192. pa->buffer[ia->buffer[i * 3 ] * 4 + 1] * scale,
  193. pa->buffer[ia->buffer[i * 3 ] * 4 + 2] * scale};
  194. tris[i].v1 = (vec4_t){pa->buffer[ia->buffer[i * 3 + 1] * 4 ] * scale,
  195. pa->buffer[ia->buffer[i * 3 + 1] * 4 + 1] * scale,
  196. pa->buffer[ia->buffer[i * 3 + 1] * 4 + 2] * scale};
  197. tris[i].v2 = (vec4_t){pa->buffer[ia->buffer[i * 3 + 2] * 4 ] * scale,
  198. pa->buffer[ia->buffer[i * 3 + 2] * 4 + 1] * scale,
  199. pa->buffer[ia->buffer[i * 3 + 2] * 4 + 2] * scale};
  200. vec4_t edge1 = vec4_sub(tris[i].v1, tris[i].v0);
  201. vec4_t edge2 = vec4_sub(tris[i].v2, tris[i].v0);
  202. tris[i].normal = vec4_mult(vec4_cross(edge1, edge2), 1.0f / vec4_len(vec4_cross(edge1, edge2)));
  203. tris[i].bounds = (aabb_t){
  204. .min = {fminf(fminf(tris[i].v0.x, tris[i].v1.x), tris[i].v2.x),
  205. fminf(fminf(tris[i].v0.y, tris[i].v1.y), tris[i].v2.y),
  206. fminf(fminf(tris[i].v0.z, tris[i].v1.z), tris[i].v2.z)},
  207. .max = {fmaxf(fmaxf(tris[i].v0.x, tris[i].v1.x), tris[i].v2.x),
  208. fmaxf(fmaxf(tris[i].v0.y, tris[i].v1.y), tris[i].v2.y),
  209. fmaxf(fmaxf(tris[i].v0.z, tris[i].v1.z), tris[i].v2.z)}
  210. };
  211. }
  212. mesh.root = create_bvh_node(tris, num_tris, 0);
  213. free(tris);
  214. return NULL;
  215. }
  216. void asim_body_apply_impulse(void *body, float x, float y, float z) {
  217. sphere.velocity.x += x;
  218. sphere.velocity.y += y;
  219. sphere.velocity.z += z;
  220. }
  221. void asim_body_get_pos(void *body, vec4_t *pos) {
  222. pos->x = sphere.position.x;
  223. pos->y = sphere.position.y;
  224. pos->z = sphere.position.z;
  225. }
  226. void asim_body_get_rot(void *body, quat_t *rot) {
  227. }
  228. void asim_body_sync_transform(void *body, vec4_t pos, quat_t rot) {
  229. sphere.position.x = pos.x;
  230. sphere.position.y = pos.y;
  231. sphere.position.z = pos.z;
  232. }
  233. void asim_body_remove(void *body) {
  234. }