Skip to content

Commit

Permalink
#80 Add Pick collision system
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Oct 18, 2021
1 parent 7ef7930 commit b524465
Show file tree
Hide file tree
Showing 13 changed files with 276 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ namespace Skylicht

// add handle renderer
CEntityManager* entityMgr = m_scene->getEntityManager();
entityMgr->addSystem<CPickCollisionSystem>();
m_handlesRenderer = entityMgr->addRenderSystem<CHandlesRenderer>();
m_gizmosRenderer = entityMgr->addRenderSystem<CGizmosRenderer>();

Expand Down Expand Up @@ -277,7 +278,7 @@ namespace Skylicht

CDirectionalLight* directionalLight = lightObj->addComponent<CDirectionalLight>();
SColor c(255, 255, 244, 214);
directionalLight->setColor(SColorf(c));
directionalLight->setColor(SColorf(c));

// update search index
m_scene->updateAddRemoveObject();
Expand Down
2 changes: 2 additions & 0 deletions Projects/Editor/Source/Editor/Space/Scene/CSpaceScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ This file is part of the "Skylicht Engine".
#include "Editor/History/CSceneHistory.h"
#include "EditorComponents/Handles/CHandlesRenderer.h"
#include "EditorComponents/Gizmos/CGizmosRenderer.h"
#include "EditorComponents/PickCollision/CPickCollisionSystem.h"

#include "CViewpointController.h"

#include "Reactive/ISubject.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ This file is part of the "Skylicht Engine".
#include "Utils/CGameSingleton.h"

#include "Collision/CBBCollisionManager.h"
#include "EditorComponents/PickCollision/CPickCollisionData.h"

namespace Skylicht
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ namespace Skylicht

CBBCollisionManager* bbCollision = CCollisionController::getInstance()->getBBCollision();
m_collisionNode = bbCollision->addBBCollision(m_gameObject, core::aabbox3df(core::vector3df(-1.0f, -1.0f, -1.0f), core::vector3df(1.0f, 1.0f, 1.0f)));

CPickCollisionData* pickData = m_gameObject->getEntity()->addData<CPickCollisionData>();
pickData->CollisionNode = m_collisionNode;
pickData->IsBBoxCollision = true;
}

void CGDirectionLight::updateComponent()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
!@
MIT License
Copyright (c) 2021 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CPickCollisionData.h"

namespace Skylicht
{
namespace Editor
{
CPickCollisionData::CPickCollisionData() :
CollisionNode(NULL),
IsBBoxCollision(false),
IsChanged(true)
{

}

CPickCollisionData::~CPickCollisionData()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
!@
MIT License
Copyright (c) 2021 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "Entity/IEntityData.h"
#include "Collision/CCollisionNode.h"

namespace Skylicht
{
namespace Editor
{
class CPickCollisionData : public IEntityData
{
public:
CCollisionNode* CollisionNode;
bool IsBBoxCollision;
bool IsChanged;

public:
CPickCollisionData();

virtual ~CPickCollisionData();
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
!@
MIT License
Copyright (c) 2021 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CPickCollisionSystem.h"
#include "Editor/SpaceController/CCollisionController.h"

namespace Skylicht
{
namespace Editor
{
CPickCollisionSystem::CPickCollisionSystem()
{


}

CPickCollisionSystem::~CPickCollisionSystem()
{

}

void CPickCollisionSystem::beginQuery()
{
m_pickCollisions.set_used(0);
}

void CPickCollisionSystem::onQuery(CEntityManager* entityManager, CEntity* entity)
{
// if transform is changed
CPickCollisionData* collisionData = entity->getData<CPickCollisionData>();
if (collisionData != NULL &&
collisionData->CollisionNode != NULL &&
collisionData->IsChanged)
{
m_pickCollisions.push_back(collisionData);
}
}

void CPickCollisionSystem::init(CEntityManager* entityManager)
{

}

void CPickCollisionSystem::update(CEntityManager* entityManager)
{
CPickCollisionData** listCollision = m_pickCollisions.pointer();
u32 numCollision = m_pickCollisions.size();

CBBCollisionManager* bbCollision = CCollisionController::getInstance()->getBBCollision();

for (u32 i = 0; i < numCollision; i++)
{
CPickCollisionData* collision = listCollision[i];

if (collision->IsBBoxCollision)
{
bbCollision->updateNode(collision->CollisionNode);
}

collision->IsChanged = false;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
!@
MIT License
Copyright (c) 2021 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the Rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "Entity/IEntityData.h"
#include "Entity/IEntitySystem.h"

#include "CPickCollisionData.h"
#include "Transform/CWorldTransformData.h"

namespace Skylicht
{
namespace Editor
{
class CPickCollisionSystem : public IEntitySystem
{
protected:
core::array<CPickCollisionData*> m_pickCollisions;

public:
CPickCollisionSystem();

virtual ~CPickCollisionSystem();

virtual void beginQuery();

virtual void onQuery(CEntityManager* entityManager, CEntity* entity);

virtual void init(CEntityManager* entityManager);

virtual void update(CEntityManager* entityManager);
};
}
}
18 changes: 11 additions & 7 deletions Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ namespace Skylicht

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

m_global.VolumeBox.MinEdge = m_size.MinEdge;
m_global.VolumeBox.MaxEdge = m_size.MinEdge + core::vector3df(m_numX * m_patchSize, m_numY * m_patchSize, m_numZ * m_patchSize);
m_global.BBox = m_size;

for (int x = 0; x < m_numX; x++)
Expand All @@ -98,8 +100,8 @@ namespace Skylicht
p.Y = y;
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 + patchSize;
p.VolumeBox.MinEdge = m_size.MinEdge + core::vector3df(x * m_patchSize, y * m_patchSize, z * m_patchSize);
p.VolumeBox.MaxEdge = p.VolumeBox.MinEdge + patchSize;
}
}
}
Expand All @@ -113,9 +115,12 @@ namespace Skylicht
// step4: collect the patch have collisions
for (u32 i = 0, n = m_patchs.size(); i < n; i++)
{
if (m_patchs[i]->Collisions.size() > 0)
SPatch* patch = m_patchs[i];

if (patch->Collisions.size() > 0)
{
m_collisionPatchs.push_back(m_patchs[i]);
patch->calculateBBox();
m_collisionPatchs.push_back(patch);
}
}

Expand Down Expand Up @@ -173,7 +178,7 @@ namespace Skylicht
SPatch* patch = getPatch(x, y, z);
if (patch)
{
if (bbox.isFullInside(patch->BBox))
if (bbox.isFullInside(patch->VolumeBox))
{
patch->Collisions.push_back(collision);
}
Expand Down Expand Up @@ -226,6 +231,7 @@ namespace Skylicht
if (patch->Collisions[i] == collision)
{
patch->Collisions.erase(i);
patch->calculateBBox();
return;
}
}
Expand Down Expand Up @@ -262,7 +268,6 @@ namespace Skylicht

void CBBoxPatchBuilder::drawDebug()
{
/*
CSceneDebug* debug = CSceneDebug::getInstance();

debug->addBoudingBox(m_global.BBox, SColor(255, 255, 0, 255));
Expand All @@ -278,7 +283,6 @@ namespace Skylicht
debug->addBoudingBox(node->getTransformBBox(), SColor(255, 255, 255, 0));
}
}
*/
}

bool CBBoxPatchBuilder::getCollisionPoint(
Expand Down
14 changes: 14 additions & 0 deletions Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace Skylicht
protected:
struct SPatch
{
core::aabbox3df VolumeBox;
core::aabbox3df BBox;
int X;
int Y;
Expand All @@ -45,6 +46,19 @@ namespace Skylicht
Y = 0;
Z = 0;
}

void calculateBBox()
{
BBox.reset(core::vector3df());

for (u32 i = 0, n = Collisions.size(); i < n; i++)
{
if (i == 0)
BBox = Collisions[0]->getTransformBBox();
else
BBox.addInternalBox(Collisions[i]->getTransformBBox());
}
}
};

SPatch m_global;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace Skylicht
CSpriteDrawData::CSpriteDrawData() :
Frame(NULL),
Scale(1.0f),
ViewScale(1.0f),
Color(255, 255, 255, 255),
Center(false),
Billboard(false),
Expand Down
Loading

0 comments on commit b524465

Please sign in to comment.