From 5d1eccacac46a4396a19862284b9739340381e06 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Wed, 14 Feb 2024 10:49:12 +0800 Subject: [PATCH] add StandardComponents for collision --- ios/include/components/StandardComponents.h | 64 +++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 ios/include/components/StandardComponents.h diff --git a/ios/include/components/StandardComponents.h b/ios/include/components/StandardComponents.h new file mode 100644 index 00000000..bd5e3f20 --- /dev/null +++ b/ios/include/components/StandardComponents.h @@ -0,0 +1,64 @@ +#ifndef _STANDARD_COMPONENTS_H +#define _STANDARD_COMPONENTS_H + +#include "utils/Entity.h" +#include "utils/EntityInstance.h" +#include "utils/SingleInstanceComponentManager.h" +#include "filament/TransformManager.h" +#include "gltfio/FilamentAsset.h" +#include "gltfio/FilamentInstance.h" + +namespace polyvox +{ + +class FloatComponentManager : public utils::SingleInstanceComponentManager { + + static constexpr size_t DIRECTION = 0; + static constexpr size_t POSITION = 1; + static constexpr size_t MAX = 2; + static constexpr size_t SPEED = 2; + + // void update() { + // const auto* entities = getEntities(); + // for(int i = 0; i < getComponentCount(); i++) { + // // const auto instance = getInstance(); + // // elementAt + // // const auto component = get + // // const auto entity = entities[i]; + + // } + // } +}; + +class CollisionComponentManager : public utils::SingleInstanceComponentManager { + + static constexpr size_t INSTANCE = 0; + + const filament::TransformManager& _transformManager; + public: + CollisionComponentManager(const filament::TransformManager& transformManager) : _transformManager(transformManager) {} + + bool collides(filament::Aabb sourceBox) { + auto sourceCorners = sourceBox.getCorners(); + const auto& entities = getEntities(); + for(auto it = begin(); it < end(); it++) { + auto entity = entities[it]; + + auto targetInstance = elementAt(it); + auto targetXformInstance = _transformManager.getInstance(entity); + auto targetXform = _transformManager.getWorldTransform(targetXformInstance); + auto targetBox = targetInstance->getBoundingBox().transform(targetXform); + for(int i = 0; i < 8; i++) { + if(targetBox.contains(sourceCorners.vertices[i]) < 0) { + return true; + } + } + } + return false; + } +}; + + +} + +#endif \ No newline at end of file