Skip to content

Commit c47552d

Browse files
switched simulated bodies to use cube colliders
1 parent f883dd6 commit c47552d

3 files changed

Lines changed: 48 additions & 21 deletions

File tree

include/sim/cube.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,11 @@ bool ccube_is_sphere_inside(ccube_t cube, csphere_t sphere);
5252
* Checks if two cubes are overlapping
5353
*/
5454
bool ccube_is_ccube_inside(ccube_t a, ccube_t b);
55+
56+
/**
57+
* @brief Gets the surface normal of a point on a given cube
58+
* @param cube The cube to get the surface normal of
59+
* @param point_on_surface The point where the normal will start
60+
* @return The surface normal
61+
*/
62+
vec3_t ccube_get_surface_normal(ccube_t cube, vec3_t point_on_surface);

src/main/graphicmain.c

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
#include <malloc.h>
77
#include <gl_includes.h>
88
#include <cglm/cglm.h>
9-
#include "sim/aabb.h"
9+
#include "sim/cube.h"
1010
#include "sim/body.h"
1111
#include "common/defines.h"
1212
#include "viewer/window.h"
1313
#include "viewer/shader.h"
1414
#include "viewer/color.h"
1515
#include "viewer/camera.h"
16-
#include "viewer/aabb.h"
16+
#include "viewer/cube.h"
1717
#include "viewer/body.h"
1818
#include "viewer/model.h"
1919

@@ -25,10 +25,10 @@ const color_t WINDOW_BACKGROUND_COLOR = COLOR_PURPLE;
2525
#include "shaders/matrix_vertex.h"
2626
#include "shaders/uniform_color_fragment.h"
2727

28-
static GLfloat box1_vertices[BBOX_VERTEX_ARRAY_SIZE];
29-
static GLuint box1_indices[BBOX_INDEX_ARRAY_SIZE];
30-
static GLfloat box2_vertices[BBOX_VERTEX_ARRAY_SIZE];
31-
static GLuint box2_indices[BBOX_INDEX_ARRAY_SIZE];
28+
static GLfloat cube1_vertices[CCUBE_VERTEX_ARRAY_SIZE];
29+
static GLuint cube1_indices[CCUBE_INDEX_ARRAY_SIZE];
30+
static GLfloat cube2_vertices[CCUBE_VERTEX_ARRAY_SIZE];
31+
static GLuint cube2_indices[CCUBE_INDEX_ARRAY_SIZE];
3232

3333
#define CAMERA_MOVE_SPEED 0.05
3434
#define CAMERA_ROTATE_SPEED 0.0025
@@ -45,14 +45,13 @@ int graphic_main(void) {
4545
return result;
4646
}
4747

48-
bbox_t box1, box2;
49-
bbox_make(&box1, 0, 0, 0, 3, 3, 3);
50-
bbox_make(&box2, 0, 0, 0, 2, 2, 2);
48+
ccube_t cube1 = ccube_make(VEC3_ZERO, QUATERNION_NOROTATION, 3, 3, 3);
49+
ccube_t cube2 = ccube_make(VEC3_ZERO, QUATERNION_NOROTATION, 2, 2, 2);
5150
body_t body1, body2;
52-
body_make(&body1, vec3_make(1, 0, 0), VEC3_ZERO, vec3_make(0, 1, 0), VEC3_ZERO, 1, 0, 0);
53-
body_make(&body2, vec3_make(0, 10, 0), VEC3_ZERO, vec3_make(0, -0.5, 0), VEC3_ZERO, 1, 0, 0);
54-
bbox_gen_vertices(box1, box1_vertices, box1_indices);
55-
bbox_gen_vertices(box2, box2_vertices, box2_indices);
51+
body_make(&body1, vec3_make(1, -5, 0), VEC3_ZERO, vec3_make(0, 1, 0), VEC3_ZERO, 1, 0, 0);
52+
body_make(&body2, vec3_make(0, 5, 0), VEC3_ZERO, vec3_make(0, -1, 0), VEC3_ZERO, 1, 0, 0);
53+
ccube_gen_vertices(cube1, cube1_vertices, cube1_indices);
54+
ccube_gen_vertices(cube2, cube2_vertices, cube2_indices);
5655

5756
l_printf("Building shaders...\n");
5857

@@ -93,8 +92,8 @@ int graphic_main(void) {
9392

9493
l_printf("Shaders successfully built!\n");
9594

96-
model_t box1_model = model_from_indices(box1_vertices, 3, BBOX_VERTEX_ARRAY_SIZE, MODEL_BUFFER_STATIC, box1_indices, BBOX_INDEX_ARRAY_SIZE, MODEL_BUFFER_STATIC, MODEL_DRAW_TRIANGLES);
97-
model_t box2_model = model_from_indices(box2_vertices, 3, BBOX_VERTEX_ARRAY_SIZE, MODEL_BUFFER_STATIC, box2_indices, BBOX_INDEX_ARRAY_SIZE, MODEL_BUFFER_STATIC, MODEL_DRAW_TRIANGLES);
95+
model_t box1_model = model_from_indices(cube1_vertices, 3, CCUBE_VERTEX_ARRAY_SIZE, MODEL_BUFFER_STATIC, cube1_indices, CCUBE_INDEX_ARRAY_SIZE, MODEL_BUFFER_STATIC, MODEL_DRAW_TRIANGLES);
96+
model_t box2_model = model_from_indices(cube2_vertices, 3, CCUBE_VERTEX_ARRAY_SIZE, MODEL_BUFFER_STATIC, cube2_indices, CCUBE_INDEX_ARRAY_SIZE, MODEL_BUFFER_STATIC, MODEL_DRAW_TRIANGLES);
9897

9998
color_t color1 = COLOR_GREEN;
10099
color_t color2 = COLOR_BLUE;
@@ -134,12 +133,12 @@ int graphic_main(void) {
134133
phy_body_add_gravity_force(&body1, &body2);
135134

136135
#ifndef NOCOLLISION
137-
if (bbox_is_bbox_inside(box1, box2)) {
136+
if (ccube_is_ccube_inside(cube1, cube2)) {
138137
// a and b are colliding; find normal
139138
// and do collision forces
140-
vec3_t b_closest_a = box2.position;
141-
bbox_clamp_point_within_bounds(box1, &b_closest_a);
142-
vec3_t normal_a_b = bbox_get_surface_normal(box1, b_closest_a);
139+
vec3_t b_closest_a = cube2.position;
140+
ccube_clamp_point_within_cube(cube1, &b_closest_a);
141+
vec3_t normal_a_b = ccube_get_surface_normal(cube1, b_closest_a);
143142
phy_calculate_normal_force(&normal_a_b, body1, normal_a_b);
144143
phy_body_add_collision_forces(&body1, &body2, normal_a_b, b_closest_a);
145144
}
@@ -149,8 +148,10 @@ int graphic_main(void) {
149148
phy_body_step(&body2);
150149
}
151150

152-
box1.position = body1.position;
153-
box2.position = body2.position;
151+
cube1.position = body1.position;
152+
glm_euler_xyz_quat(vec3_to_cglm(body1.rotation), vec4_to_cglm(cube1.rotation));
153+
cube2.position = body2.position;
154+
glm_euler_xyz_quat(vec3_to_cglm(body2.rotation), vec4_to_cglm(cube2.rotation));
154155

155156
// move camera up and down
156157
if (window_is_key_down(window, GLFW_KEY_E)) {

src/sim/cube.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,21 @@ bool ccube_is_ccube_inside(ccube_t a, ccube_t b) {
8282
ccube_clamp_point_within_cube(a, &a_closest_b);
8383
return ccube_is_point_inside(b, a_closest_b);
8484
}
85+
86+
vec3_t ccube_get_surface_normal(ccube_t cube, vec3_t point_on_surface) {
87+
// clamp the point and transform it so that it is relative to the cube's
88+
// center and within the cube's extents
89+
ccube_clamp_point_within_cube(cube, &point_on_surface);
90+
ccube_apply_cube_transformations(cube, &point_on_surface);
91+
92+
// now that we've applied transformations, we can treat this as a bounding
93+
// box operation
94+
bbox_t cube_box;
95+
bbox_make(&cube_box, 0, 0, 0, cube.length, cube.width, cube.height);
96+
bbox_get_surface_normal(cube_box, point_on_surface);
97+
98+
// finally, undo transformations to get the point back to world space
99+
ccube_undo_cube_transformations(cube, &point_on_surface);
100+
101+
return point_on_surface;
102+
}

0 commit comments

Comments
 (0)