metaforce/Runtime/Collision/CGameCollision.cpp

217 lines
8.4 KiB
C++
Raw Normal View History

2016-06-24 20:09:38 +00:00
#include "CGameCollision.hpp"
#include "CCollidableOBBTreeGroup.hpp"
#include "CMaterialFilter.hpp"
#include "CMaterialList.hpp"
#include "World/CActor.hpp"
#include "CStateManager.hpp"
#include "TCastTo.hpp"
#include "World/CWorld.hpp"
2016-06-24 20:09:38 +00:00
namespace urde
{
float CollisionImpulseFiniteVsInfinite(float f1, float f2, float f3) { return f1 * ((1.f / f3) * f2); }
float CollisionImpulseFiniteVsFinite(float f1, float f2, float f3, float f4)
{
return (-(1.f + f4) * f3) / ((1.f / f1) + (1.f / f2));
}
2016-06-24 20:09:38 +00:00
void CGameCollision::InitCollision()
{
/* Types */
2016-06-24 20:09:38 +00:00
CCollisionPrimitive::InitBeginTypes();
CCollisionPrimitive::InitAddType(CCollidableOBBTreeGroup::GetType());
CCollisionPrimitive::InitEndTypes();
/* Colliders */
CCollisionPrimitive::InitBeginColliders();
CCollisionPrimitive::InitAddCollider(CCollidableOBBTreeGroup::SphereCollide, "CCollidableSphere",
"CCollidableOBBTreeGroup");
CCollisionPrimitive::InitAddCollider(CCollidableOBBTreeGroup::AABoxCollide, "CCollidableAABox",
"CCollidableOBBTreeGroup");
CCollisionPrimitive::InitAddBooleanCollider(CCollidableOBBTreeGroup::SphereCollideBoolean, "CCollidableSphere",
"CCollidableOBBTreeGroup");
CCollisionPrimitive::InitAddBooleanCollider(CCollidableOBBTreeGroup::AABoxCollideBoolean, "CCollidableAABox",
"CCollidableOBBTreeGroup");
CCollisionPrimitive::InitAddMovingCollider(CCollidableOBBTreeGroup::CollideMovingAABox, "CCollidableAABox",
"CCollidableOBBTreeGroup");
CCollisionPrimitive::InitAddMovingCollider(CCollidableOBBTreeGroup::CollideMovingSphere, "CCollidableSphere",
"CCollidableOBBTreeGroup");
CCollisionPrimitive::InitAddCollider(CGameCollision::NullCollisionCollider, "CCollidableOBBTreeGroup",
"CCollidableOBBTreeGroup");
CCollisionPrimitive::InitAddBooleanCollider(CGameCollision::NullBooleanCollider, "CCollidableOBBTreeGroup",
"CCollidableOBBTreeGroup");
CCollisionPrimitive::InitAddMovingCollider(CGameCollision::NullMovingCollider, "CCollidableOBBTreeGroup",
"CCollidableOBBTreeGroup");
CCollisionPrimitive::InitEndColliders();
}
2017-03-26 05:53:04 +00:00
void CGameCollision::Move(CStateManager& mgr, CPhysicsActor& actor, float dt, const rstl::reserved_vector<TUniqueId, 1024>*)
2017-03-24 05:30:16 +00:00
{
}
bool CGameCollision::CanBlock(const CMaterialList& mat, const zeus::CVector3f& v)
{
if ((mat.HasMaterial(EMaterialTypes::Character) && !mat.HasMaterial(EMaterialTypes::Scannable)) ||
mat.HasMaterial(EMaterialTypes::Occluder))
return false;
if (mat.HasMaterial(EMaterialTypes::Occluder) || mat.HasMaterial(EMaterialTypes::Floor))
return true;
return (v.z > 0.85f);
2016-06-24 20:09:38 +00:00
}
bool CGameCollision::IsFloor(const CMaterialList& mat, const zeus::CVector3f& v)
{
if (mat.HasMaterial(EMaterialTypes::Floor))
return true;
return (v.z > 0.85f);
}
void CGameCollision::SendMaterialMessage(CStateManager& mgr, const CMaterialList& mat, CActor& act)
{
EScriptObjectMessage msg;
if (mat.HasMaterial(EMaterialTypes::Ice))
msg = EScriptObjectMessage::InternalMessage05;
else if (mat.HasMaterial(EMaterialTypes::MudSlow))
msg = EScriptObjectMessage::InternalMessage06;
else
msg = EScriptObjectMessage::InternalMessage07;
mgr.SendScriptMsg(&act, kInvalidUniqueId, msg);
}
CRayCastResult
CGameCollision::RayStaticIntersection(const CStateManager& mgr, const zeus::CVector3f& pos,
const zeus::CVector3f& dir, float length, const CMaterialFilter& filter)
{
CRayCastResult ret;
float bestT = length;
if (bestT <= 0.f)
bestT = 100000.f;
zeus::CLine line(pos, dir);
for (const CGameArea& area : *mgr.GetWorld())
{
CAreaOctTree::SRayResult rayRes;
CAreaOctTree& collision = *area.GetPostConstructed()->x0_collision;
collision.GetRootNode().LineTestEx(line, filter, rayRes, length);
if (!rayRes.x10_surface || (length != 0.f && length < rayRes.x3c_t))
continue;
if (rayRes.x3c_t < bestT)
{
ret = CRayCastResult(rayRes.x3c_t, dir * rayRes.x3c_t + pos,
rayRes.x0_plane, rayRes.x10_surface->GetSurfaceFlags());
bestT = rayRes.x3c_t;
}
}
return ret;
}
bool CGameCollision::RayStaticIntersectionBool(const CStateManager& mgr, const zeus::CVector3f& start,
const zeus::CVector3f& dir, float length,
const CMaterialFilter& filter)
{
if (length <= 0.f)
length = 100000.f;
zeus::CLine line(start, dir);
for (const CGameArea& area : *mgr.GetWorld())
{
const CAreaOctTree& collision = *area.GetPostConstructed()->x0_collision;
CAreaOctTree::Node root = collision.GetRootNode();
if (!root.LineTest(line, filter, length))
return false;
}
return true;
}
CRayCastResult
CGameCollision::RayDynamicIntersection(const CStateManager& mgr, TUniqueId& idOut, const zeus::CVector3f& pos,
const zeus::CVector3f& dir, float length, const CMaterialFilter& filter,
const rstl::reserved_vector<TUniqueId, 1024>& nearList)
{
CRayCastResult ret;
float bestT = length;
if (bestT <= 0.f)
bestT = 100000.f;
for (TUniqueId id : nearList)
{
CEntity* ent = const_cast<CEntity*>(mgr.GetObjectById(id));
if (TCastToPtr<CPhysicsActor> physActor = ent)
{
zeus::CTransform xf = physActor->GetPrimitiveTransform();
const CCollisionPrimitive* prim = physActor->GetCollisionPrimitive();
CRayCastResult res = prim->CastRay(pos, dir, bestT, filter, xf);
if (!res.IsInvalid() && res.GetT() < bestT)
{
bestT = res.GetT();
ret = res;
idOut = physActor->GetUniqueId();
}
}
}
return ret;
}
bool CGameCollision::RayDynamicIntersectionBool(const CStateManager& mgr,
const zeus::CVector3f& pos, const zeus::CVector3f& dir,
const CMaterialFilter& filter,
const rstl::reserved_vector<TUniqueId, 1024>& nearList,
const CActor* damagee, float length)
{
if (length <= 0.f)
length = 100000.f;
for (TUniqueId id : nearList)
{
const CEntity* ent = mgr.GetObjectById(id);
if (TCastToConstPtr<CPhysicsActor> physActor = ent)
{
if (damagee && physActor->GetUniqueId() == damagee->GetUniqueId())
continue;
zeus::CTransform xf = physActor->GetPrimitiveTransform();
const CCollisionPrimitive* prim = physActor->GetCollisionPrimitive();
CRayCastResult res = prim->CastRay(pos, dir, length, filter, xf);
if (!res.IsInvalid())
return false;
}
}
return true;
}
CRayCastResult
CGameCollision::RayWorldIntersection(const CStateManager& mgr, TUniqueId& idOut, const zeus::CVector3f& pos,
const zeus::CVector3f& dir, float mag, const CMaterialFilter& filter,
const rstl::reserved_vector<TUniqueId, 1024>& nearList)
{
CRayCastResult staticRes = RayStaticIntersection(mgr, pos, dir, mag, filter);
CRayCastResult dynamicRes = RayDynamicIntersection(mgr, idOut, pos, dir, mag, filter, nearList);
if (!dynamicRes.IsInvalid() && staticRes.IsInvalid())
return dynamicRes;
else if (staticRes.GetT() >= dynamicRes.GetT())
return dynamicRes;
else
return staticRes;
}
bool CGameCollision::RayStaticIntersectionArea(const CGameArea& area, const zeus::CVector3f& pos,
const zeus::CVector3f& dir, float mag, const CMaterialFilter& filter)
{
if (mag <= 0.f)
mag = 100000.f;
CAreaOctTree::Node node = area.GetPostConstructed()->x0_collision->GetRootNode();
zeus::CLine line(pos, dir);
return node.LineTest(line, filter, mag);
}
2016-06-24 20:09:38 +00:00
}