Skip to content

Commit

Permalink
#80 Update bbox for collision node
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Oct 9, 2021
1 parent bc06879 commit af99999
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ namespace Skylicht
CCollisionNode* CBBCollisionManager::addBBCollision(CGameObject* object, const core::aabbox3df& bbox)
{
CEntity* entity = object->getEntity();
CCollisionNode* node = new CCollisionNode(object, entity, new CTriangleBB(entity, bbox));

CCollisionNode* node = new CCollisionNode(object, entity, new CTriangleBB(entity, bbox));
m_nodes.push_back(node);

build();

return node;
Expand Down
16 changes: 16 additions & 0 deletions Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ namespace Skylicht

void CBBoxPatchBuilder::build()
{
const u32 start = os::Timer::getRealTime();
u32 numPoly = 0;

core::aabbox3df size;

// step 1: caculator the box size
for (u32 i = 0, n = m_nodes.size(); i < n; i++)
{
if (i == 0)
size = m_nodes[i]->getTransformBBox();
else
size.addInternalBox(m_nodes[i]->getTransformBBox());
}

c8 tmp[256];
sprintf(tmp, "Needed %ums to CBBoxPatchBuilder::build", os::Timer::getRealTime() - start);
os::Printer::log(tmp, ELL_INFORMATION);
}
}
15 changes: 14 additions & 1 deletion Projects/Skylicht/Collision/Source/Collision/CCollisionNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,24 @@ namespace Skylicht
if (Selector != NULL && Entity != NULL)
{
CWorldTransformData* world = Entity->getData<CWorldTransformData>();
// CCollisionData* data = Entity->getData<CCollisionData>();

Triangles.set_used(Selector->getTriangleCount());

Selector->getTriangles(Triangles.pointer(), &world->World);
}
}

core::aabbox3df CCollisionNode::getTransformBBox()
{
core::aabbox3df ret;

if (Selector != NULL && Entity != NULL)
{
CWorldTransformData* world = Entity->getData<CWorldTransformData>();
ret = Selector->getBBox();
world->World.transformBoxEx(ret);
}

return ret;
}
}
7 changes: 3 additions & 4 deletions Projects/Skylicht/Collision/Source/Collision/CCollisionNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,17 @@ namespace Skylicht

CTriangleSelector* Selector;

//! Transform BBox
core::aabbox3df BBox;

//! Triangles that calculated World Transform
core::array<core::triangle3df> Triangles;

public:

CCollisionNode(CGameObject *gameObject, CEntity *entity, CTriangleSelector *selector);
CCollisionNode(CGameObject* gameObject, CEntity* entity, CTriangleSelector* selector);

virtual ~CCollisionNode();

void updateTransform();

core::aabbox3df getTransformBBox();
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ namespace Skylicht
{
COctreeBuilder::COctreeBuilder() :
m_root(NULL),
m_minimalPolysPerNode(128),
m_nodeCount(0)
m_minimalPolysPerNode(128)
{

}
Expand Down Expand Up @@ -102,7 +101,7 @@ namespace Skylicht
constructOctree(m_root);

c8 tmp[256];
sprintf(tmp, "Needed %ums to COctreeBuilder::build (%d nodes, %u polys)", os::Timer::getRealTime() - start, m_nodeCount, numPoly);
sprintf(tmp, "Needed %ums to COctreeBuilder::build (%u polys)", os::Timer::getRealTime() - start, numPoly);
os::Printer::log(tmp, ELL_INFORMATION);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ namespace Skylicht
COctreeNode* m_root;

u32 m_minimalPolysPerNode;
u32 m_nodeCount;

public:
COctreeBuilder();
Expand Down
2 changes: 1 addition & 1 deletion Projects/Skylicht/Collision/Source/Collision/COctreeNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ namespace Skylicht

virtual ~COctreeNode();
};
}
}
9 changes: 7 additions & 2 deletions Projects/Skylicht/Collision/Source/Collision/CTriangleBB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ This file is part of the "Skylicht Engine".
namespace Skylicht
{
CTriangleBB::CTriangleBB(CEntity* entity, const core::aabbox3df& bbox) :
CTriangleSelector(entity),
m_bbox(bbox)
CTriangleSelector(entity)
{
m_triangles.set_used(12);
m_bbox = bbox;
}

CTriangleBB::~CTriangleBB()
Expand Down Expand Up @@ -64,4 +64,9 @@ namespace Skylicht

CTriangleSelector::getTriangles(triangles, transform);
}

const core::aabbox3df& CTriangleBB::getBBox()
{
return m_bbox;
}
}
5 changes: 2 additions & 3 deletions Projects/Skylicht/Collision/Source/Collision/CTriangleBB.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ namespace Skylicht
{
class CTriangleBB : public CTriangleSelector
{
protected:
core::aabbox3df m_bbox;

public:
CTriangleBB(CEntity* entity, const core::aabbox3df& bbox);

virtual ~CTriangleBB();

virtual void getTriangles(core::triangle3df* triangles, const core::matrix4* transform);

virtual const core::aabbox3df& getBBox();
};
}
15 changes: 15 additions & 0 deletions Projects/Skylicht/Collision/Source/Collision/CTriangleSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,19 @@ namespace Skylicht
mat.transformVect(triangles[i].pointC, m_triangles[i].pointC);
}
}

const core::aabbox3df& CTriangleSelector::getBBox()
{
m_bbox.reset(m_triangles[0].pointA);

u32 cnt = m_triangles.size();
for (u32 i = 0; i < cnt; ++i)
{
m_bbox.addInternalPoint(m_triangles[i].pointA);
m_bbox.addInternalPoint(m_triangles[i].pointB);
m_bbox.addInternalPoint(m_triangles[i].pointC);
}

return m_bbox;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Skylicht
protected:
CEntity* m_entity;

core::aabbox3df m_bbox;
core::array<core::triangle3df> m_triangles;

public:
Expand All @@ -42,6 +43,8 @@ namespace Skylicht

virtual void getTriangles(core::triangle3df* triangles, const core::matrix4* transform);

virtual const core::aabbox3df& getBBox();

inline u32 getTriangleCount()
{
return m_triangles.size();
Expand Down

0 comments on commit af99999

Please sign in to comment.