2016-04-20 21:44:18 +00:00
|
|
|
#include "CCollidableOBBTreeGroup.hpp"
|
2016-04-27 00:26:02 +00:00
|
|
|
#include "CCollidableOBBTree.hpp"
|
2017-07-03 03:34:19 +00:00
|
|
|
#include "CCollidableAABox.hpp"
|
|
|
|
#include "CCollidableSphere.hpp"
|
|
|
|
#include "CInternalRayCastStructure.hpp"
|
|
|
|
#include "CollisionUtil.hpp"
|
2016-04-20 21:44:18 +00:00
|
|
|
#include "CToken.hpp"
|
|
|
|
|
|
|
|
namespace urde
|
|
|
|
{
|
2016-04-27 00:26:02 +00:00
|
|
|
const CCollisionPrimitive::Type CCollidableOBBTreeGroup::sType(CCollidableOBBTreeGroup::SetStaticTableIndex, "CCollidableOBBTreeGroup");
|
|
|
|
u32 CCollidableOBBTreeGroup::sTableIndex = -1;
|
2016-04-20 21:44:18 +00:00
|
|
|
|
2017-03-03 22:13:23 +00:00
|
|
|
CCollidableOBBTreeGroupContainer::CCollidableOBBTreeGroupContainer(CInputStream& in)
|
2016-04-20 21:44:18 +00:00
|
|
|
{
|
2016-04-26 10:40:56 +00:00
|
|
|
u32 treeCount = in.readUint32Big();
|
|
|
|
x0_trees.reserve(treeCount);
|
|
|
|
|
|
|
|
for (u32 i = 0 ; i < treeCount ; i++)
|
2016-04-27 00:26:02 +00:00
|
|
|
{
|
|
|
|
std::unique_ptr<COBBTree> tree(new COBBTree(in));
|
|
|
|
x0_trees.push_back(std::move(tree));
|
|
|
|
}
|
|
|
|
|
|
|
|
x10_aabbs.reserve(x0_trees.size());
|
|
|
|
|
|
|
|
for (const std::unique_ptr<COBBTree>& tree : x0_trees)
|
|
|
|
x10_aabbs.push_back(CCollidableOBBTree(tree.get(), CMaterialList()).CalculateLocalAABox());
|
|
|
|
}
|
|
|
|
|
2017-03-03 22:13:23 +00:00
|
|
|
CCollidableOBBTreeGroupContainer::CCollidableOBBTreeGroupContainer(const zeus::CVector3f &, const zeus::CVector3f &)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCollidableOBBTreeGroup::CCollidableOBBTreeGroup(const CCollidableOBBTreeGroupContainer* container,
|
|
|
|
const CMaterialList& matList)
|
|
|
|
: CCollisionPrimitive(matList)
|
|
|
|
, x10_container(container)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-04-27 19:59:38 +00:00
|
|
|
void CCollidableOBBTreeGroup::ResetTestStats() const
|
2016-04-27 00:26:02 +00:00
|
|
|
{
|
2017-07-03 03:34:19 +00:00
|
|
|
/* Remove me? */
|
2016-04-27 00:26:02 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 19:59:38 +00:00
|
|
|
u32 CCollidableOBBTreeGroup::GetTableIndex() const
|
2016-04-27 00:26:02 +00:00
|
|
|
{
|
2016-04-27 19:59:38 +00:00
|
|
|
return sTableIndex;
|
|
|
|
}
|
2016-04-27 00:26:02 +00:00
|
|
|
|
2016-04-27 19:59:38 +00:00
|
|
|
zeus::CAABox CCollidableOBBTreeGroup::CalculateAABox(const zeus::CTransform& xf) const
|
|
|
|
{
|
2017-03-03 22:13:23 +00:00
|
|
|
return x10_container->x20_aabox.getTransformedAABox(xf);
|
2016-04-27 00:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
zeus::CAABox CCollidableOBBTreeGroup::CalculateLocalAABox() const
|
|
|
|
{
|
2017-03-03 22:13:23 +00:00
|
|
|
return x10_container->x20_aabox;
|
2016-04-27 00:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FourCC CCollidableOBBTreeGroup::GetPrimType() const
|
|
|
|
{
|
|
|
|
return SBIG('OBTG');
|
|
|
|
}
|
|
|
|
|
2017-07-03 03:34:19 +00:00
|
|
|
CRayCastResult CCollidableOBBTreeGroup::CastRayInternal(const CInternalRayCastStructure& rayCast) const
|
2016-04-27 00:26:02 +00:00
|
|
|
{
|
2017-07-03 03:34:19 +00:00
|
|
|
CRayCastResult ret;
|
|
|
|
|
|
|
|
zeus::CMRay xfRay = rayCast.GetRay().getInvUnscaledTransformRay(rayCast.GetTransform());
|
|
|
|
auto aabbIt = x10_container->x10_aabbs.cbegin();
|
|
|
|
float mag = rayCast.GetMaxTime();
|
|
|
|
for (const std::unique_ptr<COBBTree>& tree : x10_container->x0_trees)
|
|
|
|
{
|
|
|
|
CCollidableOBBTree obbTree(tree.get(), GetMaterial());
|
|
|
|
float tMin = 0.f;
|
|
|
|
float tMax = 0.f;
|
|
|
|
if (CollisionUtil::RayAABoxIntersection(xfRay, *aabbIt++, tMin, tMax))
|
|
|
|
{
|
|
|
|
CInternalRayCastStructure localCast(xfRay.start, xfRay.dir, mag,
|
|
|
|
zeus::CTransform::Identity(), rayCast.GetFilter());
|
|
|
|
CRayCastResult localResult = obbTree.CastRayInternal(localCast);
|
|
|
|
if (localResult.IsValid())
|
|
|
|
{
|
|
|
|
if (ret.IsInvalid() || localResult.GetT() < ret.GetT())
|
|
|
|
{
|
|
|
|
ret = localResult;
|
|
|
|
mag = localResult.GetT();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2016-04-27 00:26:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const CCollisionPrimitive::Type& CCollidableOBBTreeGroup::GetType()
|
|
|
|
{
|
|
|
|
return sType;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCollidableOBBTreeGroup::SetStaticTableIndex(u32 index)
|
|
|
|
{
|
|
|
|
sTableIndex = index;
|
2016-04-20 21:44:18 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 03:34:19 +00:00
|
|
|
bool CCollidableOBBTreeGroup::SphereCollide(const CInternalCollisionStructure& collision, CCollisionInfoList& list)
|
2017-01-04 04:08:30 +00:00
|
|
|
{
|
2017-07-03 03:34:19 +00:00
|
|
|
bool ret = false;
|
|
|
|
const CCollidableSphere& p0 = static_cast<const CCollidableSphere&>(collision.GetLeft().GetPrim());
|
|
|
|
const CCollidableOBBTreeGroup& p1 = static_cast<const CCollidableOBBTreeGroup&>(collision.GetRight().GetPrim());
|
|
|
|
|
|
|
|
zeus::CSphere s0 = p0.Transform(collision.GetLeft().GetTransform());
|
|
|
|
zeus::COBBox obb1 = zeus::COBBox::FromAABox(p0.CalculateLocalAABox(),
|
|
|
|
collision.GetRight().GetTransform().inverse() * collision.GetLeft().GetTransform());
|
|
|
|
|
|
|
|
for (const std::unique_ptr<COBBTree>& tree : p1.x10_container->x0_trees)
|
|
|
|
{
|
|
|
|
CCollidableOBBTree obbTree(tree.get(), p1.GetMaterial());
|
|
|
|
if (obbTree.SphereCollision(obbTree.x10_tree->GetRoot(), collision.GetRight().GetTransform(),
|
|
|
|
s0, obb1, p0.GetMaterial(), collision.GetLeft().GetFilter(), list))
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2017-01-04 04:08:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 03:34:19 +00:00
|
|
|
bool CCollidableOBBTreeGroup::SphereCollideBoolean(const CInternalCollisionStructure& collision)
|
2017-01-04 04:08:30 +00:00
|
|
|
{
|
2017-07-03 03:34:19 +00:00
|
|
|
const CCollidableSphere& p0 = static_cast<const CCollidableSphere&>(collision.GetLeft().GetPrim());
|
|
|
|
const CCollidableOBBTreeGroup& p1 = static_cast<const CCollidableOBBTreeGroup&>(collision.GetRight().GetPrim());
|
|
|
|
|
|
|
|
zeus::CSphere s0 = p0.Transform(collision.GetLeft().GetTransform());
|
|
|
|
zeus::COBBox obb1 = zeus::COBBox::FromAABox(p0.CalculateLocalAABox(),
|
|
|
|
collision.GetRight().GetTransform().inverse() * collision.GetLeft().GetTransform());
|
|
|
|
|
|
|
|
for (const std::unique_ptr<COBBTree>& tree : p1.x10_container->x0_trees)
|
|
|
|
{
|
|
|
|
CCollidableOBBTree obbTree(tree.get(), p1.GetMaterial());
|
|
|
|
if (obbTree.SphereCollisionBoolean(obbTree.x10_tree->GetRoot(), collision.GetRight().GetTransform(),
|
|
|
|
s0, obb1, collision.GetLeft().GetFilter()))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:08:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-03 03:34:19 +00:00
|
|
|
bool CCollidableOBBTreeGroup::CollideMovingSphere(const CInternalCollisionStructure& collision, const zeus::CVector3f& dir,
|
|
|
|
double& mag, CCollisionInfo& info)
|
2017-01-04 04:08:30 +00:00
|
|
|
{
|
2017-07-03 03:34:19 +00:00
|
|
|
bool ret = false;
|
|
|
|
const CCollidableSphere& p0 = static_cast<const CCollidableSphere&>(collision.GetLeft().GetPrim());
|
|
|
|
const CCollidableOBBTreeGroup& p1 = static_cast<const CCollidableOBBTreeGroup&>(collision.GetRight().GetPrim());
|
|
|
|
|
|
|
|
zeus::CSphere s0 = p0.Transform(collision.GetLeft().GetTransform());
|
|
|
|
|
|
|
|
zeus::CAABox movedAABB = p0.CalculateLocalAABox();
|
|
|
|
zeus::CVector3f moveVec = float(mag) * dir;
|
|
|
|
movedAABB.accumulateBounds(movedAABB.min + moveVec);
|
|
|
|
movedAABB.accumulateBounds(movedAABB.max + moveVec);
|
|
|
|
|
|
|
|
zeus::COBBox p0Obb =
|
|
|
|
zeus::COBBox::FromAABox(movedAABB,
|
|
|
|
collision.GetRight().GetTransform().inverse() * collision.GetLeft().GetTransform());
|
|
|
|
|
|
|
|
for (const std::unique_ptr<COBBTree>& tree : p1.x10_container->x0_trees)
|
|
|
|
{
|
|
|
|
CCollidableOBBTree obbTree(tree.get(), p1.GetMaterial());
|
|
|
|
CMetroidAreaCollider::ResetInternalCounters();
|
|
|
|
if (obbTree.SphereCollisionMoving(obbTree.x10_tree->GetRoot(), collision.GetRight().GetTransform(),
|
|
|
|
s0, p0Obb, p0.GetMaterial(), collision.GetLeft().GetFilter(),
|
|
|
|
dir, mag, info))
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2017-01-04 04:08:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 03:34:19 +00:00
|
|
|
bool CCollidableOBBTreeGroup::AABoxCollide(const CInternalCollisionStructure& collision, CCollisionInfoList& list)
|
2017-01-04 04:08:30 +00:00
|
|
|
{
|
2017-07-03 03:34:19 +00:00
|
|
|
bool ret = false;
|
|
|
|
const CCollidableAABox& p0 = static_cast<const CCollidableAABox&>(collision.GetLeft().GetPrim());
|
|
|
|
const CCollidableOBBTreeGroup& p1 = static_cast<const CCollidableOBBTreeGroup&>(collision.GetRight().GetPrim());
|
|
|
|
|
|
|
|
zeus::CAABox b0 = p0.CalculateAABox(collision.GetLeft().GetTransform());
|
|
|
|
zeus::COBBox p0Obb =
|
|
|
|
zeus::COBBox::FromAABox(p0.CalculateLocalAABox(),
|
|
|
|
collision.GetRight().GetTransform().inverse() * collision.GetLeft().GetTransform());
|
|
|
|
|
|
|
|
zeus::CPlane planes[] =
|
|
|
|
{
|
|
|
|
{zeus::CVector3f::skRight, b0.min.dot(zeus::CVector3f::skRight)},
|
|
|
|
{zeus::CVector3f::skLeft, b0.max.dot(zeus::CVector3f::skLeft)},
|
|
|
|
{zeus::CVector3f::skForward, b0.min.dot(zeus::CVector3f::skForward)},
|
|
|
|
{zeus::CVector3f::skBack, b0.max.dot(zeus::CVector3f::skBack)},
|
|
|
|
{zeus::CVector3f::skUp, b0.min.dot(zeus::CVector3f::skUp)},
|
|
|
|
{zeus::CVector3f::skDown, b0.max.dot(zeus::CVector3f::skDown)}
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const std::unique_ptr<COBBTree>& tree : p1.x10_container->x0_trees)
|
|
|
|
{
|
|
|
|
CCollidableOBBTree obbTree(tree.get(), p1.GetMaterial());
|
|
|
|
if (obbTree.AABoxCollision(obbTree.x10_tree->GetRoot(), collision.GetRight().GetTransform(),
|
|
|
|
b0, p0Obb, p0.GetMaterial(), collision.GetLeft().GetFilter(),
|
|
|
|
planes, list))
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2017-01-04 04:08:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 03:34:19 +00:00
|
|
|
bool CCollidableOBBTreeGroup::AABoxCollideBoolean(const CInternalCollisionStructure& collision)
|
2017-01-04 04:08:30 +00:00
|
|
|
{
|
2017-07-03 03:34:19 +00:00
|
|
|
const CCollidableAABox& p0 = static_cast<const CCollidableAABox&>(collision.GetLeft().GetPrim());
|
|
|
|
const CCollidableOBBTreeGroup& p1 = static_cast<const CCollidableOBBTreeGroup&>(collision.GetRight().GetPrim());
|
|
|
|
|
|
|
|
zeus::CAABox b0 = p0.CalculateAABox(collision.GetLeft().GetTransform());
|
|
|
|
zeus::COBBox p0Obb =
|
|
|
|
zeus::COBBox::FromAABox(p0.CalculateLocalAABox(),
|
|
|
|
collision.GetRight().GetTransform().inverse() * collision.GetLeft().GetTransform());
|
|
|
|
|
|
|
|
for (const std::unique_ptr<COBBTree>& tree : p1.x10_container->x0_trees)
|
|
|
|
{
|
|
|
|
CCollidableOBBTree obbTree(tree.get(), p1.GetMaterial());
|
|
|
|
if (obbTree.AABoxCollisionBoolean(obbTree.x10_tree->GetRoot(), collision.GetRight().GetTransform(),
|
|
|
|
b0, p0Obb, collision.GetLeft().GetFilter()))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:08:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-03 03:34:19 +00:00
|
|
|
bool CCollidableOBBTreeGroup::CollideMovingAABox(const CInternalCollisionStructure& collision, const zeus::CVector3f& dir,
|
|
|
|
double& mag, CCollisionInfo& info)
|
2017-01-04 04:08:30 +00:00
|
|
|
{
|
2017-07-03 03:34:19 +00:00
|
|
|
bool ret = false;
|
|
|
|
const CCollidableAABox& p0 = static_cast<const CCollidableAABox&>(collision.GetLeft().GetPrim());
|
|
|
|
const CCollidableOBBTreeGroup& p1 = static_cast<const CCollidableOBBTreeGroup&>(collision.GetRight().GetPrim());
|
|
|
|
|
|
|
|
zeus::CAABox b0 = p0.CalculateAABox(collision.GetLeft().GetTransform());
|
|
|
|
|
|
|
|
CMovingAABoxComponents components(b0, dir);
|
|
|
|
|
|
|
|
zeus::CAABox movedAABB = p0.CalculateLocalAABox();
|
|
|
|
zeus::CVector3f moveVec = float(mag) * dir;
|
|
|
|
movedAABB.accumulateBounds(movedAABB.min + moveVec);
|
|
|
|
movedAABB.accumulateBounds(movedAABB.max + moveVec);
|
|
|
|
|
|
|
|
zeus::COBBox p0Obb =
|
|
|
|
zeus::COBBox::FromAABox(movedAABB,
|
|
|
|
collision.GetRight().GetTransform().inverse() * collision.GetLeft().GetTransform());
|
|
|
|
|
|
|
|
for (const std::unique_ptr<COBBTree>& tree : p1.x10_container->x0_trees)
|
|
|
|
{
|
|
|
|
CCollidableOBBTree obbTree(tree.get(), p1.GetMaterial());
|
|
|
|
CMetroidAreaCollider::ResetInternalCounters();
|
|
|
|
if (obbTree.AABoxCollisionMoving(obbTree.x10_tree->GetRoot(), collision.GetRight().GetTransform(),
|
|
|
|
b0, p0Obb, p0.GetMaterial(), collision.GetLeft().GetFilter(),
|
|
|
|
components, dir, mag, info))
|
|
|
|
ret = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2017-01-04 04:08:30 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 19:32:57 +00:00
|
|
|
CFactoryFnReturn FCollidableOBBTreeGroupFactory(const SObjectTag &tag, CInputStream& in,
|
|
|
|
const CVParamTransfer& vparms,
|
|
|
|
CObjectReference* selfRef)
|
2016-04-20 21:44:18 +00:00
|
|
|
{
|
2017-03-03 22:13:23 +00:00
|
|
|
return TToken<CCollidableOBBTreeGroupContainer>::GetIObjObjectFor(std::make_unique<CCollidableOBBTreeGroupContainer>(in));
|
2016-04-20 21:44:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|