Skip to content

Commit

Permalink
#80 Add BBox builder for dynamic transform object collision
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Oct 9, 2021
1 parent 759b404 commit dec3e75
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 112 deletions.
21 changes: 6 additions & 15 deletions Projects/Editor/Source/GUI/Controls/CNumberInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ This file is part of the "Skylicht Engine".

#include "pch.h"

#include <codecvt>
#include <locale>

#include "CNumberInput.h"
#include "GUI/Theme/CThemeConfig.h"
#include "GUI/Input/CInput.h"

#include "Utils/CStringImp.h"

namespace Skylicht
{
namespace Editor
Expand Down Expand Up @@ -230,26 +229,18 @@ namespace Skylicht
void CNumberInput::applyTextValue()
{
const std::wstring s = getString();
std::string utf8 = CStringImp::convertUnicodeToUTF8(s.c_str());

using convert_t = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_t, wchar_t> strconverter;

std::string utf8 = strconverter.to_bytes(s);

float value = atof(utf8.c_str());
float value = (float)atof(utf8.c_str());
setValue(value, false);
}

void CNumberInput::onValidateValue()
{
const std::wstring s = getString();
std::string utf8 = CStringImp::convertUnicodeToUTF8(s.c_str());

using convert_t = std::codecvt_utf8<wchar_t>;
std::wstring_convert<convert_t, wchar_t> strconverter;

std::string utf8 = strconverter.to_bytes(s);

m_value = atof(utf8.c_str());
m_value = (float)atof(utf8.c_str());
}

void CNumberInput::setValue(float value, bool invokeEvent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace Skylicht
CCollisionNode* node = new CCollisionNode(object, entity, new CTriangleBB(entity, bbox));

m_nodes.push_back(node);
rebuildOctree();
build();

return node;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ This file is part of the "Skylicht Engine".

#include "CTriangleSelector.h"
#include "CCollisionNode.h"
#include "COctreeNode.h"
#include "COctreeBuilder.h"

#include "CBBoxPatchBuilder.h"

#include "GameObject/CGameObject.h"
#include "Utils/CGameSingleton.h"

namespace Skylicht
{
class CBBCollisionManager : public COctreeBuilder
class CBBCollisionManager : public CBBoxPatchBuilder
{
protected:

Expand Down
49 changes: 49 additions & 0 deletions Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
!@
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 "CBBoxPatchBuilder.h"

namespace Skylicht
{
CBBoxPatchBuilder::CBBoxPatchBuilder()
{

}

CBBoxPatchBuilder::~CBBoxPatchBuilder()
{
clear();
}

void CBBoxPatchBuilder::clear()
{
CCollisionBuilder::clear();
}

void CBBoxPatchBuilder::build()
{

}
}
42 changes: 42 additions & 0 deletions Projects/Skylicht/Collision/Source/Collision/CBBoxPatchBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
!@
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 "CCollisionBuilder.h"

namespace Skylicht
{
class CBBoxPatchBuilder : public CCollisionBuilder
{
public:
CBBoxPatchBuilder();

virtual ~CBBoxPatchBuilder();

virtual void clear();

virtual void build();
};
}
123 changes: 123 additions & 0 deletions Projects/Skylicht/Collision/Source/Collision/CCollisionBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
!@
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 "CCollisionBuilder.h"

#include "GameObject/CGameObject.h"

namespace Skylicht
{
CCollisionBuilder::CCollisionBuilder()
{

}

CCollisionBuilder::~CCollisionBuilder()
{

}

void CCollisionBuilder::removeCollision(CGameObject* object)
{
for (u32 i = 0, n = m_nodes.size(); i < n; i++)
{
if (m_nodes[i]->GameObject == object)
{
if (n >= 2)
{
// swap to last and delete
CCollisionNode* t = m_nodes[n - 1];
m_nodes[n - 1] = m_nodes[i];
m_nodes[i] = t;
delete m_nodes[n - 1];
m_nodes.erase(n - 1);
return;
}
else
{
delete m_nodes[i];
m_nodes.erase(i);
return;
}
}
}

build();
}

void CCollisionBuilder::removeCollision(CCollisionNode** nodes, int count)
{
for (u32 i = 0, n = m_nodes.size(); i < n; i++)
{
int id = findNode(m_nodes[i], nodes, count);
if (id >= 0)
{
nodes[id] = NULL;

if (n >= 2)
{
// swap to last and delete
CCollisionNode* t = m_nodes[n - 1];
m_nodes[n - 1] = m_nodes[i];
m_nodes[i] = t;
delete m_nodes[n - 1];
m_nodes.erase(n - 1);
return;
}
else
{
delete m_nodes[i];
m_nodes.erase(i);
return;
}
}
}

build();
}

void CCollisionBuilder::clear()
{
for (u32 i = 0, n = m_nodes.size(); i < n; i++)
{
delete m_nodes[i]->Selector;
delete m_nodes[i];
}
m_nodes.clear();
}

int CCollisionBuilder::findNode(CCollisionNode* node, CCollisionNode** nodes, int count)
{
for (int i = 0; i < count; i++)
{
if (nodes[i] == node)
{
return i;
}
}

return -1;
}
}
51 changes: 51 additions & 0 deletions Projects/Skylicht/Collision/Source/Collision/CCollisionBuilder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
!@
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 "COctreeNode.h"

namespace Skylicht
{
class CCollisionBuilder
{
protected:
core::array<CCollisionNode*> m_nodes;

public:
CCollisionBuilder();

virtual ~CCollisionBuilder();

void removeCollision(CGameObject* object);

void removeCollision(CCollisionNode** nodes, int count);

virtual void clear();

int findNode(CCollisionNode* node, CCollisionNode** nodes, int count);

virtual void build() = 0;
};
}
Loading

0 comments on commit dec3e75

Please sign in to comment.