Skip to content

Commit

Permalink
Got as far as I can, need more physics utils
Browse files Browse the repository at this point in the history
  • Loading branch information
tmayoff committed Nov 28, 2024
1 parent 5407f41 commit d256416
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
25 changes: 19 additions & 6 deletions examples/nand/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
#include <wren/application.hpp>
#include <wren/assets/manager.hpp>
#include <wren/physics/ray.hpp>
#include <wren/scene/components/collider.hpp>
#include <wren/scene/entity.hpp>
#include <wren/scene/scene.hpp>

namespace components = wren::scene::components;

auto initialize(const std::shared_ptr<wren::Application>& app)
-> wren::expected<void> {
Expand Down Expand Up @@ -38,7 +43,7 @@ auto initialize(const std::shared_ptr<wren::Application>& app)
return {};
}

void update();
void update(const std::shared_ptr<wren::scene::Scene>& scene);

auto main() -> int {
spdlog::set_level(spdlog::level::debug);
Expand All @@ -57,19 +62,27 @@ auto main() -> int {
return EXIT_FAILURE;
}

app->add_callback_to_phase(wren::CallbackPhase::Update, []() { update(); });
const auto scene = wren::scene::Scene::create();

auto quad = scene->create_entity("Quad");
quad.add_component<wren::scene::components::BoxCollider2D::Ptr>(
new components::BoxCollider2D());
auto& collider = quad.get_component<components::BoxCollider2D>();

app->add_callback_to_phase(wren::CallbackPhase::Update,
[scene]() { update(scene); });

app->run();

return EXIT_SUCCESS;
}

void update() {
// TODO Raycasting here

void update(const std::shared_ptr<wren::scene::Scene>& scene) {
wren::physics::RayHit hit;
wren::physics::Ray ray;
if (wren::physics::raycast(ray, hit)) {
ray.origin = wren::math::Vec3f{100, 0, 1};
ray.direction = wren::math::Vec3f{0, 0, -1};
if (wren::physics::raycast(scene->world(), ray, hit)) {
spdlog::info("Hit object");
}
}
2 changes: 2 additions & 0 deletions wren/src/scene/components/collider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ auto BoxCollider2D::raycast(const Transform& transform,
return {};
}

// TODO Check if the collision is within the bounds of the plane

return origin + direction * t;
}

Expand Down

0 comments on commit d256416

Please sign in to comment.