Skip to content

Commit

Permalink
#80 Add interface raycast collide
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Oct 13, 2021
1 parent 7018d87 commit 8ea9836
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Projects/Editor/Source/GUI/Controls/CSlider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace Skylicht
float x = core::clamp(local.X, 0.0f, m_bounds.Width);
float f = x / m_bounds.Width;

float value = (m_max - m_min) * f;
float value = m_min + (m_max - m_min) * f;
setValue(value, m_min, m_max, true);
}

Expand Down
71 changes: 66 additions & 5 deletions Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace Skylicht

m_patchs.set_used(0);
m_collisionPatchs.set_used(0);
m_global.Collisions.set_used(0);

const u32 start = os::Timer::getRealTime();
u32 numPoly = 0;
Expand All @@ -77,6 +78,10 @@ namespace Skylicht
m_numY = core::ceil32(s.Y / m_patchSize);
m_numZ = core::ceil32(s.Z / m_patchSize);

core::vector3df patchSize(m_patchSize, m_patchSize, m_patchSize);

m_global.BBox = m_size;

for (int x = 0; x < m_numX; x++)
{
for (int z = 0; z < m_numZ; z++)
Expand All @@ -91,7 +96,7 @@ namespace Skylicht
p.Z = z;

p.BBox.MinEdge = m_size.MinEdge + core::vector3df(x * m_patchSize, y * m_patchSize, z * m_patchSize);
p.BBox.MaxEdge = p.BBox.MinEdge + core::vector3df(m_patchSize, m_patchSize, m_patchSize);
p.BBox.MaxEdge = p.BBox.MinEdge + patchSize;
}
}
}
Expand All @@ -111,20 +116,40 @@ namespace Skylicht
}
}


c8 tmp[256];
sprintf(tmp, "Needed %ums to CBBoxPatchBuilder::build", os::Timer::getRealTime() - start);
os::Printer::log(tmp, ELL_INFORMATION);
}

int CBBoxPatchBuilder::getPatchID(int x, int y, int z)
{
return x * m_numZ * m_numY + z * m_numY + y;
}

CBBoxPatchBuilder::SPatch* CBBoxPatchBuilder::getPatch(int x, int y, int z)
{
int id = x * m_numZ * m_numY + z * m_numY + y;
int id = getPatchID(x, y, z);
if (id < 0 || id >= (int)m_patchs.size())
return NULL;
return m_patchs[id];
}

void CBBoxPatchBuilder::updateNode(CCollisionNode* collision)
{
core::aabbox3df bbox = collision->getTransformBBox();
if (bbox.isFullInside(m_size))
{
// just update the collision
removeNode(collision);
addNodeToPatch(collision);
}
else
{
// rebuild
build();
}
}

void CBBoxPatchBuilder::addNodeToPatch(CCollisionNode* collision)
{
core::aabbox3df bbox = collision->getTransformBBox();
Expand All @@ -145,10 +170,14 @@ namespace Skylicht
SPatch* patch = getPatch(x, y, z);
if (patch)
{
if (patch->BBox.intersectsWithBox(bbox))
if (bbox.isFullInside(patch->BBox))
{
patch->Collisions.push_back(collision);
}
else
{
m_global.Collisions.push_back(collision);
}
}
}
}
Expand All @@ -175,14 +204,16 @@ namespace Skylicht
SPatch* patch = getPatch(x, y, z);
if (patch)
{
if (patch->BBox.intersectsWithBox(bbox))
if (bbox.isFullInside(patch->BBox))
{
removeNode(collision, patch);
}
}
}
}
}

removeNode(collision, &m_global);
}

void CBBoxPatchBuilder::removeNode(CCollisionNode* collision, SPatch* patch)
Expand All @@ -196,4 +227,34 @@ namespace Skylicht
}
}
}

CGameObject* CBBoxPatchBuilder::getObjectWithRay(
const core::line3d<f32>& ray,
f32& outBestDistanceSquared,
core::vector3df& outCollisionPoint,
core::triangle3df& outTriangle,
CCollisionNode*& outNode)
{
return NULL;
}

bool CBBoxPatchBuilder::getCollisionPoint(
const core::line3d<f32>& ray,
f32& outBestDistanceSquared,
core::vector3df& outIntersection,
core::triangle3df& outTriangle,
CCollisionNode*& outNode)
{
return false;
}

bool CBBoxPatchBuilder::getCollisionPoint(
const core::vector3df& target,
const core::vector3df& pos,
core::vector3df& outPos,
core::triangle3df& outTri,
CCollisionNode*& outNode)
{
return false;
}
}
38 changes: 37 additions & 1 deletion Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ namespace Skylicht
int Y;
int Z;
core::array<CCollisionNode*> Collisions;

SPatch()
{
X = 0;
Y = 0;
Z = 0;
}
};

SPatch m_global;
core::array<SPatch*> m_collisionPatchs;
core::array<SPatch*> m_patchs;
core::aabbox3df m_size;
Expand All @@ -56,14 +64,42 @@ namespace Skylicht

virtual void build();

int getPatchID(int x, int y, int z);

SPatch* getPatch(int x, int y, int z);

void updateNode(CCollisionNode* collision);

void addNodeToPatch(CCollisionNode* collision);

void removeNode(CCollisionNode* collision);

public:

virtual CGameObject* getObjectWithRay(
const core::line3d<f32>& ray,
f32& outBestDistanceSquared,
core::vector3df& outCollisionPoint,
core::triangle3df& outTriangle,
CCollisionNode*& outNode);

virtual bool getCollisionPoint(
const core::line3d<f32>& ray,
f32& outBestDistanceSquared,
core::vector3df& outIntersection,
core::triangle3df& outTriangle,
CCollisionNode*& outNode);

virtual bool getCollisionPoint(
const core::vector3df& target,
const core::vector3df& pos,
core::vector3df& outPos,
core::triangle3df& outTri,
CCollisionNode*& outNode);

protected:
void removeNode(CCollisionNode* collision, SPatch *patch);

void removeNode(CCollisionNode* collision, SPatch* patch);

};
}
21 changes: 21 additions & 0 deletions Projects/Skylicht/Collision/Source/Collision/CCollisionBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,26 @@ namespace Skylicht
int findNode(CCollisionNode* node, CCollisionNode** nodes, int count);

virtual void build() = 0;

virtual CGameObject* getObjectWithRay(
const core::line3d<f32>& ray,
f32& outBestDistanceSquared,
core::vector3df& outCollisionPoint,
core::triangle3df& outTriangle,
CCollisionNode*& outNode) = 0;

virtual bool getCollisionPoint(
const core::line3d<f32>& ray,
f32& outBestDistanceSquared,
core::vector3df& outIntersection,
core::triangle3df& outTriangle,
CCollisionNode*& outNode) = 0;

virtual bool getCollisionPoint(
const core::vector3df& target,
const core::vector3df& pos,
core::vector3df& outPos,
core::triangle3df& outTri,
CCollisionNode*& outNode) = 0;
};
}
30 changes: 30 additions & 0 deletions Projects/Skylicht/Collision/Source/Collision/COctreeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,34 @@ namespace Skylicht
}
}
}

CGameObject* COctreeBuilder::getObjectWithRay(
const core::line3d<f32>& ray,
f32& outBestDistanceSquared,
core::vector3df& outCollisionPoint,
core::triangle3df& outTriangle,
CCollisionNode*& outNode)
{
return NULL;
}

bool COctreeBuilder::getCollisionPoint(
const core::line3d<f32>& ray,
f32& outBestDistanceSquared,
core::vector3df& outIntersection,
core::triangle3df& outTriangle,
CCollisionNode*& outNode)
{
return false;
}

bool COctreeBuilder::getCollisionPoint(
const core::vector3df& target,
const core::vector3df& pos,
core::vector3df& outPos,
core::triangle3df& outTri,
CCollisionNode*& outNode)
{
return false;
}
}
23 changes: 23 additions & 0 deletions Projects/Skylicht/Collision/Source/Collision/COctreeBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ namespace Skylicht

virtual void clear();

public:

virtual CGameObject* getObjectWithRay(
const core::line3d<f32>& ray,
f32& outBestDistanceSquared,
core::vector3df& outCollisionPoint,
core::triangle3df& outTriangle,
CCollisionNode*& outNode);

virtual bool getCollisionPoint(
const core::line3d<f32>& ray,
f32& outBestDistanceSquared,
core::vector3df& outIntersection,
core::triangle3df& outTriangle,
CCollisionNode*& outNode);

virtual bool getCollisionPoint(
const core::vector3df& target,
const core::vector3df& pos,
core::vector3df& outPos,
core::triangle3df& outTri,
CCollisionNode*& outNode);

protected:

void constructOctree(COctreeNode* node);
Expand Down

0 comments on commit 8ea9836

Please sign in to comment.