2020-01-15 12:07:48 +00:00
|
|
|
#include "Runtime/Collision/CCollisionActorManager.hpp"
|
|
|
|
|
|
|
|
#include "Runtime/CStateManager.hpp"
|
|
|
|
#include "Runtime/Collision/CCollisionActor.hpp"
|
|
|
|
#include "Runtime/Collision/CMaterialList.hpp"
|
|
|
|
#include "Runtime/World/CActor.hpp"
|
|
|
|
|
2019-09-21 13:07:13 +00:00
|
|
|
#include "TCastTo.hpp" // Generated file, do not modify include path
|
2017-03-01 03:42:06 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
2017-03-01 03:42:06 +00:00
|
|
|
|
2018-11-10 02:47:07 +00:00
|
|
|
CCollisionActorManager::CCollisionActorManager(CStateManager& mgr, TUniqueId owner, TAreaId area,
|
2018-11-13 09:20:11 +00:00
|
|
|
const std::vector<CJointCollisionDescription>& descs, bool active)
|
2018-12-08 05:30:43 +00:00
|
|
|
: x10_ownerId(owner), x12_active(active) {
|
|
|
|
if (TCastToConstPtr<CActor> act = mgr.GetObjectById(x10_ownerId)) {
|
2020-04-02 06:06:42 +00:00
|
|
|
const zeus::CTransform xf = act->GetTransform();
|
2018-12-08 05:30:43 +00:00
|
|
|
const CAnimData* animData = act->GetModelData()->GetAnimationData();
|
2020-04-02 06:06:42 +00:00
|
|
|
const zeus::CVector3f scale = act->GetModelData()->GetScale();
|
|
|
|
const zeus::CTransform scaleXf = zeus::CTransform::Scale(scale);
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
x0_jointDescriptions.reserve(descs.size());
|
|
|
|
for (const CJointCollisionDescription& desc : descs) {
|
|
|
|
CJointCollisionDescription modDesc = desc;
|
|
|
|
modDesc.ScaleAllBounds(scale);
|
2020-04-02 06:06:42 +00:00
|
|
|
const zeus::CTransform locXf = GetWRLocatorTransform(*animData, modDesc.GetPivotId(), xf, scaleXf);
|
|
|
|
|
2021-05-02 00:28:04 +00:00
|
|
|
if (modDesc.GetNextId().IsInvalid()) {
|
|
|
|
// We only have the pivot id
|
|
|
|
const TUniqueId newId = mgr.AllocateUniqueId();
|
|
|
|
CCollisionActor* newAct;
|
2020-04-02 06:06:42 +00:00
|
|
|
|
2021-05-02 00:28:04 +00:00
|
|
|
if (modDesc.GetType() == CJointCollisionDescription::ECollisionType::Sphere) {
|
|
|
|
newAct = new CCollisionActor(newId, area, x10_ownerId, active, modDesc.GetRadius(), modDesc.GetMass(),
|
|
|
|
desc.GetName());
|
|
|
|
} else if (modDesc.GetType() == CJointCollisionDescription::ECollisionType::OBB) {
|
|
|
|
newAct = new CCollisionActor(newId, area, x10_ownerId, modDesc.GetBounds(), modDesc.GetPivotPoint(), active,
|
|
|
|
modDesc.GetMass(), desc.GetName());
|
|
|
|
} else {
|
|
|
|
newAct = new CCollisionActor(newId, area, x10_ownerId, modDesc.GetBounds(), active, modDesc.GetMass(),
|
|
|
|
desc.GetName());
|
|
|
|
}
|
2020-04-02 06:06:42 +00:00
|
|
|
|
2021-05-02 00:28:04 +00:00
|
|
|
newAct->SetTransform(locXf);
|
|
|
|
mgr.AddObject(newAct);
|
|
|
|
x0_jointDescriptions.push_back(desc);
|
|
|
|
x0_jointDescriptions.back().SetCollisionActorId(newId);
|
|
|
|
} else { // We have another bone in to connect to!
|
|
|
|
const zeus::CTransform locXf2 = GetWRLocatorTransform(*animData, modDesc.GetNextId(), xf, scaleXf);
|
|
|
|
const float dist = (locXf2.origin - locXf.origin).magnitude();
|
2020-04-02 06:06:42 +00:00
|
|
|
|
2021-05-02 00:28:04 +00:00
|
|
|
if (modDesc.GetType() != CJointCollisionDescription::ECollisionType::OBBAutoSize) {
|
2020-04-02 06:06:42 +00:00
|
|
|
const TUniqueId newId = mgr.AllocateUniqueId();
|
|
|
|
auto* newAct = new CCollisionActor(newId, area, x10_ownerId, active, modDesc.GetRadius(), modDesc.GetMass(),
|
|
|
|
desc.GetName());
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
newAct->SetTransform(locXf);
|
|
|
|
mgr.AddObject(newAct);
|
|
|
|
x0_jointDescriptions.push_back(CJointCollisionDescription::SphereCollision(
|
|
|
|
modDesc.GetPivotId(), modDesc.GetRadius(), modDesc.GetName(), 0.001f));
|
|
|
|
x0_jointDescriptions.back().SetCollisionActorId(newId);
|
2020-04-02 06:06:42 +00:00
|
|
|
|
|
|
|
const u32 numSeps = u32(dist / modDesc.GetMaxSeparation());
|
|
|
|
if (numSeps != 0) {
|
2018-12-08 05:30:43 +00:00
|
|
|
x0_jointDescriptions.reserve(x0_jointDescriptions.capacity() + numSeps);
|
2020-04-02 06:06:42 +00:00
|
|
|
const float pitch = dist / float(numSeps + 1);
|
2018-12-08 05:30:43 +00:00
|
|
|
for (u32 i = 0; i < numSeps; ++i) {
|
2020-04-02 06:06:42 +00:00
|
|
|
const float separation = pitch * float(i + 1);
|
2018-12-08 05:30:43 +00:00
|
|
|
x0_jointDescriptions.push_back(CJointCollisionDescription::SphereSubdivideCollision(
|
|
|
|
modDesc.GetPivotId(), modDesc.GetNextId(), modDesc.GetRadius(), separation,
|
|
|
|
CJointCollisionDescription::EOrientationType::One, modDesc.GetName(), 0.001f));
|
2020-04-02 06:06:42 +00:00
|
|
|
|
|
|
|
const TUniqueId newId2 = mgr.AllocateUniqueId();
|
|
|
|
auto* newAct2 = new CCollisionActor(newId2, area, x10_ownerId, active, modDesc.GetRadius(),
|
|
|
|
modDesc.GetMass(), desc.GetName());
|
2018-12-08 05:30:43 +00:00
|
|
|
if (modDesc.GetOrientationType() == CJointCollisionDescription::EOrientationType::Zero) {
|
2021-05-02 00:28:04 +00:00
|
|
|
newAct2->SetTransform(zeus::CTransform::Translate(locXf.origin + (separation * locXf.basis[1])));
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
2020-04-02 06:06:42 +00:00
|
|
|
const zeus::CVector3f delta = (locXf2.origin - locXf.origin).normalized();
|
2018-12-08 05:30:43 +00:00
|
|
|
zeus::CVector3f upVector = locXf.basis[2];
|
2020-04-02 06:06:42 +00:00
|
|
|
|
|
|
|
if (zeus::close_enough(std::fabs(delta.dot(upVector)), 1.f)) {
|
2018-12-08 05:30:43 +00:00
|
|
|
upVector = locXf.basis[1];
|
2020-04-02 06:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const zeus::CTransform lookAt = zeus::lookAt(zeus::skZero3f, delta, upVector);
|
2021-05-02 00:28:04 +00:00
|
|
|
newAct2->SetTransform(zeus::CTransform::Translate(locXf.origin + (separation * lookAt.basis[1])));
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2020-04-02 06:06:42 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
mgr.AddObject(newAct2);
|
|
|
|
x0_jointDescriptions.back().SetCollisionActorId(newId2);
|
2018-11-13 09:20:11 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2020-04-02 06:06:42 +00:00
|
|
|
} else {
|
2021-05-02 00:28:04 +00:00
|
|
|
if (dist <= FLT_EPSILON) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-04-02 06:06:42 +00:00
|
|
|
|
2021-05-02 00:28:04 +00:00
|
|
|
zeus::CVector3f bounds = modDesc.GetBounds();
|
|
|
|
bounds.y() += dist;
|
|
|
|
auto* newAct =
|
|
|
|
new CCollisionActor(mgr.AllocateUniqueId(), area, x10_ownerId, bounds,
|
|
|
|
zeus::CVector3f(0.f, 0.5f * dist, 0.f), active, modDesc.GetMass(), desc.GetName());
|
|
|
|
|
|
|
|
if (modDesc.GetOrientationType() == CJointCollisionDescription::EOrientationType::Zero) {
|
|
|
|
newAct->SetTransform(locXf);
|
|
|
|
} else {
|
|
|
|
const zeus::CVector3f delta = (locXf2.origin - locXf.origin).normalized();
|
|
|
|
zeus::CVector3f upVector = locXf.basis[2];
|
|
|
|
|
|
|
|
if (zeus::close_enough(std::fabs(delta.dot(upVector)), 1.f)) {
|
|
|
|
upVector = locXf.basis[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
newAct->SetTransform(zeus::lookAt(locXf.origin, locXf.origin + delta, upVector));
|
|
|
|
}
|
|
|
|
|
|
|
|
mgr.AddObject(newAct);
|
|
|
|
x0_jointDescriptions.push_back(desc);
|
|
|
|
x0_jointDescriptions.back().SetCollisionActorId(newAct->GetUniqueId());
|
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2018-11-13 09:20:11 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-03-01 03:42:06 +00:00
|
|
|
}
|
2017-03-13 01:07:03 +00:00
|
|
|
|
2020-04-10 00:32:35 +00:00
|
|
|
void CCollisionActorManager::Destroy(CStateManager& mgr) {
|
2020-04-02 06:06:42 +00:00
|
|
|
for (const CJointCollisionDescription& desc : x0_jointDescriptions) {
|
2018-12-08 05:30:43 +00:00
|
|
|
mgr.FreeScriptObject(desc.GetCollisionActorId());
|
2020-04-02 06:06:42 +00:00
|
|
|
}
|
2017-05-07 06:55:01 +00:00
|
|
|
|
2020-04-10 00:32:35 +00:00
|
|
|
x13_destroyed = true;
|
2017-05-07 06:55:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CCollisionActorManager::SetActive(CStateManager& mgr, bool active) {
|
|
|
|
x12_active = active;
|
|
|
|
for (const CJointCollisionDescription& jDesc : x0_jointDescriptions) {
|
|
|
|
if (TCastToPtr<CActor> act = mgr.ObjectById(jDesc.GetCollisionActorId())) {
|
2020-04-02 06:06:42 +00:00
|
|
|
const bool curActive = act->GetActive();
|
|
|
|
if (curActive != active) {
|
2018-12-08 05:30:43 +00:00
|
|
|
act->SetActive(active);
|
2021-05-02 00:28:04 +00:00
|
|
|
if (!active) {
|
|
|
|
Update(0.f, mgr, EUpdateOptions::WorldSpace);
|
|
|
|
}
|
2020-04-02 06:06:42 +00:00
|
|
|
}
|
2018-11-10 02:47:07 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-03-26 04:12:06 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CCollisionActorManager::AddMaterial(CStateManager& mgr, const CMaterialList& list) {
|
2020-04-02 06:06:42 +00:00
|
|
|
for (const CJointCollisionDescription& jDesc : x0_jointDescriptions) {
|
|
|
|
if (TCastToPtr<CActor> act = mgr.ObjectById(jDesc.GetCollisionActorId())) {
|
2018-12-08 05:30:43 +00:00
|
|
|
act->AddMaterial(list);
|
2020-04-02 06:06:42 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-26 04:12:06 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CCollisionActorManager::SetMovable(CStateManager& mgr, bool movable) {
|
2020-04-02 06:06:42 +00:00
|
|
|
if (x14_movable == movable) {
|
2018-12-08 05:30:43 +00:00
|
|
|
return;
|
2020-04-02 06:06:42 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
x14_movable = movable;
|
2020-04-02 06:06:42 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
for (const CJointCollisionDescription& jDesc : x0_jointDescriptions) {
|
|
|
|
if (TCastToPtr<CCollisionActor> act = mgr.ObjectById(jDesc.GetCollisionActorId())) {
|
|
|
|
act->SetMovable(x14_movable);
|
|
|
|
act->SetUseInSortedLists(x14_movable);
|
2018-11-13 09:20:11 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-03-13 01:07:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CCollisionActorManager::Update(float dt, CStateManager& mgr, EUpdateOptions opts) {
|
2020-04-02 06:06:42 +00:00
|
|
|
if (!x14_movable) {
|
2018-12-08 05:30:43 +00:00
|
|
|
SetMovable(mgr, true);
|
2020-04-02 06:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!x12_active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const TCastToConstPtr<CActor> act = mgr.ObjectById(x10_ownerId)) {
|
|
|
|
const CAnimData& animData = *act->GetModelData()->GetAnimationData();
|
|
|
|
const zeus::CTransform actXf = act->GetTransform();
|
|
|
|
const zeus::CTransform scaleXf = zeus::CTransform::Scale(act->GetModelData()->GetScale());
|
|
|
|
|
|
|
|
for (const CJointCollisionDescription& jDesc : x0_jointDescriptions) {
|
|
|
|
if (TCastToPtr<CCollisionActor> cAct = mgr.ObjectById(jDesc.GetCollisionActorId())) {
|
|
|
|
const zeus::CTransform pivotXf = GetWRLocatorTransform(animData, jDesc.GetPivotId(), actXf, scaleXf);
|
|
|
|
zeus::CVector3f origin = pivotXf.origin;
|
|
|
|
|
|
|
|
if (jDesc.GetType() == CJointCollisionDescription::ECollisionType::OBB ||
|
|
|
|
jDesc.GetType() == CJointCollisionDescription::ECollisionType::OBBAutoSize) {
|
|
|
|
if (jDesc.GetOrientationType() == CJointCollisionDescription::EOrientationType::Zero) {
|
|
|
|
cAct->SetTransform(zeus::CQuaternion(pivotXf.basis).toTransform(cAct->GetTranslation()));
|
|
|
|
} else {
|
|
|
|
const zeus::CTransform nextXf = GetWRLocatorTransform(animData, jDesc.GetNextId(), actXf, scaleXf);
|
|
|
|
cAct->SetTransform(zeus::CQuaternion(zeus::lookAt(pivotXf.origin, nextXf.origin, pivotXf.basis[2]).basis)
|
|
|
|
.toTransform(cAct->GetTranslation()));
|
|
|
|
}
|
|
|
|
} else if (jDesc.GetType() == CJointCollisionDescription::ECollisionType::SphereSubdivide) {
|
|
|
|
if (jDesc.GetOrientationType() == CJointCollisionDescription::EOrientationType::Zero) {
|
|
|
|
origin += jDesc.GetMaxSeparation() * pivotXf.basis[1];
|
|
|
|
} else {
|
|
|
|
const zeus::CTransform nextXf = GetWRLocatorTransform(animData, jDesc.GetNextId(), actXf, scaleXf);
|
|
|
|
origin += zeus::lookAt(origin, nextXf.origin, pivotXf.basis[2]).basis[1] * jDesc.GetMaxSeparation();
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2020-04-02 06:06:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts == EUpdateOptions::ObjectSpace) {
|
|
|
|
cAct->MoveToOR(cAct->GetTransform().transposeRotate(origin - cAct->GetTranslation()), dt);
|
|
|
|
} else {
|
|
|
|
cAct->SetTranslation(origin);
|
2018-11-13 09:20:11 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2018-11-13 09:20:11 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-03-13 01:07:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 09:20:11 +00:00
|
|
|
zeus::CTransform CCollisionActorManager::GetWRLocatorTransform(const CAnimData& animData, CSegId id,
|
|
|
|
const zeus::CTransform& worldXf,
|
2018-12-08 05:30:43 +00:00
|
|
|
const zeus::CTransform& localXf) {
|
|
|
|
zeus::CTransform locXf = animData.GetLocatorTransform(id, nullptr);
|
2020-04-02 06:06:42 +00:00
|
|
|
const zeus::CVector3f origin = worldXf * (localXf * locXf.origin);
|
2018-12-08 05:30:43 +00:00
|
|
|
locXf = worldXf.multiplyIgnoreTranslation(locXf);
|
|
|
|
locXf.origin = origin;
|
|
|
|
return locXf;
|
2018-10-28 00:20:30 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 02:05:17 +00:00
|
|
|
std::optional<zeus::CVector3f> CCollisionActorManager::GetDeviation(const CStateManager& mgr, CSegId seg) {
|
2018-12-31 08:37:52 +00:00
|
|
|
for (const CJointCollisionDescription& desc : x0_jointDescriptions) {
|
2020-04-02 06:06:42 +00:00
|
|
|
if (desc.GetPivotId() != seg) {
|
2018-12-31 08:37:52 +00:00
|
|
|
continue;
|
2020-04-02 06:06:42 +00:00
|
|
|
}
|
2018-12-31 08:37:52 +00:00
|
|
|
|
2020-04-02 06:06:42 +00:00
|
|
|
if (const TCastToConstPtr<CActor> act = mgr.GetObjectById(x10_ownerId)) {
|
|
|
|
if (const TCastToConstPtr<CCollisionActor> colAct = mgr.GetObjectById(desc.GetCollisionActorId())) {
|
|
|
|
const zeus::CTransform xf =
|
|
|
|
GetWRLocatorTransform(*act->GetModelData()->GetAnimationData(), desc.GetPivotId(), act->GetTransform(),
|
|
|
|
zeus::CTransform::Scale(act->GetModelData()->GetScale()));
|
2018-12-31 08:37:52 +00:00
|
|
|
|
|
|
|
return {colAct->GetTranslation() - xf.origin};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 06:08:11 +00:00
|
|
|
return std::nullopt;
|
2018-12-31 08:37:52 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|