diff --git a/engine/core/Scene.cpp b/engine/core/Scene.cpp index 2b8cadff..2e37cf7b 100644 --- a/engine/core/Scene.cpp +++ b/engine/core/Scene.cpp @@ -85,8 +85,10 @@ Scene::~Scene(){ destroy(); std::vector entityList = entityManager.getEntityList(); - for(int entity : entityList){ - destroyEntity(entity); + while(entityList.size() > 0){ + destroyEntity(entityList.front()); + // some entities can destroy other entities (ex: models) + entityList = entityManager.getEntityList(); } } diff --git a/engine/core/math/Ray.cpp b/engine/core/math/Ray.cpp index 14e5fde5..53915204 100644 --- a/engine/core/math/Ray.cpp +++ b/engine/core/math/Ray.cpp @@ -365,7 +365,38 @@ RayReturn Ray::intersects(Scene* scene, RayFilter raytest, bool onlyStatic, uint return {true, hit.mFraction, getPoint(hit.mFraction), Vector3(normal.GetX(), normal.GetY(), normal.GetZ()), entity, shapeIndex}; } + } + } + + return NO_HIT; +} + +RayReturn Ray::intersects(Scene* scene, uint8_t broadPhaseLayer3D){ + return intersects(scene, broadPhaseLayer3D, (uint16_t)~0u, (uint16_t)~0u); +} + +RayReturn Ray::intersects(Scene* scene, uint8_t broadPhaseLayer3D, uint16_t categoryBits, uint16_t maskBits){ + JPH::PhysicsSystem* world = scene->getSystem()->getWorld3D(); + + if (world && broadPhaseLayer3D < MAX_BROADPHASELAYER_3D){ + JPH::RayCast ray(JPH::Vec3(origin.x, origin.y, origin.z), JPH::Vec3(direction.x, direction.y, direction.z)); + JPH::RayCastResult hit; + + JPH::ObjectLayer objectLayer = JPH::ObjectLayerPairFilterMask::sGetObjectLayer(categoryBits, maskBits); + + if (world->GetNarrowPhaseQuery().CastRay(JPH::RRayCast(ray), hit, JPH::SpecifiedBroadPhaseLayerFilter(JPH::BroadPhaseLayer(broadPhaseLayer3D)), JPH::DefaultObjectLayerFilter(JPH::ObjectLayerPairFilterMask(), objectLayer))){ + JPH::Vec3 normal; + Entity entity = NULL_ENTITY; + size_t shapeIndex = 0; + JPH::BodyLockRead lock(world->GetBodyLockInterface(), hit.mBodyID); + if (lock.Succeeded()){ + const JPH::Body &hit_body = lock.GetBody(); + normal = hit_body.GetWorldSpaceSurfaceNormal(hit.mSubShapeID2, ray.GetPointOnRay(hit.mFraction)); + entity = hit_body.GetUserData(); + shapeIndex = hit_body.GetShape()->GetSubShapeUserData(hit.mSubShapeID2); + } + return {true, hit.mFraction, getPoint(hit.mFraction), Vector3(normal.GetX(), normal.GetY(), normal.GetZ()), entity, shapeIndex}; } } diff --git a/engine/core/math/Ray.h b/engine/core/math/Ray.h index 9fbdc89d..4d0f75df 100644 --- a/engine/core/math/Ray.h +++ b/engine/core/math/Ray.h @@ -59,6 +59,8 @@ namespace Supernova { RayReturn intersects(Scene* scene, RayFilter raytest, bool onlyStatic); RayReturn intersects(Scene* scene, RayFilter raytest, uint16_t categoryBits, uint16_t maskBits); RayReturn intersects(Scene* scene, RayFilter raytest, bool onlyStatic, uint16_t categoryBits, uint16_t maskBits); + RayReturn intersects(Scene* scene, uint8_t broadPhaseLayer3D); // only 3D bodies + RayReturn intersects(Scene* scene, uint8_t broadPhaseLayer3D, uint16_t categoryBits, uint16_t maskBits); // only 3D bodies }; } diff --git a/engine/core/script/binding/MathClassesLua.cpp b/engine/core/script/binding/MathClassesLua.cpp index b577e0e3..9016f440 100644 --- a/engine/core/script/binding/MathClassesLua.cpp +++ b/engine/core/script/binding/MathClassesLua.cpp @@ -423,7 +423,9 @@ void LuaBinding::registerMathClasses(lua_State *L){ luabridge::overload(&Ray::intersects), luabridge::overload(&Ray::intersects), luabridge::overload(&Ray::intersects), - luabridge::overload(&Ray::intersects)) + luabridge::overload(&Ray::intersects), + luabridge::overload(&Ray::intersects), + luabridge::overload(&Ray::intersects)) .endClass(); #endif //DISABLE_LUA_BINDINGS diff --git a/engine/core/subsystem/MeshSystem.cpp b/engine/core/subsystem/MeshSystem.cpp index 0fec0d5c..631c7c1f 100644 --- a/engine/core/subsystem/MeshSystem.cpp +++ b/engine/core/subsystem/MeshSystem.cpp @@ -1914,8 +1914,6 @@ bool MeshSystem::loadGLTF(Entity entity, std::string filename){ } } - clearAnimations(model); - for (size_t i = 0; i < model.gltfModel->animations.size(); i++) { const tinygltf::Animation &animation = model.gltfModel->animations[i]; @@ -2082,6 +2080,8 @@ bool MeshSystem::loadOBJ(Entity entity, std::string filename){ ModelComponent& model = scene->getComponent(entity); Transform& transform = scene->getComponent(entity); + destroyModel(model); + tinyobj::attrib_t attrib; std::vector shapes; std::vector materials;