simplified collecting contacts by removing expiry and just clearing the list

This commit is contained in:
Sara 2023-10-25 23:25:04 +02:00
parent 1e039c310b
commit 77c266b43f
2 changed files with 2 additions and 24 deletions

View file

@ -90,14 +90,13 @@ void physics_entity_update(PhysicsEntity self) {
ASSERT_RETURN(!visnanf(rigidbody_get_velocity(body)),, "Velocity is NaN (0)"); ASSERT_RETURN(!visnanf(rigidbody_get_velocity(body)),, "Velocity is NaN (0)");
rigidbody_collect_contacts(body);
List* contacts = rigidbody_get_contacts(body); List* contacts = rigidbody_get_contacts(body);
if(contacts->len > 0) { if(contacts->len > 0) {
self.tc->collision_solver(self.data, contacts); self.tc->collision_solver(self.data, contacts);
list_foreach(Contact, contact, contacts) list_foreach(Contact, contact, contacts)
self.tc->on_collision(self.data, contact->hit); self.tc->on_collision(self.data, contact->hit);
} }
rigidbody_collect_contacts(body);
ASSERT_RETURN(!visnanf(rigidbody_get_velocity(body)),, "Velocity is NaN (1)"); ASSERT_RETURN(!visnanf(rigidbody_get_velocity(body)),, "Velocity is NaN (1)");
} }

View file

@ -45,36 +45,15 @@ void rigidbody_destroy(RigidBody* self) {
} }
void rigidbody_add_contact(RigidBody* self, Collision hit) { void rigidbody_add_contact(RigidBody* self, Collision hit) {
// update an existing contact
list_foreach(Contact, contact, &self->contacts) {
if(contact->hit.other.data == hit.other.data) {
++contact->expiry;
contact->hit = hit;
contact->duration += delta_time();
return;
}
}
// create a new contact
list_add(&self->contacts, list_add(&self->contacts,
&(Contact) { &(Contact) {
.expiry = 1,
.hit = hit, .hit = hit,
.duration = delta_time() .duration = delta_time()
}); });
} }
void rigidbody_collect_contacts(RigidBody* self) { void rigidbody_collect_contacts(RigidBody* self) {
for(size_t i = 0; i < self->contacts.len; ++i) { list_empty(&self->contacts);
Contact* contact = list_at(&self->contacts, i);
if(contact->expiry <= 0) {
list_erase(&self->contacts, i);
i--;
} else {
--(contact->expiry);
}
}
} }
List* rigidbody_get_contacts(RigidBody* self) { List* rigidbody_get_contacts(RigidBody* self) {