From e0a05e546e2e8c8fb330585026fa55a1c995b16a Mon Sep 17 00:00:00 2001 From: Sara Date: Sun, 26 Nov 2023 13:06:36 +0100 Subject: [PATCH] feat: added physics world queries --- core/src/physics_world.c | 45 ++++++++++++++++++++++++++++++---------- core/src/physics_world.h | 5 +++-- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/core/src/physics_world.c b/core/src/physics_world.c index 2f03102..a5d80e7 100644 --- a/core/src/physics_world.c +++ b/core/src/physics_world.c @@ -30,17 +30,6 @@ void physics_world_remove_entity(PhysicsEntity entity) { ASSERT_RETURN(0,, "Physics entity with data at %p is not registered in physics world", entity.data); } -Collider* physics_world_query(PhysicsQuery query) { - list_foreach(RigidBody**, body, &_world_bodies) { - list_foreach(Collider**, collider, rigidbody_get_colliders(*body)) { - if(overlap_check(query, *collider)) - return *collider; - } - } - - return NULL; -} - static inline void _internal_physics_check_entities(PhysicsEntity left, PhysicsEntity right) { RigidBody* rbleft = left.tc->get_rigidbody(left.data); @@ -99,3 +88,37 @@ void physics_world_tick() { _internal_physics_narrow_collision(); _internal_physics_apply(); } + +Collider* physics_world_query(PhysicsQuery query, RigidBody* ignore) { + list_foreach(PhysicsEntity*, entity, &_world_bodies) { + RigidBody* body = entity->tc->get_rigidbody(entity->data); + if(body == ignore) continue; + + list_foreach(Collider**, collider, rigidbody_get_colliders(body)) { + if(overlap_check(query, *collider)) + return *collider; + } + } + + return NULL; +} + +Collider* physics_world_box_query(Vector centre, Vector extents, PhysicsMask mask, RigidBody* ignore) { + Transform transform = { + .position = centre, + .scale = OneVector, + .rotation = 0.f + }; + Shape* shape = shape_new((Vector[]){ + MakeVector(-extents.x, -extents.y), + MakeVector(extents.x, -extents.y), + MakeVector(extents.x, extents.y), + MakeVector(-extents.x, extents.y) + }, 4); + PhysicsQuery query = { + .shape = shape, + .transform = &transform, + .mask = mask + }; + return physics_world_query(query, ignore); +} diff --git a/core/src/physics_world.h b/core/src/physics_world.h index b4ce2df..1152184 100644 --- a/core/src/physics_world.h +++ b/core/src/physics_world.h @@ -10,8 +10,9 @@ extern void physics_world_clean(); extern void physics_world_add_entity(PhysicsEntity entity); extern void physics_world_remove_entity(PhysicsEntity entity); -extern Collider* physics_world_query(PhysicsQuery query); - extern void physics_world_tick(); +extern Collider* physics_world_query(PhysicsQuery query, RigidBody* ignore); +extern Collider* physics_world_box_query(Vector centre, Vector extents, PhysicsMask mask, RigidBody* ignore); + #endif // !_fencer_physics_world_h