-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'examples' of github.com:tmayoff/wren into examples
- Loading branch information
Showing
16 changed files
with
220 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
coverage: | ||
status: | ||
project: false | ||
ignore: | ||
- "examples" | ||
- "tools" | ||
- "subprojects" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,21 @@ | ||
#pragma once | ||
|
||
#include <functional> | ||
#include <flecs.h> | ||
|
||
#include "scene.hpp" | ||
namespace wren::scene::components { | ||
|
||
namespace wren::scene { | ||
struct Base { | ||
Base() = default; | ||
Base(const Base &) = default; | ||
Base(Base &&) = delete; | ||
auto operator=(const Base &) -> Base & = default; | ||
auto operator=(Base &&) -> Base & = delete; | ||
virtual ~Base() = default; | ||
|
||
// virtual void init(const flecs::world &world) = 0; | ||
}; | ||
|
||
// void iterate_known_components(const Scene& scene, entt::entity entity, | ||
// std::function<void()>); | ||
|
||
} | ||
} // namespace wren::scene::components |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#pragma once | ||
|
||
#include <flecs.h> | ||
|
||
#include <memory> | ||
#include <optional> | ||
#include <wren/math/vector.hpp> | ||
#include <wren/scene/components/transform.hpp> | ||
|
||
#include "../components.hpp" | ||
|
||
namespace wren::scene::components { | ||
|
||
struct Collider : public Base { | ||
using Ptr = std::shared_ptr<Collider>; | ||
|
||
[[nodiscard]] virtual auto raycast(const Transform& transform, | ||
const math::Vec3f& origin, | ||
const math::Vec3f& direction) const | ||
-> std::optional<math::Vec3f> = 0; | ||
}; | ||
|
||
struct BoxCollider2D : public Collider { | ||
static void init(const flecs::world& world) { | ||
static bool inited = false; | ||
if (!inited) { | ||
inited = true; | ||
world.component<BoxCollider2D::Ptr>().is_a<Collider>(); | ||
} | ||
} | ||
|
||
[[nodiscard]] auto raycast(const Transform& transform, | ||
const math::Vec3f& origin, | ||
const math::Vec3f& direction) const | ||
-> std::optional<math::Vec3f> override; | ||
|
||
math::Vec2f size; | ||
}; | ||
|
||
} // namespace wren::scene::components |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "scene/components/collider.hpp" | ||
|
||
namespace wren::scene::components { | ||
|
||
auto BoxCollider2D::raycast(const Transform& transform, | ||
const math::Vec3f& origin, | ||
const math::Vec3f& direction) const | ||
-> std::optional<math::Vec3f> { | ||
math::Vec3f normal = | ||
math::Quaternionf{transform.rotation}.to_mat() * math::Vec3f{0, 0, 1}; | ||
|
||
float denominator = normal.dot(direction); | ||
if (std::abs(denominator) < 0.0001) { | ||
return {}; | ||
} | ||
|
||
float t = normal.dot(transform.position - origin) / denominator; | ||
if (t < 0) { | ||
// Intersection points is behind the ray's origin | ||
return {}; | ||
} | ||
|
||
return origin + direction * t; | ||
} | ||
|
||
} // namespace wren::scene::components |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#include <flecs.h> | ||
|
||
#include <wren/math/vector.hpp> | ||
|
||
namespace wren::physics { | ||
|
||
struct RayHit { | ||
bool hit = false; | ||
math::Vec3f point; | ||
}; | ||
|
||
struct Ray { | ||
math::Vec3f origin; | ||
math::Vec3f direction; | ||
}; | ||
|
||
auto raycast(const flecs::world& world, const Ray& ray, RayHit& hit) -> bool; | ||
|
||
} // namespace wren::physics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
wren_physics = static_library( | ||
'wren_physics', | ||
'src/ray.cpp', | ||
dependencies: [wren_dep, flecs], | ||
include_directories: ['include/wren', 'include/wren/physics'], | ||
) | ||
wren_physics_dep = declare_dependency( | ||
include_directories: 'include', | ||
link_with: wren_physics, | ||
) | ||
|
||
subdir('tests') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "physics/ray.hpp" | ||
|
||
#include <wren/scene/components/collider.hpp> | ||
#include <wren/scene/components/transform.hpp> | ||
|
||
namespace wren::physics { | ||
|
||
auto raycast(const flecs::world& world, const Ray& ray, RayHit& hit) -> bool { | ||
const auto q = world.query<const scene::components::Transform, | ||
const scene::components::Collider::Ptr>(); | ||
|
||
q.each([ray, &hit](const scene::components::Transform& transform, | ||
const scene::components::Collider::Ptr& collider) { | ||
if (hit.hit) { | ||
return; | ||
} | ||
|
||
auto pos = collider->raycast(transform, ray.origin, ray.direction); | ||
if (pos.has_value()) { | ||
hit.hit = true; | ||
hit.point = pos.value(); | ||
} | ||
}); | ||
|
||
return hit.hit; | ||
} | ||
|
||
} // namespace wren::physics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
tests = ['raycast'] | ||
foreach test : tests | ||
test( | ||
'wren_physics_@0@'.format(test), | ||
executable( | ||
'wren_physics_@0@_test'.format(test), | ||
'@[email protected]'.format(test), | ||
dependencies: [wren_physics_dep, wren_dep, boost_test], | ||
cpp_args: ['-DBOOST_TEST_MODULE=@0@'.format(test)], | ||
), | ||
) | ||
endforeach |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <boost/test/unit_test.hpp> | ||
#include <wren/physics/ray.hpp> | ||
#include <wren/scene/components.hpp> | ||
#include <wren/scene/components/collider.hpp> | ||
#include <wren/scene/components/transform.hpp> | ||
#include <wren/scene/entity.hpp> | ||
#include <wren/scene/scene.hpp> | ||
|
||
namespace physics = wren::physics; | ||
namespace components = wren::scene::components; | ||
namespace math = wren::math; | ||
|
||
BOOST_AUTO_TEST_SUITE(raycasting) | ||
|
||
BOOST_AUTO_TEST_CASE(RaycastBoxCollider) { | ||
const auto scene = wren::scene::Scene::create(); | ||
|
||
auto box = scene->create_entity("box"); | ||
|
||
box.add_component<components::BoxCollider2D::Ptr>( | ||
new components::BoxCollider2D()); | ||
auto& transform = box.get_component<components::Transform>(); | ||
transform.position.z(-10); | ||
// transform.position.x(1000); | ||
|
||
BOOST_TEST(box.has_component<components::BoxCollider2D::Ptr>()); | ||
|
||
const auto q = scene->world().query<const components::Collider::Ptr>(); | ||
BOOST_TEST(q.count() == 1); | ||
|
||
physics::Ray ray; | ||
ray.direction = (transform.position - math::Vec3f(0)); | ||
|
||
physics::RayHit hit{}; | ||
physics::raycast(scene->world(), ray, hit); | ||
|
||
BOOST_TEST(hit.hit); | ||
|
||
const float length = (hit.point - math::Vec3f{0, 0, -10}).length(); | ||
BOOST_TEST(length <= 0.001); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |