metaforce/Runtime/Collision/CCollidableSphere.cpp

285 lines
10 KiB
C++
Raw Normal View History

#include "Runtime/Collision/CCollidableSphere.hpp"
#include "Runtime/Collision/CCollidableAABox.hpp"
#include "Runtime/Collision/CCollisionInfoList.hpp"
#include "Runtime/Collision/CInternalRayCastStructure.hpp"
#include "Runtime/Collision/CollisionUtil.hpp"
2016-06-24 20:09:38 +00:00
2018-12-08 05:30:43 +00:00
namespace urde {
constexpr CCollisionPrimitive::Type sType(CCollidableSphere::SetStaticTableIndex, "CCollidableSphere");
2016-06-24 20:09:38 +00:00
2018-12-08 05:30:43 +00:00
namespace Collide {
2017-07-01 03:23:49 +00:00
2018-12-08 05:30:43 +00:00
bool Sphere_AABox(const CInternalCollisionStructure& collision, CCollisionInfoList& list) {
const CCollidableSphere& p0 = static_cast<const CCollidableSphere&>(collision.GetLeft().GetPrim());
const CCollidableAABox& p1 = static_cast<const CCollidableAABox&>(collision.GetRight().GetPrim());
2017-07-01 03:23:49 +00:00
2018-12-08 05:30:43 +00:00
zeus::CSphere s0 = p0.Transform(collision.GetLeft().GetTransform());
zeus::CAABox b1 = p1.Transform(collision.GetRight().GetTransform());
2017-07-01 03:23:49 +00:00
2018-12-08 05:30:43 +00:00
float distSq = 0.f;
int flags = 0;
for (int i = 0; i < 3; ++i) {
if (s0.position[i] < b1.min[i]) {
if (s0.position[i] + s0.radius >= b1.min[i]) {
float dist = s0.position[i] - b1.min[i];
distSq += dist * dist;
flags |= 1 << (2 * i);
} else {
return false;
}
} else if (s0.position[i] > b1.max[i]) {
if (s0.position[i] - s0.radius <= b1.max[i]) {
float dist = s0.position[i] - b1.max[i];
distSq += dist * dist;
flags |= 1 << (2 * i + 1);
} else {
return false;
}
2017-07-01 03:23:49 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-07-01 03:23:49 +00:00
2018-12-08 05:30:43 +00:00
if (!flags) {
zeus::CVector3f normal = (s0.position - b1.center()).normalized();
zeus::CVector3f point = s0.position + normal * s0.radius;
CCollisionInfo info(point, p0.GetMaterial(), p1.GetMaterial(), normal);
2017-07-01 03:23:49 +00:00
list.Add(info, false);
return true;
2018-12-08 05:30:43 +00:00
}
if (distSq > s0.radius * s0.radius)
return false;
zeus::CVector3f point;
switch (flags) {
case 0x1a:
point = zeus::CVector3f(b1.max.x(), b1.max.y(), b1.min.z());
break;
case 0x19:
point = zeus::CVector3f(b1.min.x(), b1.max.y(), b1.min.z());
break;
case 0x16:
point = zeus::CVector3f(b1.max.x(), b1.min.y(), b1.min.z());
break;
case 0x15:
point = zeus::CVector3f(b1.min.x(), b1.min.y(), b1.min.z());
break;
case 0x2a:
point = zeus::CVector3f(b1.max.x(), b1.max.y(), b1.max.z());
break;
case 0x29:
point = zeus::CVector3f(b1.min.x(), b1.max.y(), b1.max.z());
break;
case 0x26:
point = zeus::CVector3f(b1.max.x(), b1.min.y(), b1.max.z());
break;
case 0x25:
point = zeus::CVector3f(b1.min.x(), b1.min.y(), b1.max.z());
break;
case 0x11:
point = zeus::CVector3f(b1.min.x(), s0.position.y(), b1.min.z());
break;
case 0x12:
point = zeus::CVector3f(b1.max.x(), s0.position.y(), b1.min.z());
break;
case 0x14:
point = zeus::CVector3f(s0.position.x(), b1.min.y(), b1.min.z());
break;
case 0x18:
point = zeus::CVector3f(s0.position.x(), b1.max.y(), b1.min.z());
break;
case 0x5:
point = zeus::CVector3f(b1.min.x(), b1.min.y(), s0.position.z());
break;
case 0x6:
point = zeus::CVector3f(b1.max.x(), b1.min.y(), s0.position.z());
break;
case 0x9:
point = zeus::CVector3f(b1.min.x(), b1.max.y(), s0.position.z());
break;
case 0xa:
point = zeus::CVector3f(b1.max.x(), b1.max.y(), s0.position.z());
break;
case 0x21:
point = zeus::CVector3f(b1.min.x(), s0.position.y(), b1.max.z());
break;
case 0x22:
point = zeus::CVector3f(b1.max.x(), s0.position.y(), b1.max.z());
break;
case 0x24:
point = zeus::CVector3f(s0.position.x(), b1.min.y(), b1.max.z());
break;
case 0x28:
point = zeus::CVector3f(s0.position.x(), b1.max.y(), b1.max.z());
break;
case 0x1:
point = zeus::CVector3f(b1.min.x(), s0.position.y(), s0.position.z());
break;
case 0x2:
point = zeus::CVector3f(b1.max.x(), s0.position.y(), s0.position.z());
break;
case 0x4:
point = zeus::CVector3f(s0.position.x(), b1.min.y(), s0.position.z());
break;
case 0x8:
point = zeus::CVector3f(s0.position.x(), b1.max.y(), s0.position.z());
break;
case 0x10:
point = zeus::CVector3f(s0.position.x(), s0.position.y(), b1.min.z());
break;
case 0x20:
point = zeus::CVector3f(s0.position.x(), s0.position.y(), b1.max.z());
break;
default:
break;
}
CCollisionInfo info(point, p0.GetMaterial(), p1.GetMaterial(), (s0.position - point).normalized());
list.Add(info, false);
return true;
2017-07-01 03:23:49 +00:00
}
2018-12-08 05:30:43 +00:00
bool Sphere_AABox_Bool(const CInternalCollisionStructure& collision) {
const CCollidableSphere& p0 = static_cast<const CCollidableSphere&>(collision.GetLeft().GetPrim());
const CCollidableAABox& p1 = static_cast<const CCollidableAABox&>(collision.GetRight().GetPrim());
2017-07-01 03:23:49 +00:00
2018-12-08 05:30:43 +00:00
zeus::CSphere s0 = p0.Transform(collision.GetLeft().GetTransform());
zeus::CAABox b1 = p1.Transform(collision.GetRight().GetTransform());
2016-06-24 20:09:38 +00:00
2018-12-08 05:30:43 +00:00
return CCollidableSphere::Sphere_AABox_Bool(s0, b1);
2017-07-01 03:23:49 +00:00
}
2018-12-08 05:30:43 +00:00
bool Sphere_Sphere(const CInternalCollisionStructure& collision, CCollisionInfoList& list) {
const CCollidableSphere& p0 = static_cast<const CCollidableSphere&>(collision.GetLeft().GetPrim());
const CCollidableSphere& p1 = static_cast<const CCollidableSphere&>(collision.GetRight().GetPrim());
zeus::CSphere s0 = p0.Transform(collision.GetLeft().GetTransform());
zeus::CSphere s1 = p1.Transform(collision.GetRight().GetTransform());
float radiusSum = s0.radius + s1.radius;
zeus::CVector3f delta = s0.position - s1.position;
float deltaMagSq = delta.magSquared();
if (deltaMagSq <= radiusSum * radiusSum) {
zeus::CVector3f deltaNorm =
delta.canBeNormalized() ? (1.f / std::sqrt(deltaMagSq)) * delta : zeus::skRight;
2018-12-08 05:30:43 +00:00
zeus::CVector3f collisionPoint = deltaNorm * s1.radius + s1.position;
CCollisionInfo info(collisionPoint, p0.GetMaterial(), p1.GetMaterial(), deltaNorm);
list.Add(info, false);
2017-07-01 03:23:49 +00:00
2018-12-08 05:30:43 +00:00
return true;
}
return false;
2017-03-29 02:51:12 +00:00
}
2016-06-24 20:09:38 +00:00
2018-12-08 05:30:43 +00:00
bool Sphere_Sphere_Bool(const CInternalCollisionStructure& collision) {
const CCollidableSphere& p0 = static_cast<const CCollidableSphere&>(collision.GetLeft().GetPrim());
const CCollidableSphere& p1 = static_cast<const CCollidableSphere&>(collision.GetRight().GetPrim());
2017-07-01 03:23:49 +00:00
2018-12-08 05:30:43 +00:00
zeus::CSphere s0 = p0.Transform(collision.GetLeft().GetTransform());
zeus::CSphere s1 = p1.Transform(collision.GetRight().GetTransform());
2017-07-01 03:23:49 +00:00
2018-12-08 05:30:43 +00:00
float radiusSum = s0.radius + s1.radius;
return (s0.position - s1.position).magSquared() <= radiusSum * radiusSum;
2017-07-01 03:23:49 +00:00
}
2016-06-24 20:09:38 +00:00
2018-12-08 05:30:43 +00:00
} // namespace Collide
2016-06-24 20:09:38 +00:00
2017-03-01 03:42:06 +00:00
CCollidableSphere::CCollidableSphere(const zeus::CSphere& sphere, const CMaterialList& list)
2018-12-08 05:30:43 +00:00
: CCollisionPrimitive(list), x10_sphere(sphere) {}
2016-06-24 21:30:25 +00:00
2018-12-08 05:30:43 +00:00
zeus::CSphere CCollidableSphere::Transform(const zeus::CTransform& xf) const {
return zeus::CSphere(xf * x10_sphere.position, x10_sphere.radius);
}
2017-03-01 03:42:06 +00:00
u32 CCollidableSphere::GetTableIndex() const { return sTableIndex; }
2018-12-08 05:30:43 +00:00
zeus::CAABox CCollidableSphere::CalculateAABox(const zeus::CTransform& xf) const {
zeus::CVector3f xfPos = xf * x10_sphere.position;
return {xfPos - x10_sphere.radius, xfPos + x10_sphere.radius};
}
2018-12-08 05:30:43 +00:00
zeus::CAABox CCollidableSphere::CalculateLocalAABox() const {
return {x10_sphere.position - x10_sphere.radius, x10_sphere.position + x10_sphere.radius};
}
2017-03-01 03:42:06 +00:00
FourCC CCollidableSphere::GetPrimType() const { return SBIG('SPHR'); }
2018-12-08 05:30:43 +00:00
CRayCastResult CCollidableSphere::CastRayInternal(const CInternalRayCastStructure& rayCast) const {
if (!rayCast.GetFilter().Passes(GetMaterial()))
return {};
2018-12-08 05:30:43 +00:00
zeus::CSphere xfSphere = Transform(rayCast.GetTransform());
float t = 0.f;
zeus::CVector3f point;
if (CollisionUtil::RaySphereIntersection(xfSphere, rayCast.GetRay().start, rayCast.GetRay().dir, rayCast.GetMaxTime(),
t, point)) {
zeus::CVector3f delta = point - xfSphere.position;
float deltaMag = delta.magnitude();
zeus::CUnitVector3f planeNormal = (deltaMag > 0.01f) ? delta * (1.f / deltaMag) : rayCast.GetRay().dir;
float planeD = point.dot(planeNormal);
return CRayCastResult(t, point, zeus::CPlane(planeNormal, planeD), GetMaterial());
}
return {};
}
2017-03-01 03:42:06 +00:00
const CCollisionPrimitive::Type& CCollidableSphere::GetType() { return sType; }
2017-07-01 03:23:49 +00:00
bool CCollidableSphere::CollideMovingAABox(const CInternalCollisionStructure& collision, const zeus::CVector3f& dir,
2018-12-08 05:30:43 +00:00
double& dOut, CCollisionInfo& infoOut) {
const CCollidableSphere& p0 = static_cast<const CCollidableSphere&>(collision.GetLeft().GetPrim());
const CCollidableAABox& p1 = static_cast<const CCollidableAABox&>(collision.GetRight().GetPrim());
zeus::CSphere s0 = p0.Transform(collision.GetLeft().GetTransform());
zeus::CAABox b1 = p1.CalculateAABox(collision.GetRight().GetTransform());
double d = dOut;
zeus::CVector3f point, normal;
if (CollisionUtil::MovingSphereAABox(s0, b1, dir, d, point, normal) && d < dOut) {
dOut = d;
infoOut = CCollisionInfo(point, p0.GetMaterial(), p1.GetMaterial(), normal);
return true;
}
2017-07-01 03:23:49 +00:00
2018-12-08 05:30:43 +00:00
return false;
}
2017-07-01 03:23:49 +00:00
bool CCollidableSphere::CollideMovingSphere(const CInternalCollisionStructure& collision, const zeus::CVector3f& dir,
2018-12-08 05:30:43 +00:00
double& dOut, CCollisionInfo& infoOut) {
const CCollidableSphere& p0 = static_cast<const CCollidableSphere&>(collision.GetLeft().GetPrim());
const CCollidableSphere& p1 = static_cast<const CCollidableSphere&>(collision.GetRight().GetPrim());
zeus::CSphere s0 = p0.Transform(collision.GetLeft().GetTransform());
zeus::CSphere s1 = p1.Transform(collision.GetRight().GetTransform());
double d = dOut;
if (CollisionUtil::RaySphereIntersection_Double(zeus::CSphere(s1.position, s0.radius + s1.radius), s0.position, dir,
d) &&
d >= 0.0 && d < dOut) {
dOut = d;
zeus::CVector3f normal = (s0.position + float(d) * dir - s1.position).normalized();
infoOut = CCollisionInfo(s1.position + s1.radius * normal, p0.GetMaterial(), p1.GetMaterial(), normal);
return true;
}
2017-07-01 03:23:49 +00:00
2018-12-08 05:30:43 +00:00
return false;
}
2017-03-29 02:51:12 +00:00
2018-12-08 05:30:43 +00:00
bool CCollidableSphere::Sphere_AABox_Bool(const zeus::CSphere& sphere, const zeus::CAABox& aabb) {
float mag = 0.f;
for (int i = 0; i < 3; ++i) {
if (sphere.position[i] < aabb.min[i]) {
float tmp = sphere.position[i] - aabb.min[i];
mag += tmp * tmp;
} else if (sphere.position[i] > aabb.max[i]) {
float tmp = sphere.position[i] - aabb.max[i];
mag += tmp * tmp;
2017-03-29 02:51:12 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-03-29 02:51:12 +00:00
2018-12-08 05:30:43 +00:00
return mag <= sphere.radius * sphere.radius;
2016-06-24 20:09:38 +00:00
}
2018-12-08 05:30:43 +00:00
} // namespace urde