diff options
Diffstat (limited to 'src/phys/world.odin')
| -rw-r--r-- | src/phys/world.odin | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/src/phys/world.odin b/src/phys/world.odin index 4a10590..25db947 100644 --- a/src/phys/world.odin +++ b/src/phys/world.odin @@ -158,6 +158,40 @@ remove_body :: proc(h: Body_Handle) { world.bodies[h.idx] = {} } +@(require_results) +get_closest_raycast_collision :: proc( + rc: Raycast, +) -> (Raycast_Collision, bool) { + nearest_collision: Raycast_Collision + nearest_dist := math.INF_F32 + + bin_list := get_surrounding_bins(rc.start) + + for bin in bin_list { + for b_h in bin { + b := _get_body(b_h) + + if b.layers & rc.mask == nil { + continue + } + + b_rect := b.rect + b_rect.start += b.pos + + collision, collided := raycast_to_aabb(rc, b^) + if collided { + dist := linalg.length2(collision.enter - rc.start) + if dist < nearest_dist { + nearest_dist = dist + nearest_collision = collision + } + } + } + } + + return nearest_collision, nearest_collision.body.uses > 0 +} + is_colliding :: proc{ is_body_colliding, is_raycast_colliding, @@ -184,8 +218,8 @@ get_colliding_bodies :: proc{ get_colliding_bodies_raycast_vs_body :: proc( rc: Raycast, allocator := context.allocator -) -> []Body_Handle { - bodies := make([dynamic]Body_Handle, allocator) +) -> []Raycast_Collision { + bodies := make([dynamic]Raycast_Collision, allocator) bin_list := get_surrounding_bins(rc.start) @@ -200,8 +234,9 @@ get_colliding_bodies_raycast_vs_body :: proc( b_rect := b.rect b_rect.start += b.pos - if raycast_to_aabb(rc, b^) { - append(&bodies, b_h) + collision, collided := raycast_to_aabb(rc, b^) + if collided { + append(&bodies, collision) } } } |
