Skip to content

Commit

Permalink
Added 3D broadphaselayer test in ray
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardodoria committed Mar 14, 2024
1 parent 9384129 commit e9854cd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
6 changes: 4 additions & 2 deletions engine/core/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ Scene::~Scene(){
destroy();

std::vector<Entity> 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();
}

}
Expand Down
31 changes: 31 additions & 0 deletions engine/core/math/Ray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PhysicsSystem>()->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};
}
}

Expand Down
2 changes: 2 additions & 0 deletions engine/core/math/Ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

}
Expand Down
4 changes: 3 additions & 1 deletion engine/core/script/binding/MathClassesLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,9 @@ void LuaBinding::registerMathClasses(lua_State *L){
luabridge::overload<Scene*, RayFilter>(&Ray::intersects),
luabridge::overload<Scene*, RayFilter, bool>(&Ray::intersects),
luabridge::overload<Scene*, RayFilter, uint16_t, uint16_t>(&Ray::intersects),
luabridge::overload<Scene*, RayFilter, bool, uint16_t, uint16_t>(&Ray::intersects))
luabridge::overload<Scene*, RayFilter, bool, uint16_t, uint16_t>(&Ray::intersects),
luabridge::overload<Scene*, uint8_t>(&Ray::intersects),
luabridge::overload<Scene*, uint8_t, uint16_t, uint16_t>(&Ray::intersects))
.endClass();

#endif //DISABLE_LUA_BINDINGS
Expand Down
4 changes: 2 additions & 2 deletions engine/core/subsystem/MeshSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -2082,6 +2080,8 @@ bool MeshSystem::loadOBJ(Entity entity, std::string filename){
ModelComponent& model = scene->getComponent<ModelComponent>(entity);
Transform& transform = scene->getComponent<Transform>(entity);

destroyModel(model);

tinyobj::attrib_t attrib;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
Expand Down

0 comments on commit e9854cd

Please sign in to comment.