add raycasting in level
This commit is contained in:
@@ -3,11 +3,20 @@
|
|||||||
#include <NazaraEditor/Core/Config.hpp>
|
#include <NazaraEditor/Core/Config.hpp>
|
||||||
|
|
||||||
#include <Nazara/Core/EnttWorld.hpp>
|
#include <Nazara/Core/EnttWorld.hpp>
|
||||||
|
#include <Nazara/Math/Ray.hpp>
|
||||||
|
|
||||||
namespace Nz
|
namespace Nz
|
||||||
{
|
{
|
||||||
class EditorBaseApplication;
|
class EditorBaseApplication;
|
||||||
|
|
||||||
|
struct RaycastHit
|
||||||
|
{
|
||||||
|
entt::handle entity;
|
||||||
|
|
||||||
|
Nz::Vector3f position;
|
||||||
|
float distance;
|
||||||
|
};
|
||||||
|
|
||||||
class NAZARAEDITOR_CORE_API Level final
|
class NAZARAEDITOR_CORE_API Level final
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -21,6 +30,9 @@ namespace Nz
|
|||||||
entt::handle CreateEntity();
|
entt::handle CreateEntity();
|
||||||
bool CreateNewLevel();
|
bool CreateNewLevel();
|
||||||
|
|
||||||
|
// This is slow af
|
||||||
|
std::vector<RaycastHit> Raycast(const Nz::Rayf& ray);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
EditorBaseApplication* m_application;
|
EditorBaseApplication* m_application;
|
||||||
|
|
||||||
|
|||||||
@@ -20,4 +20,31 @@ namespace Nz
|
|||||||
m_world = &ecs.AddWorld<Nz::EnttWorld>();
|
m_world = &ecs.AddWorld<Nz::EnttWorld>();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<RaycastHit> Level::Raycast(const Nz::Rayf& ray)
|
||||||
|
{
|
||||||
|
std::vector<RaycastHit> entities;
|
||||||
|
|
||||||
|
auto& registry = m_world->GetRegistry();
|
||||||
|
for (auto&& entity : registry.storage<entt::entity>().each())
|
||||||
|
{
|
||||||
|
Nz::GraphicsComponent* component = registry.try_get<Nz::GraphicsComponent>(std::get<entt::entity>(entity));
|
||||||
|
if (component != nullptr)
|
||||||
|
{
|
||||||
|
float distance = 0;
|
||||||
|
if (ray.Intersect(component->GetAABB(), &distance))
|
||||||
|
{
|
||||||
|
entities.push_back({
|
||||||
|
.entity = entt::handle(registry, std::get<entt::entity>(entity)),
|
||||||
|
.position = ray.origin + ray.direction * distance,
|
||||||
|
.distance = distance
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort the result by distance first
|
||||||
|
std::sort(entities.begin(), entities.end(), [](auto&& A, auto&& B) { return A.distance < B.distance; });
|
||||||
|
return entities;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user