metaforce/Runtime/Character/CBoneTracking.cpp

113 lines
5.0 KiB
C++
Raw Permalink Normal View History

#include "Runtime/Character/CBoneTracking.hpp"
#include "Runtime/CStateManager.hpp"
#include "Runtime/Character/CAnimData.hpp"
#include "Runtime/Character/CBodyController.hpp"
#include "Runtime/Character/CHierarchyPoseBuilder.hpp"
#include "Runtime/World/CPatterned.hpp"
2018-11-17 05:05:57 +00:00
#include "TCastTo.hpp" // Generated file, do not modify include path
2018-11-17 05:05:57 +00:00
2021-04-10 08:42:06 +00:00
namespace metaforce {
2018-11-17 05:05:57 +00:00
2021-06-07 19:29:18 +00:00
CBoneTracking::CBoneTracking(const CAnimData& animData, std::string_view bone, float maxTrackingAngle, float angSpeed,
EBoneTrackingFlags flags)
2018-12-08 05:30:43 +00:00
: x14_segId(animData.GetCharLayoutInfo().GetSegIdFromString(bone))
2018-12-13 07:39:16 +00:00
, x1c_maxTrackingAngle(maxTrackingAngle)
, x20_angSpeed(angSpeed)
2019-04-03 04:32:31 +00:00
, x36_26_noParent(True(flags & EBoneTrackingFlags::NoParent))
, x36_27_noParentOrigin(True(flags & EBoneTrackingFlags::NoParentOrigin))
, x36_28_noHorizontalAim(True(flags & EBoneTrackingFlags::NoHorizontalAim))
, x36_29_parentIk(True(flags & EBoneTrackingFlags::ParentIk)) {}
2018-12-08 05:30:43 +00:00
void CBoneTracking::Update(float dt) { x18_time += dt; }
2018-11-17 05:05:57 +00:00
void CBoneTracking::PreRender(const CStateManager& mgr, CAnimData& animData, const zeus::CTransform& xf,
2018-12-08 05:30:43 +00:00
const zeus::CVector3f& vec, const CBodyController& bodyController) {
TCastToPtr<CPatterned> patterned = bodyController.GetOwner();
2018-11-17 05:05:57 +00:00
2018-12-08 05:30:43 +00:00
PreRender(mgr, animData, xf, vec,
(bodyController.GetBodyStateInfo().ApplyHeadTracking() && patterned && patterned->ApplyBoneTracking()));
2018-11-17 05:05:57 +00:00
}
2018-12-13 07:39:16 +00:00
void CBoneTracking::PreRender(const CStateManager& mgr, CAnimData& animData, const zeus::CTransform& worldXf,
const zeus::CVector3f& localOffsetScale, bool tracking) {
2018-12-08 05:30:43 +00:00
if (x14_segId == 0)
return;
2018-12-13 07:39:16 +00:00
CHierarchyPoseBuilder& pb = animData.PoseBuilder();
2018-12-15 06:29:41 +00:00
TCastToConstPtr<CActor> targetAct = mgr.GetObjectById(x34_target);
2018-12-13 07:39:16 +00:00
if (x36_24_active && tracking && (targetAct || x24_targetPosition)) {
x36_25_hasTrackedRotation = true;
const auto& layoutInfo = pb.CharLayoutInfo();
2018-12-13 07:39:16 +00:00
CSegId bone;
if (x36_26_noParent)
bone = x14_segId;
else
bone = layoutInfo->GetRootNode()->GetBoneMap()[x14_segId].x0_parentId;
zeus::CTransform parentBoneXf;
pb.BuildTransform(bone, parentBoneXf);
zeus::CVector3f pos = parentBoneXf.origin;
if (x36_27_noParentOrigin && !x36_26_noParent) {
zeus::CTransform thisBoneXf;
pb.BuildTransform(x14_segId, thisBoneXf);
pos = thisBoneXf.origin;
}
parentBoneXf.origin = pos * localOffsetScale;
zeus::CTransform finalXf = worldXf * parentBoneXf;
2021-06-07 19:29:18 +00:00
zeus::CVector3f localDir =
finalXf
.transposeRotate((targetAct ? targetAct->GetAimPosition(mgr, 0.f) : *x24_targetPosition) - finalXf.origin)
.normalized();
2018-12-13 07:39:16 +00:00
if (x36_28_noHorizontalAim)
localDir = zeus::CVector3f(0.f, localDir.toVec2f().magnitude(), localDir.z());
if (x36_29_parentIk) {
float negElev = -parentBoneXf.basis[1].z();
zeus::CVector3f ikBase(0.f, std::sqrt(1.f - negElev * negElev), negElev);
float angle = zeus::CVector3f::getAngleDiff(ikBase, localDir);
angle = std::min(angle, x1c_maxTrackingAngle);
localDir = zeus::CVector3f::slerp(ikBase, localDir, angle);
} else {
float angle = zeus::CVector3f::getAngleDiff(zeus::skForward, localDir);
2018-12-13 07:39:16 +00:00
angle = std::min(angle, x1c_maxTrackingAngle);
localDir = zeus::CVector3f::slerp(zeus::skForward, localDir, angle);
2018-12-13 07:39:16 +00:00
}
float angle = zeus::CVector3f::getAngleDiff(x0_curRotation.transform(zeus::skForward), localDir);
2018-12-13 07:39:16 +00:00
float clampedAngle = std::min(angle, x18_time * x20_angSpeed);
if (clampedAngle > 1.0e-05f) {
2021-06-07 19:29:18 +00:00
x0_curRotation = zeus::CQuaternion::slerpShort(
x0_curRotation, zeus::CQuaternion::lookAt(zeus::skForward, zeus::CUnitVector3f(localDir), 2.f * M_PIF),
clampedAngle / angle);
2018-12-13 07:39:16 +00:00
}
pb.GetTreeMap()[x14_segId].x4_rotation = x0_curRotation;
animData.MarkPoseDirty();
} else if (x36_25_hasTrackedRotation) {
zeus::CQuaternion qb = pb.GetTreeMap()[x14_segId].x4_rotation;
2021-06-07 19:29:18 +00:00
float angle =
zeus::CVector3f::getAngleDiff(x0_curRotation.transform(zeus::skForward), qb.transform(zeus::skForward));
2018-12-13 07:39:16 +00:00
float maxAngDelta = x18_time * x20_angSpeed;
float clampedAngle = std::min(angle, maxAngDelta);
if (clampedAngle > 0.5f * maxAngDelta) {
x0_curRotation = zeus::CQuaternion::slerpShort(x0_curRotation, qb, clampedAngle / angle);
pb.GetTreeMap()[x14_segId].x4_rotation = x0_curRotation;
animData.MarkPoseDirty();
} else {
x36_25_hasTrackedRotation = false;
x0_curRotation = qb;
}
} else {
x0_curRotation = pb.GetTreeMap()[x14_segId].x4_rotation;
}
2018-12-08 05:30:43 +00:00
x18_time = 0.f;
2018-11-17 05:05:57 +00:00
}
void CBoneTracking::SetActive(bool active) { x36_24_active = active; }
2018-11-17 05:05:57 +00:00
2018-12-08 05:30:43 +00:00
void CBoneTracking::SetTarget(TUniqueId target) { x34_target = target; }
2018-11-17 05:05:57 +00:00
2018-12-13 07:39:16 +00:00
void CBoneTracking::UnsetTarget() { x34_target = kInvalidUniqueId; }
2018-12-08 05:30:43 +00:00
void CBoneTracking::SetTargetPosition(const zeus::CVector3f& targetPos) { x24_targetPosition = targetPos; }
2018-12-13 07:39:16 +00:00
void CBoneTracking::SetNoHorizontalAim(bool b) { x36_28_noHorizontalAim = b; }
2021-04-10 08:42:06 +00:00
} // namespace metaforce