Skip to content

Commit

Permalink
Implement collision groups, category and masks
Browse files Browse the repository at this point in the history
  • Loading branch information
kadir014 committed Nov 9, 2023
1 parent abd1a08 commit e2addcb
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 31 deletions.
11 changes: 7 additions & 4 deletions examples/chains.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ void setup(Example *example) {
(nv_Material){1.0, 0.0, 0.0},
width, height
);

chain_part->collision_group = 1;
nv_Space_add(example->space, chain_part);

// Temporary solution to avoid constraints exploding
nv_Body_apply_force(chain_part, NV_VEC2((nv_float)(i%10)*50.0, 0.0));
}

// Link chain parts
Expand Down Expand Up @@ -69,10 +72,10 @@ void setup(Example *example) {
(nv_Material){1.0, 0.0, 0.0},
radius
);

chain_part->collision_group = 2;
nv_Space_add(example->space, chain_part);

// Temporary solution to circle chain going crazy
// Temporary solution to avoid constraints exploding
nv_Body_apply_force(chain_part, NV_VEC2((nv_float)(i%10)*50.0, 0.0));
}

Expand Down Expand Up @@ -110,7 +113,7 @@ void setup(Example *example) {
(nv_Material){1.0, 0.0, 0.0},
size, size / 3.0
);

chain_part->collision_group = 3;
nv_Space_add(example->space, chain_part);
}

Expand Down
4 changes: 2 additions & 2 deletions examples/example_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,7 @@ void Example_run(Example *example) {
nv_Material_WOOD,
0.3
);
mouse_body->collision = false;
mouse_body->enable_collision = false;
nv_Space_add(example->space, mouse_body);
nv_Array_add(example->sprites, NULL);

Expand Down Expand Up @@ -2184,7 +2184,7 @@ void Example_run(Example *example) {
nv_Material_WOOD,
0.3
);
mouse_body->collision = false;
mouse_body->enable_collision = false;
nv_Space_add(example->space, mouse_body);

example->counter = 0;
Expand Down
44 changes: 22 additions & 22 deletions examples/spring_car.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

void update(Example *example) {
if (example->keys[SDL_SCANCODE_LEFT] || example->keys[SDL_SCANCODE_RIGHT]) {
nv_Body *wheel1 = (nv_Body *)example->space->bodies->data[4];
nv_Body *wheel1 = (nv_Body *)example->space->bodies->data[6];
nv_Body *wheel2 = (nv_Body *)example->space->bodies->data[5];

double strength = 7.0 * 1e2;
double strength = 12.0 * 1e2;
double limit = 30.0;

if (example->keys[SDL_SCANCODE_LEFT]) {
Expand Down Expand Up @@ -82,26 +82,26 @@ void setup(Example *example) {

// Create wheels

nv_Material wheel_mat = (nv_Material){1.5, 0.8, 3.0};
nv_Material wheel_mat = (nv_Material){1.5, 0.7, 3.0};

nv_Body *wheel1 = nv_Circle_new(
nv_BodyType_DYNAMIC,
NV_VEC2(49.0, 40.0),
NV_VEC2(53.0, 32.0),
0.0,
wheel_mat,
1.5
2.0
);

wheel1->collision_group = 1;
nv_Space_add(example->space, wheel1);

nv_Body *wheel2 = nv_Circle_new(
nv_BodyType_DYNAMIC,
NV_VEC2(61.0, 40.0),
NV_VEC2(57.0, 32.0),
0.0,
wheel_mat,
1.5
2.0
);

wheel2->collision_group = 1;
nv_Space_add(example->space, wheel2);


Expand All @@ -113,53 +113,53 @@ void setup(Example *example) {
(nv_Material){4.0, 0.3, 0.5},
10.0, 3.0
);

body->collision_group = 1;
nv_Space_add(example->space, body);


// Create spring constraints

double suspension_length = 2.5;
double suspension_strength = 1500.0;
double suspension_length = 2.3;
double suspension_strength = 2700.0;
double suspension_damping = 150.00;

nv_Constraint *spring1 = nv_Spring_new(
wheel1, body,
NV_VEC2(0.0, 0.0), NV_VEC2(-4.0, 1.5),
NV_VEC2(0.0, 0.0), NV_VEC2(-4.0, 0.0),
suspension_length,
suspension_strength,
suspension_strength * 1.0,
suspension_damping
);

nv_Space_add_constraint(example->space, spring1);

nv_Constraint *spring2 = nv_Spring_new(
wheel1, body,
NV_VEC2(0.0, 0.0), NV_VEC2(-1.5, 1.5),
NV_VEC2(0.0, 0.0), NV_VEC2(-1.5, 0.0),
suspension_length,
suspension_strength * 3.0,
suspension_damping
suspension_strength * 6.0,
suspension_damping * 2.0
);


nv_Space_add_constraint(example->space, spring2);

nv_Constraint *spring3 = nv_Spring_new(
wheel2, body,
NV_VEC2(0.0, 0.0), NV_VEC2(4.0, 1.5),
NV_VEC2(0.0, 0.0), NV_VEC2(4.0, 0.0),
suspension_length,
suspension_strength,
suspension_strength * 1.0,
suspension_damping
);

nv_Space_add_constraint(example->space, spring3);

nv_Constraint *spring4 = nv_Spring_new(
wheel2, body,
NV_VEC2(0.0, 0.0), NV_VEC2(1.5, 1.5),
NV_VEC2(0.0, 0.0), NV_VEC2(1.5, 0.0),
suspension_length,
suspension_strength * 3.0,
suspension_damping
suspension_strength * 6.0,
suspension_damping * 2.0
);

nv_Space_add_constraint(example->space, spring4);
Expand Down
6 changes: 5 additions & 1 deletion include/novaphysics/body.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ typedef struct {

bool is_attractor; /**< Flag reporting if the body is an attractor. */

bool collision;
bool enable_collision; /**< Whether to collide this body with other bodies or not. */
nv_uint32 collision_group; /**< Collision group of the body.
Bodies that share the same non-zero group do not collide. */
nv_uint32 collision_category; /**< Bitmask defining this body's collision category. */
nv_uint32 collision_mask; /**< Bitmask defining this body's collision mask. */
} nv_Body;

/**
Expand Down
5 changes: 4 additions & 1 deletion src/body.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ nv_Body *nv_Body_new(

body->is_attractor = false;

body->collision = true;
body->enable_collision = true;
body->collision_group = 0;
body->collision_category = 0b11111111111111111111111111111111;
body->collision_mask = 0b11111111111111111111111111111111;

nv_Body_calc_mass_and_inertia(body);

Expand Down
11 changes: 10 additions & 1 deletion src/broadphase.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static inline bool nv_BroadPhase_early_out(nv_Space *space, nv_Body *a, nv_Body
if (a == b)
return true;

if (!a->collision || !b->collision)
if (!a->enable_collision || !b->enable_collision)
return true;

// Two static bodies do not need to interact
Expand All @@ -48,6 +48,15 @@ static inline bool nv_BroadPhase_early_out(nv_Space *space, nv_Body *a, nv_Body
return true;
}

// Bodies share the same non-zero group
if (a->collision_group == b->collision_group && a->collision_group != 0)
return true;

// One of the collision mask doesn't fit the category
if (a->collision_mask & b->collision_category == 0 ||
b->collision_mask & a->collision_category == 0)
return true;

return false;
}

Expand Down

0 comments on commit e2addcb

Please sign in to comment.