Initial commit: ROW Client source code

Game client codebase including:
- CharacterActionControl: Character and creature management
- GlobalScript: Network, items, skills, quests, utilities
- RYLClient: Main client application with GUI and event handlers
- Engine: 3D rendering engine (RYLGL)
- MemoryManager: Custom memory allocation
- Library: Third-party dependencies (DirectX, boost, etc.)
- Tools: Development utilities

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-29 16:24:34 +09:00
commit e067522598
5135 changed files with 1745744 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
#include "../Include/CollisionEllipsoidHelper.h"
namespace CrossM{
namespace Scene{
CCollisionEllipsoidHelper::CCollisionEllipsoidHelper()
{
m_vEllipoidCenter.SetValue(0,0,0);
m_vEllipsoidRaius.SetValue(1,1,1);
m_fHeightBias = 0.0f;
}
CCollisionEllipsoidHelper::~CCollisionEllipsoidHelper()
{
}
void CCollisionEllipsoidHelper::SetEllipsoidRadius(const Math::VECTOR3& vRadius)
{
m_vEllipsoidRaius = vRadius;
}
void CCollisionEllipsoidHelper::SetHeightBias(float f)
{
m_fHeightBias = f;
}
void CCollisionEllipsoidHelper::SetEllipsoidCenter(const Math::VECTOR3& vCenter)
{
m_vEllipoidCenter = vCenter;
}
void CCollisionEllipsoidHelper::SetPosition(const Math::VECTOR3& vPos)
{
m_vEllipoidCenter.SetValue(vPos.x, vPos.y-m_fHeightBias, vPos.z);
}
const Math::VECTOR3& CCollisionEllipsoidHelper::GetEllipsoidRadius()
{
return m_vEllipsoidRaius;
}
const Math::VECTOR3& CCollisionEllipsoidHelper::GetEllipsoidCenter()
{
return m_vEllipoidCenter;
}
Math::VECTOR3 CCollisionEllipsoidHelper::GetPosition()
{
return Math::VECTOR3(m_vEllipoidCenter.x, m_vEllipoidCenter.y+m_fHeightBias, m_vEllipoidCenter.z);
}
}}

View File

@@ -0,0 +1,419 @@
#include "../Include/MathUtil.h"
#include <algorithm>
namespace CrossM{
namespace Math{
// 삼각형이 AABB 안에 완전히 포함되는지 검사
bool IsTriangleInAabb(const VECTOR3& vAabbMin, const VECTOR3& vAabbMax, const VECTOR3& vTri0, const VECTOR3& vTri1, const VECTOR3& vTri2)
{
if (vAabbMin.x <= vTri0.x && vTri0.x <= vAabbMax.x &&
vAabbMin.y <= vTri0.y && vTri0.y <= vAabbMax.y &&
vAabbMin.z <= vTri0.z && vTri0.z <= vAabbMax.z &&
vAabbMin.x <= vTri1.x && vTri1.x <= vAabbMax.x &&
vAabbMin.y <= vTri1.y && vTri1.y <= vAabbMax.y &&
vAabbMin.z <= vTri1.z && vTri1.z <= vAabbMax.z &&
vAabbMin.x <= vTri2.x && vTri2.x <= vAabbMax.x &&
vAabbMin.y <= vTri2.y && vTri2.y <= vAabbMax.y &&
vAabbMin.z <= vTri2.z && vTri2.z <= vAabbMax.z)
{
return true;
}
return false;
}
// 삼각형과 AABB 가 만나는지를 검사
bool CheckAabbTriangleIntersection(const VECTOR3& vAabbMin, const VECTOR3& vAabbMax, const VECTOR3& vTri0, const VECTOR3& vTri1, const VECTOR3& vTri2)
{
// Separation of Axes 에 따른 AABB - triangle intersection test 구현
// 6 축에 대해 AABB 와 삼각형을 projection 한 뒤, 둘이 겹치는지 여부를 확인한다
// 모든 축에 대해 겹치면 둘은 만나는것이고, 한 축에 대해서라도 겹치지 않는다면 만나지 않는 것이다
float fBoxProjectionMin, fBoxProjectionMax;
float fTriProjectionMin, fTriProjectionMax;
// AABB 의 세 축에 대한 projection 을 검사
// X 축
fBoxProjectionMin = vAabbMin.x;
fBoxProjectionMax = vAabbMax.x;
fTriProjectionMin = std::min(vTri0.x, std::min(vTri1.x, vTri2.x));
fTriProjectionMax = std::max(vTri0.x, std::max(vTri1.x, vTri2.x));
if (false == IsRangeOverlap(fTriProjectionMin, fTriProjectionMax, fBoxProjectionMin, fBoxProjectionMax))
{
return false;
}
// Y 축
fBoxProjectionMin = vAabbMin.y;
fBoxProjectionMax = vAabbMax.y;
fTriProjectionMin = std::min(vTri0.y, std::min(vTri1.y, vTri2.y));
fTriProjectionMax = std::max(vTri0.y, std::max(vTri1.y, vTri2.y));
if (false == IsRangeOverlap(fTriProjectionMin, fTriProjectionMax, fBoxProjectionMin, fBoxProjectionMax))
{
return false;
}
// Z 축
fBoxProjectionMin = vAabbMin.z;
fBoxProjectionMax = vAabbMax.z;
fTriProjectionMin = std::min(vTri0.z, std::min(vTri1.z, vTri2.z));
fTriProjectionMax = std::max(vTri0.z, std::max(vTri1.z, vTri2.z));
if (false == IsRangeOverlap(fTriProjectionMin, fTriProjectionMax, fBoxProjectionMin, fBoxProjectionMax))
{
return false;
}
// 삼각형의 의 세 축에 대한 projection 을 검사
VECTOR3 avAxis[3]; // 삼각형의 세 모서리
Math::Subtract(avAxis[0], vTri1, vTri0);
Math::Subtract(avAxis[1], vTri2, vTri0);
Math::Subtract(avAxis[2], vTri2, vTri1);
// 삼각형 각 점을 축에 projection 한 값
float fProjectionTri0, fProjectionTri1, fProjectionTri2;
for (int i = 0; i < 3; ++i)
{
VECTOR3& vAxis = avAxis[i];
// AABB 의 max/min 점의 xyz 는 각각의 최대/최소값이므로,
// axis 의 각 컴포넌트와 내적했을때 최대값을 구하려면
// x,y,z 각각의 곱이 큰 쪽 끼리 골라서 더하면 된다.
// (어차피 AABB 여덟점이 xyz 각각의 최대/최소간의 조합이므로,
// x,y,z 를 각각 임의로 최대/최소를 선택한다 해도 어뎗 점 안에 모두 포함되는 조합이다.)
fBoxProjectionMin =
((vAxis.x > 0) ? vAabbMin.x : vAabbMax.x) * vAxis.x +
((vAxis.y > 0) ? vAabbMin.y : vAabbMax.y) * vAxis.y +
((vAxis.z > 0) ? vAabbMin.z : vAabbMax.z) * vAxis.z;
fBoxProjectionMax =
((vAxis.x > 0) ? vAabbMax.x : vAabbMin.x) * vAxis.x +
((vAxis.y > 0) ? vAabbMax.y : vAabbMin.y) * vAxis.y +
((vAxis.z > 0) ? vAabbMax.z : vAabbMin.z) * vAxis.z;
fProjectionTri0 = Math::DotProduct(vTri0, vAxis);
fProjectionTri1 = Math::DotProduct(vTri1, vAxis);
fProjectionTri2 = Math::DotProduct(vTri2, vAxis);
fTriProjectionMin = std::min(fProjectionTri0, std::min(fProjectionTri1, fProjectionTri2));
fTriProjectionMax = std::max(fProjectionTri0, std::max(fProjectionTri1, fProjectionTri2));
if (false == IsRangeOverlap(fTriProjectionMin, fTriProjectionMax, fBoxProjectionMin, fBoxProjectionMax))
{
return false;
}
}
return true;
}
// 두 AABB 가 만나는지 검사
bool CheckAabbAabbIntersection(const VECTOR3& vAabb1Min, const VECTOR3& vAabb1Max, const VECTOR3& vAabb2Min, const VECTOR3& vAabb2Max)
{
if (false ==IsRangeOverlap(vAabb1Min.x, vAabb1Max.x, vAabb2Min.x, vAabb2Max.x)) return false;
if (false ==IsRangeOverlap(vAabb1Min.y, vAabb1Max.y, vAabb2Min.y, vAabb2Max.y)) return false;
if (false ==IsRangeOverlap(vAabb1Min.z, vAabb1Max.z, vAabb2Min.z, vAabb2Max.z)) return false;
return true;
}
// CheckTriangleSweepingSphereCollision 의 부속 함수.
// ax^2 + bx + c 의 해 중 min~max 범위 내의 가장 작은 해를 반환한다. 만족하는 해가 없으면 false 반환
static bool GetLowestRootInRange(const float a, const float b, const float c, const float fMinRoot, const float fMaxRoot, float& fRoot)
{
// 실수해가 존재하는지 판별식 계산
float fDeterminant = b*b - 4.0f*a*c;
// 실수해가 존재하지 않으면 해 없음
if (fDeterminant < 0.0f) return false;
// 두개의 해를 계산한다
float fSqrtD = sqrtf(fDeterminant);
float r1 = (-b - fSqrtD) / (2*a);
float r2 = (-b + fSqrtD) / (2*a);
// r1 < r2 의 크기가 되도록 정렬
if (r1 > r2) std::swap(r1, r2);
// 작은쪽부터 범위 내인지 판별해, 범위내일경우 해로서 리턴
if (r1 > fMinRoot && r1 < fMaxRoot)
{
fRoot = r1;
return true;
}
// 작은 해 쪽이 범위내에 들지 않으면, 큰 해 쪽이 범위내인지 검사한다
if (r2 > fMinRoot && r2 < fMaxRoot)
{
fRoot = r2;
return true;
}
return false;
}
bool CheckTriangleSweepingSphereCollision(float &fOutT, VECTOR3& vOutContactPoint, bool& bOutContactInsideTriangle, const TriangSweepingSphere& triAndSphere)
{
// 약간의 alias
const Math::VECTOR3 &vBasePoint = triAndSphere.m_vSphereSweepStart;
const Math::VECTOR3 &vTri0 = triAndSphere.m_vTri0;
const Math::VECTOR3 &vTri1 = triAndSphere.m_vTri1;
const Math::VECTOR3 &vTri2 = triAndSphere.m_vTri2;
const Math::VECTOR3 &vPath = triAndSphere.m_vSphereSweepPath;
// 삼각형의 세 모서리
Math::VECTOR3 vTriEdge01, vTriEdge02, vTriEdge12;
// 삼각형을 포함하는 평면. 추후 plane 클래스 등으로 만들어야 할 듯
Math::VECTOR3 vTriPlaneNormal;
float fTriPlaneConstant;
// 삼각형의 세 모서리 벡터를 구하고..
Math::Subtract(vTriEdge01, vTri1, vTri0);
Math::Subtract(vTriEdge02, vTri2, vTri0);
Math::Subtract(vTriEdge12, vTri2, vTri1);
// 삼각형이 포함된 평면의 파라미터들을 구함
Math::CrossProduct(vTriPlaneNormal, vTriEdge01, vTriEdge02);
Math::Normalize(vTriPlaneNormal, vTriPlaneNormal);
fTriPlaneConstant = -Math::DotProduct(vTriPlaneNormal, vTri0);
// sweeping path 와 충돌평면법선 내적
float fNormalDotPath = Math::DotProduct(vTriPlaneNormal, vPath);
// sphere 의 진행방향이 같은 방향이면 체크안함
if (fNormalDotPath > 0.0f)
{
return false;
}
float t0, t1;
bool bEmbededInPlane = false;
float fSignedDistBaseToTriPlane = Math::DotProduct(vTriPlaneNormal, vBasePoint) + fTriPlaneConstant;
if (0.0f == fNormalDotPath)
{
// sphere 가 삼각형면과 평행하게 진행
if (fabs(fSignedDistBaseToTriPlane) >= triAndSphere.m_fSphereRadius)
{
return false; // 삼각형면에서 멀리 떨어져서 평행이동중
}
else
{
bEmbededInPlane = true;
t0 = 0.0f;
t1 = 1.0f;
}
}
else
{
t0 = (-triAndSphere.m_fSphereRadius-fSignedDistBaseToTriPlane)/fNormalDotPath;
t1 = (triAndSphere.m_fSphereRadius-fSignedDistBaseToTriPlane)/fNormalDotPath;
// t0 < t1 으로 소트
if (t0 > t1)
{
std::swap(t0, t1);
}
// t 구간이 sphere 이동경로 선분 내에 있지 않다
if (t0 > 1.0f || t1 <0.0f)
{
return false;
}
// t 값을 0~1 구간내로 클램핑
t0 = std::max(t0, 0.0f);
t1 = std::min(t1, 1.0f);
}
VECTOR3 vContactPoint;
bool bFoundCollision = false;
float t = 1.0f;
// 삼각형 내부와 체크
if (!bEmbededInPlane)
{
vContactPoint = vBasePoint + (vPath*t0) - vTriPlaneNormal;
if (IsPointInTriangle(vContactPoint, vTri0, vTri1, vTri2))
{
bFoundCollision = true;
t = t0;
}
}
// 아직 충돌점을 찾지 못했다면 모서리와 꼭지점에 대해 테스트를 해야한다
if (!bFoundCollision)
{
float fSQuaredPathLength = GetSquaredLength(vPath);
float a, b, c;
float newT;
float fSquaredRadius = triAndSphere.m_fSphereRadius*triAndSphere.m_fSphereRadius;
a = fSQuaredPathLength;
// vTri0
b = 2.0f * (DotProduct(vPath, vBasePoint - vTri0));
c = (Math::GetSquaredLength(vTri0 - vBasePoint) - fSquaredRadius);
if (GetLowestRootInRange(a, b, c, 0.0f, t, newT))
{
t = newT;
bFoundCollision = true;
vContactPoint = vTri0;
}
// vTri1
if (bFoundCollision)
{
b = 2.0f * (DotProduct(vPath, vBasePoint - vTri1));
c = (Math::GetSquaredLength(vTri1 - vBasePoint) - fSquaredRadius);
if (GetLowestRootInRange(a, b, c, 0.0f, t, newT))
{
t = newT;
bFoundCollision = true;
vContactPoint = vTri1;
}
}
// vTri2
if (bFoundCollision)
{
b = 2.0f * (DotProduct(vPath, vBasePoint - vTri2));
c = (Math::GetSquaredLength(vTri2 - vBasePoint) - fSquaredRadius);
if (GetLowestRootInRange(a, b, c, 0.0f, t, newT))
{
t = newT;
bFoundCollision = true;
vContactPoint = vTri0;
}
}
// 모서리에 대해 테스트
VECTOR3 vBaseToVertex;
float fEdgeSquaredLength;
float fEdgeDotPath;
float fEdgeDotBaseToVertex;
// vTri0 ~ vTri1
vBaseToVertex = vTri0 - vBasePoint;
fEdgeSquaredLength = GetSquaredLength(vTriEdge01);
fEdgeDotPath = DotProduct(vTriEdge01, vPath);
fEdgeDotBaseToVertex = DotProduct(vTriEdge01, vBaseToVertex);
a = fEdgeSquaredLength* -fSQuaredPathLength +
fEdgeDotPath*fEdgeDotPath;
b = fEdgeSquaredLength* (2.0f*Math::DotProduct(vPath, vBaseToVertex)) -
2.0f*fEdgeDotPath*fEdgeDotBaseToVertex;
c = (fEdgeSquaredLength* (1.0f - Math::GetSquaredLength(vBaseToVertex)) +
fEdgeDotBaseToVertex*fEdgeDotBaseToVertex);
if (GetLowestRootInRange(a, b, c, 0.0f, t, newT))
{
float f = (fEdgeDotPath*t - fEdgeDotBaseToVertex) / fEdgeSquaredLength;
if (f >= 0.0f && f <= 1.0f)
{
t = newT;
bFoundCollision = true;
vContactPoint = vTri0 + vTriEdge01*f;
}
}
// vTri0 ~ vTri2
vBaseToVertex = vTri0 - vBasePoint;
fEdgeSquaredLength = GetSquaredLength(vTriEdge02);
fEdgeDotPath = DotProduct(vTriEdge02, vPath);
fEdgeDotBaseToVertex = DotProduct(vTriEdge02, vBaseToVertex);
a = fEdgeSquaredLength* -fSQuaredPathLength +
fEdgeDotPath*fEdgeDotPath;
b = fEdgeSquaredLength* (2.0f*Math::DotProduct(vPath, vBaseToVertex)) -
2.0f*fEdgeDotPath*fEdgeDotBaseToVertex;
c = (fEdgeSquaredLength* (1.0f - Math::GetSquaredLength(vBaseToVertex)) +
fEdgeDotBaseToVertex*fEdgeDotBaseToVertex);
if (GetLowestRootInRange(a, b, c, 0.0f, t, newT))
{
float f = (fEdgeDotPath*t - fEdgeDotBaseToVertex) / fEdgeSquaredLength;
if (f >= 0.0f && f <= 1.0f)
{
t = newT;
bFoundCollision = true;
vContactPoint = vTri0 + vTriEdge02*f;
}
}
// vTri1 ~ vTri2
vBaseToVertex = vTri1 - vBasePoint;
fEdgeSquaredLength = GetSquaredLength(vTriEdge12);
fEdgeDotPath = DotProduct(vTriEdge12, vPath);
fEdgeDotBaseToVertex = DotProduct(vTriEdge12, vBaseToVertex);
a = fEdgeSquaredLength* -fSQuaredPathLength +
fEdgeDotPath*fEdgeDotPath;
b = fEdgeSquaredLength* (2.0f*Math::DotProduct(vPath, vBaseToVertex)) -
2.0f*fEdgeDotPath*fEdgeDotBaseToVertex;
c = (fEdgeSquaredLength* (1.0f - Math::GetSquaredLength(vBaseToVertex)) +
fEdgeDotBaseToVertex*fEdgeDotBaseToVertex);
if (GetLowestRootInRange(a, b, c, 0.0f, t, newT))
{
float f = (fEdgeDotPath*t - fEdgeDotBaseToVertex) / fEdgeSquaredLength;
if (f >= 0.0f && f <= 1.0f)
{
t = newT;
bFoundCollision = true;
vContactPoint = vTri1 + vTriEdge12*f;
}
}
}
if (bFoundCollision)
{
fOutT = t;
vOutContactPoint = vContactPoint;
return true;
}
return false;
}
// 지정된 점이 삼각형 내에 포함되는지 확인하는 코드
// Fauerby 의 글에 Keidy 라는 사람이 기고한 가장 빠른 루틴이라고 한다
// 수학적인 증명은.. 모르겠다-_- 일단 이용
bool IsPointInTriangle(const VECTOR3& p, const VECTOR3& vTri0, const VECTOR3& vTri1, const VECTOR3& vTri2)
{
// 원점을 낀 두 삼각형 모서리, 원점을 기준으로 한 p 의 위치벡터
VECTOR3 vEdge1, vEdge2, pTri;
Math::Subtract(vEdge1, vTri1, vTri0);
Math::Subtract(vEdge2, vTri2, vTri0);
Math::Subtract(pTri, p, vTri0);
float a = Math::DotProduct(vEdge1, vEdge1);
float b = Math::DotProduct(vEdge1, vEdge2);
float c = Math::DotProduct(vEdge2, vEdge2);
float d = Math::DotProduct(pTri, vEdge1);
float e = Math::DotProduct(pTri, vEdge2);
float x = d*c - e*b;
float y = e*a - d*b;
float z = x + y - (a*c - b*b);
return ( ((unsigned int&)z) & ~ ( ((unsigned int&)x) | ((unsigned int&)y) ) & 0x80000000) != 0 ? true : false;
}
}}

View File

@@ -0,0 +1,541 @@
#include "../Include/OctreeCollider.h"
#include "../Include/MathUtil.h"
#include <algorithm>
#include <windows.h>
#include <d3d8.h>
#include <d3dx8.h>
namespace CrossM{
namespace Scene{
COctreeCollisionNode::COctreeCollisionNode()
{
for (int i = 0; i < 8; ++i)
{
m_apSubNode[i] = NULL;
}
m_vBoundingMin.SetValue(0,0,0);
m_vBoundingMax.SetValue(0,0,0);
}
COctreeCollisionNode::~COctreeCollisionNode()
{
ReleaseSubNode();
}
void COctreeCollisionNode::ReleaseSubNode()
{
for (int i = 0; i < 8; ++i)
{
if (NULL != m_apSubNode[i])
{
delete m_apSubNode[i];
m_apSubNode[i] = NULL;
}
}
}
// 자식노드가 전혀 없는 것이 리프 노드
bool COctreeCollisionNode::IsLeafNode()
{
for (int i = 0; i < 8; ++i)
{
if (NULL != m_apSubNode[i])
{
return false;
}
}
return true;
}
void COctreeCollisionNode::BuildSubNode(
const std::vector< CollisionTriangleInfo >& vecTriangle,
const size_t nMaximumRecursion, const size_t nMinPolyCount,
size_t nCurrentRecursionLevel)
{
static size_t nProcessedTri = 0;
unsigned int i, j;
// recursion level 이 너무 깊거나, 포함된 face 갯수가 정해진것보다 작으면
// 더이상 sub node 를 나누지 않는다 (재귀 종료 조건)
if (nCurrentRecursionLevel >= nMaximumRecursion ||
m_vecTriangleIndex.size() <= nMinPolyCount)
{
nProcessedTri += m_vecTriangleIndex.size();
printf("\r%d / %d", nProcessedTri, vecTriangle.size());
return;
}
// 자식 노드들의 bounding box min/max
Math::VECTOR3 aSubNodeBoundingMin[8];
Math::VECTOR3 aSubNodeBoundingMax[8];
Math::VECTOR3 vMedian = (m_vBoundingMin + m_vBoundingMax)/2.0f;
// 바운딩 박스 값 설정
for (i = 0; i < 8; ++i)
{
if (0 == (i & 1))
{
aSubNodeBoundingMin[i].x = m_vBoundingMin.x;
aSubNodeBoundingMax[i].x = vMedian.x;
}
else
{
aSubNodeBoundingMin[i].x = vMedian.x;
aSubNodeBoundingMax[i].x = m_vBoundingMax.x;
}
if (0 == (i & 2))
{
aSubNodeBoundingMin[i].y = m_vBoundingMin.y;
aSubNodeBoundingMax[i].y = vMedian.y;
}
else
{
aSubNodeBoundingMin[i].y = vMedian.y;
aSubNodeBoundingMax[i].y = m_vBoundingMax.y;
}
if (0 == (i & 4))
{
aSubNodeBoundingMin[i].z = m_vBoundingMin.z;
aSubNodeBoundingMax[i].z = vMedian.z;
}
else
{
aSubNodeBoundingMin[i].z = vMedian.z;
aSubNodeBoundingMax[i].z = m_vBoundingMax.z;
}
}
// 넘겨받은 삼각형 인덱스에서 하부 노드로 내려갈것들과 이 노드에 저장될것들을 구분해낸다
for (j = 0; j < m_vecTriangleIndex.size(); ++j)
{
// 삼각형 세 점 얻기
const CollisionTriangleInfo &tri = vecTriangle[ m_vecTriangleIndex[j] ];
const Math::VECTOR3 &vTri0 = tri.m_avVertex[0];
const Math::VECTOR3 &vTri1 = tri.m_avVertex[1];
const Math::VECTOR3 &vTri2 = tri.m_avVertex[2];
// 각 자식노드들에 대해 삼각형이 포함되는지 체크한다
bool bIncludedInSubNode = false; // 자식 노드에 삼각형이 포함되는지 여부를 나타내는 플래그
for (i = 0; i < 8; ++i)
{
if (Math::IsTriangleInAabb(aSubNodeBoundingMin[i], aSubNodeBoundingMax[i], vTri0, vTri1, vTri2))
{
// 노드가 할당되지 않았으면 할당
COctreeCollisionNode* &pNode = m_apSubNode[i];
if (NULL == pNode)
{
pNode = new COctreeCollisionNode;
pNode->m_vBoundingMin = aSubNodeBoundingMin[i];
pNode->m_vBoundingMax = aSubNodeBoundingMax[i];
}
// 자식노드에 넘겨줄 삼각형 인덱스에 저장
pNode->m_vecTriangleIndex.push_back(m_vecTriangleIndex[j]);
bIncludedInSubNode = true;
break;
}
}
if (!bIncludedInSubNode)
{
// 한 노드에 완전히 포함되는것이 아니라 다수 노드간에 걸치는 삼각형이므로,
// triangle - AABB intersection 테스트를 한다
for (i = 0; i < 8; ++i)
{
if (Math::CheckAabbTriangleIntersection(aSubNodeBoundingMin[i], aSubNodeBoundingMax[i], vTri0, vTri1, vTri2))
{
// 노드가 할당되지 않았으면 할당
COctreeCollisionNode* &pNode = m_apSubNode[i];
if (NULL == pNode)
{
pNode = new COctreeCollisionNode;
pNode->m_vBoundingMin = aSubNodeBoundingMin[i];
pNode->m_vBoundingMax = aSubNodeBoundingMax[i];
}
// 자식노드에 넘겨줄 삼각형 인덱스에 저장
pNode->m_vecTriangleIndex.push_back(m_vecTriangleIndex[j]);
}
}
}
}
// 삼각형 인덱스를 자식 노드에 모두 넘겨주었으므로 더 이상 인덱스를 들고 있을 필요가 없다
m_vecTriangleIndex.clear();
// 삼각형이 있는 자식 노드는 BuildSubNode 를 호출한다
for (i = 0; i < 8; ++i)
{
if (NULL != m_apSubNode[i])
{
m_apSubNode[i]->BuildSubNode(vecTriangle, nMaximumRecursion, nMinPolyCount, nCurrentRecursionLevel+1);
}
}
}
void COctreeCollisionNode::CollectCollidableNodes(
const Math::VECTOR3& vSweptVolumeMin, const Math::VECTOR3& vSweptVolumeMax,
std::vector< COctreeCollisionNode* >& vecCollidableNode)
{
if(!Math::CheckAabbAabbIntersection(vSweptVolumeMin, vSweptVolumeMax, m_vBoundingMin, m_vBoundingMax))
{
return;
}
if (IsLeafNode())
{
vecCollidableNode.push_back(this);
}
else
{
for (int i = 0; i < 8; ++i)
{
if (NULL != m_apSubNode[i])
{
m_apSubNode[i]->CollectCollidableNodes(vSweptVolumeMin, vSweptVolumeMax, vecCollidableNode);
}
}
}
}
//////////////////////////////////////////////////////////////////////////
COctreeCollider::COctreeCollider()
{
m_nColTriIndex = 0;
}
COctreeCollider::~COctreeCollider()
{
}
void COctreeCollider::SetTriangleCount(unsigned int uiTriangleCount)
{
m_vecCollisionTriangle.resize(uiTriangleCount);
}
void COctreeCollider::GetTriangleDataPtr(CollisionTriangleInfo*& pTriangleData)
{
if (m_vecCollisionTriangle.size() > 0)
{
pTriangleData = &(m_vecCollisionTriangle[0]);
}
else
{
pTriangleData = NULL;
}
}
bool COctreeCollider::BuildOctree(const size_t nMaximumRecursion, const size_t nMinPolyCount)
{
unsigned int i, j;
if (0 == m_vecCollisionTriangle.size())
{
m_vCollisionBoundingMin.SetValue(0,0,0);
m_vCollisionBoundingMax.SetValue(0,0,0);
return true; // 메시 데이터가 비어 있어도 일단은 정상종료..
}
// 일단 기존 노드들을 삭제
m_octreeRootNode.ReleaseSubNode();
// bounding min/max 구하기
m_vCollisionBoundingMin = m_vecCollisionTriangle[0].m_avVertex[0];
m_vCollisionBoundingMax = m_vecCollisionTriangle[0].m_avVertex[0];
for (i = 0; i < m_vecCollisionTriangle.size(); ++i)
{
for (j = 0; j < 3; ++j)
{
CrossM::Math::VECTOR3& v = m_vecCollisionTriangle[i].m_avVertex[j];
if (v.x < m_vCollisionBoundingMin.x) m_vCollisionBoundingMin.x = v.x;
if (v.y < m_vCollisionBoundingMin.y) m_vCollisionBoundingMin.y = v.y;
if (v.z < m_vCollisionBoundingMin.z) m_vCollisionBoundingMin.z = v.z;
if (v.x > m_vCollisionBoundingMax.x) m_vCollisionBoundingMax.x = v.x;
if (v.y > m_vCollisionBoundingMax.y) m_vCollisionBoundingMax.y = v.y;
if (v.z > m_vCollisionBoundingMax.z) m_vCollisionBoundingMax.z = v.z;
}
}
// octree root node 에 바운딩 박스 값 세팅
m_octreeRootNode.m_vBoundingMin = m_vCollisionBoundingMin;
m_octreeRootNode.m_vBoundingMax = m_vCollisionBoundingMax;
// octree root node 에 포함된 삼각형의 인덱스(전체)를 세팅
m_octreeRootNode.m_vecTriangleIndex.resize(m_vecCollisionTriangle.size());
for (i = 0; i < m_octreeRootNode.m_vecTriangleIndex.size(); ++i)
{
m_octreeRootNode.m_vecTriangleIndex[i] = i;
}
// DWORD dwTime = timeGetTime();
// for (i = 0; i < m_vecCollisionTriangle.size(); ++i)
// {
// CollisionTriangleInfo& tri = m_vecCollisionTriangle[i];
// Math::VECTOR3& vTri0 = m_vecCollisionVertex[ tri.m_lIndex[0] ];
// Math::VECTOR3& vTri1 = m_vecCollisionVertex[ tri.m_lIndex[1] ];
// Math::VECTOR3& vTri2 = m_vecCollisionVertex[ tri.m_lIndex[2] ];
//
// Math::CheckAabbTriangleIntersection(m_vCollisionBoundingMin, m_vCollisionBoundingMax, vTri0, vTri1, vTri2);
// }
// DWORD dwElapsed = timeGetTime() - dwTime;
//
// dwTime = timeGetTime();
// for (i = 0; i < m_vecCollisionTriangle.size(); ++i)
// {
// CollisionTriangleInfo& tri = m_vecCollisionTriangle[i];
// Math::VECTOR3& vTri0 = m_vecCollisionVertex[ tri.m_lIndex[0] ];
// Math::VECTOR3& vTri1 = m_vecCollisionVertex[ tri.m_lIndex[1] ];
// Math::VECTOR3& vTri2 = m_vecCollisionVertex[ tri.m_lIndex[2] ];
//
// Math::IsTriangleInAabb(m_vCollisionBoundingMin, m_vCollisionBoundingMax, vTri0, vTri1, vTri2);
// }
// DWORD dwElapsed2 = timeGetTime() - dwTime;
// 하부 노드들을 생성
m_octreeRootNode.BuildSubNode(m_vecCollisionTriangle, nMaximumRecursion, nMinPolyCount, 0);
return true;
}
void COctreeCollider::GetCollisionRespondingPosition(
Math::VECTOR3& vOutRespondingPos,
const Math::VECTOR3& vPrevPos, const Math::VECTOR3& vNewPos,
const Math::VECTOR3& vEllipsoidRadius,
const unsigned int nRecursionLevel)
{
size_t i, j, nMinCollisionTriangleIndex;
// ellipsoid 가 쓸고 지나간 궤적을 커버하는 사각형(AABB)의 min/max 점
Math::VECTOR3 vSweptVolumeMin, vSweptVolumeMax;
vSweptVolumeMin.x = min(vPrevPos.x, vNewPos.x) - vEllipsoidRadius.x;
vSweptVolumeMax.x = max(vPrevPos.x, vNewPos.x) + vEllipsoidRadius.x;
vSweptVolumeMin.y = min(vPrevPos.y, vNewPos.y) - vEllipsoidRadius.y;
vSweptVolumeMax.y = max(vPrevPos.y, vNewPos.y) + vEllipsoidRadius.y;
vSweptVolumeMin.z = min(vPrevPos.z, vNewPos.z) - vEllipsoidRadius.z;
vSweptVolumeMax.z = max(vPrevPos.z, vNewPos.z) + vEllipsoidRadius.z;
m_vecpCollidableNode.clear();
m_octreeRootNode.CollectCollidableNodes(vSweptVolumeMin, vSweptVolumeMax, m_vecpCollidableNode);
if (0 == m_vecpCollidableNode.size())
{
// 충돌 여지가 있는 삼각형조차 없다
vOutRespondingPos = vNewPos;
return;
}
// ellipsoid 좌표계 상의 값들. sweep 시작, 끝점과 sweep 방향벡터, sweep 구간의 길이
Math::VECTOR3 vESweepStart, vESweepEnd, vESweepPath;
float fESweepLength;
vESweepStart.SetValue(vPrevPos.x/vEllipsoidRadius.x, vPrevPos.y/vEllipsoidRadius.y, vPrevPos.z/vEllipsoidRadius.z);
vESweepEnd.SetValue(vNewPos.x/vEllipsoidRadius.x, vNewPos.y/vEllipsoidRadius.y, vNewPos.z/vEllipsoidRadius.z);
Math::Subtract(vESweepPath, vESweepEnd, vESweepStart);
fESweepLength = Math::GetLength(vESweepPath);
// 움직임이 없는 경우
if (fESweepLength < Math::F_EPSILON)
{
vOutRespondingPos = vPrevPos;
return;
}
// sweeping sphere 에 대한 정보를 설정
Math::TriangSweepingSphere triAndSphere;
triAndSphere.m_vSphereSweepStart = vESweepStart;
triAndSphere.m_vSphereSweepPath = vESweepPath;
triAndSphere.m_fSphereRadius = 1.0f; // ellipsoid 좌표계이므로 충돌타원체는 단위 구체로 변환되어짐
bool bCollision = false; // 선택된 노드들에서 충돌이 일어난 삼각형이 있는지 여부를 나타내는 플래그
float fMinCollisionDistFactor = 9999.0f;// collision 이 일어난 점이 sweeping path 의 어느 위치인지 나타내는 숫자
// 0 이 시작점, 1 이 끝점임
Math::VECTOR3 vMinContactPoint; // fMinCollisionDistFactor 에서의 접점의 좌표. ellipsoid 좌표계임
bool bMinContactInsideTriangle; // fMinCollisionDistFactor 에서 삼각형과의 접점이 삼각형 내부인지(모서리나 꼭지점이 아닌) 나타내는 플래그
for (i = 0; i < m_vecpCollidableNode.size(); ++i)
{
COctreeCollisionNode& node = *(m_vecpCollidableNode[i]);
for (j = 0; j < node.m_vecTriangleIndex.size(); ++j)
{
CollisionTriangleInfo& tri = m_vecCollisionTriangle[ node.m_vecTriangleIndex[j] ];
// 삼각형 정보를 설정
Math::VECTOR3& vTri0 = tri.m_avVertex[0];
Math::VECTOR3& vTri1 = tri.m_avVertex[1];
Math::VECTOR3& vTri2 = tri.m_avVertex[2];
// 삼각형 역시 ellipsoid 좌표계로 설정해줘야함을 잊지말것-ㅂ-
triAndSphere.m_vTri0.SetValue(vTri0.x/vEllipsoidRadius.x, vTri0.y/vEllipsoidRadius.y, vTri0.z/vEllipsoidRadius.z);
triAndSphere.m_vTri1.SetValue(vTri1.x/vEllipsoidRadius.x, vTri1.y/vEllipsoidRadius.y, vTri1.z/vEllipsoidRadius.z);
triAndSphere.m_vTri2.SetValue(vTri2.x/vEllipsoidRadius.x, vTri2.y/vEllipsoidRadius.y, vTri2.z/vEllipsoidRadius.z);
float fCollisionDistFactor;
Math::VECTOR3 vContactPoint;
bool bContactInside;
// 삼각형 - sweeping sphere 충돌 검사
if (Math::CheckTriangleSweepingSphereCollision(fCollisionDistFactor, vContactPoint, bContactInside, triAndSphere))
{
// 충돌이면
bCollision = true;
if(fCollisionDistFactor < fMinCollisionDistFactor)
{
vMinContactPoint = vContactPoint;
fMinCollisionDistFactor = fCollisionDistFactor;
nMinCollisionTriangleIndex = node.m_vecTriangleIndex[j];
bMinContactInsideTriangle = bContactInside;
}
}
}
}
if (!bCollision)
{
// 충돌 없심
vOutRespondingPos = vNewPos;
return;
}
m_nColTriIndex = nMinCollisionTriangleIndex;
// collision response phase
Math::VECTOR3 vMovedPos; // 일단 충돌이 일어난점까지 이동한 위치
Math::Lerp(vMovedPos, vPrevPos, vNewPos, fMinCollisionDistFactor); // 일단 이동 가능한 위치로 옮겨놓은 뒤..
// 원래 이동하려한 거리의 90% 이상을 진행했거나, recursion 이 4번 수행되었으면 종료한다.
if (fMinCollisionDistFactor > 0.9f || nRecursionLevel >= 4)
{
vOutRespondingPos = vMovedPos;
return;
}
// 접점의 좌표를 ellipsoid 좌표계에서 원래 좌표계로 환산
vMinContactPoint.x *= vEllipsoidRadius.x;
vMinContactPoint.y *= vEllipsoidRadius.y;
vMinContactPoint.z *= vEllipsoidRadius.z;
// 접접의 접평면을 구함
Math::VECTOR3 vTangentPlaneNormal;
if (bMinContactInsideTriangle)
{
// 삼각형과의 접점이 삼각형면 내에서라면, 충돌한 삼각형의 normal 을 그대로 이용한다
CollisionTriangleInfo& colTriInfo = m_vecCollisionTriangle[nMinCollisionTriangleIndex];
Math::VECTOR3& vCollTri0 = colTriInfo.m_avVertex[0];
Math::VECTOR3& vCollTri1 = colTriInfo.m_avVertex[1];
Math::VECTOR3& vCollTri2 = colTriInfo.m_avVertex[2];
vTangentPlaneNormal = ((vCollTri1 - vCollTri0) ^ (vCollTri2 - vCollTri0));
}
else
{
// 모서리나 꼭지점과 충돌한 경우, 타원체의 접평면을 구하는 식을 이용해 접면의 normal 을 구한다
Math::Subtract(vTangentPlaneNormal, vMovedPos, vMinContactPoint);
vTangentPlaneNormal.x /= (vEllipsoidRadius.x*vEllipsoidRadius.x);
vTangentPlaneNormal.y /= (vEllipsoidRadius.y*vEllipsoidRadius.y);
vTangentPlaneNormal.z /= (vEllipsoidRadius.z*vEllipsoidRadius.z);
Math::Normalize(vTangentPlaneNormal, vTangentPlaneNormal);
}
Math::Normalize(vTangentPlaneNormal, vTangentPlaneNormal);
// 충돌에 의해 진행하지 못하고 남은 이동성분 벡터를 구함
Math::VECTOR3 vRemainder;
Math::Subtract(vRemainder, vNewPos, vMovedPos);
// 충돌에 의해 움직일수 없어 사라지게 되는 이동성분을 구함
float fVanishingComponentFactor = Math::DotProduct(vTangentPlaneNormal, vRemainder);
Math::VECTOR3 vVanishingComponent;
Math::Scale(vVanishingComponent, vTangentPlaneNormal, fVanishingComponentFactor);
// 남은 이동성분에서 움직일수 없는 방향으로의 성분을 제거
Math::Subtract(vRemainder, vRemainder, vVanishingComponent);
// 남은 이동벡터만큼의 이동을 위해 충돌체크 재귀호출
GetCollisionRespondingPosition(vOutRespondingPos, vMovedPos, vMovedPos+vRemainder, vEllipsoidRadius, nRecursionLevel+1);
}
void COctreeCollider::RenderCollidableNodeTriangles(IDirect3DDevice8* pDevice)
{
size_t nRenderTriCount; //j, i, nTriFilled;
if (0 == m_vecpCollidableNode.size())
{
return;
}
// // 렌더할 삼각형의 갯수 구함
// nRenderTriCount = 0;
// for (i = 0; i < m_vecpCollidableNode.size(); ++i)
// {
// nRenderTriCount += m_vecpCollidableNode[i]->m_vecTriangleIndex.size();
// }
//
// // 버텍스 공간 확보
// m_vecRenderVertex.resize(nRenderTriCount*3);
//
// nTriFilled = 0;
// for (i = 0; i < m_vecpCollidableNode.size(); ++i)
// {
// COctreeCollisionNode& node = *(m_vecpCollidableNode[i]);
//
// for (j = 0; j < node.m_vecTriangleIndex.size(); ++j)
// {
// size_t nTriIndex = node.m_vecTriangleIndex[j];
// CollisionTriangleInfo& tri = m_vecCollisionTriangle[nTriIndex];
//
// m_vecRenderVertex[nTriFilled*3] = tri.m_avVertex[0];
// m_vecRenderVertex[nTriFilled*3 + 1] = tri.m_avVertex[1];
// m_vecRenderVertex[nTriFilled*3 + 2] = tri.m_avVertex[2];
// ++nTriFilled;
// }
// }
nRenderTriCount = 1; // 충돌삼각형 1개만
m_vecRenderVertex.resize(nRenderTriCount*3);
CollisionTriangleInfo& tri = m_vecCollisionTriangle[m_nColTriIndex];
m_vecRenderVertex[0] = tri.m_avVertex[0];
m_vecRenderVertex[1] = tri.m_avVertex[1];
m_vecRenderVertex[2] = tri.m_avVertex[2];
D3DXMATRIX mTmp;
D3DXMatrixIdentity(&mTmp);
pDevice->SetTransform(D3DTS_WORLD, &mTmp);
DWORD dwFillMode, dwZFunc;
pDevice->GetRenderState(D3DRS_FILLMODE, &dwFillMode);
pDevice->GetRenderState(D3DRS_ZFUNC, &dwZFunc);
pDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
pDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
pDevice->SetVertexShader(D3DFVF_XYZ);
pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST, (UINT)nRenderTriCount, &(m_vecRenderVertex[0]), sizeof(Math::VECTOR3));
pDevice->SetRenderState(D3DRS_FILLMODE, dwFillMode);
pDevice->SetRenderState(D3DRS_ZFUNC, dwZFunc);
}
}}