Line data Source code
1 : #include "physics/ray.hpp"
2 :
3 : #include <wren/scene/components/collider.hpp>
4 : #include <wren/scene/components/transform.hpp>
5 :
6 : namespace wren::physics {
7 :
8 3 : auto raycast(const flecs::world& world, const Ray& ray, RayHit& hit) -> bool {
9 3 : const auto q = world.query<const scene::components::Transform,
10 : const scene::components::Collider::Ptr>();
11 :
12 6 : q.each([ray, &hit](const scene::components::Transform& transform,
13 : const scene::components::Collider::Ptr& collider) {
14 3 : if (hit.hit) {
15 0 : return;
16 : }
17 :
18 3 : auto pos = collider->raycast(transform, ray.origin, ray.direction);
19 3 : if (pos.has_value()) {
20 2 : hit.hit = true;
21 2 : hit.point = pos.value();
22 2 : }
23 3 : });
24 :
25 3 : return hit.hit;
26 3 : }
27 :
28 : } // namespace wren::physics
|