metaforce/Runtime/Character/CBoneTracking.cpp

109 lines
4.8 KiB
C++
Raw Normal View History

2018-11-17 05:05:57 +00:00
#include "Character/CBoneTracking.hpp"
#include "Character/CBodyController.hpp"
#include "Character/CAnimData.hpp"
#include "Character/CHierarchyPoseBuilder.hpp"
#include "World/CPatterned.hpp"
#include "CStateManager.hpp"
#include "TCastTo.hpp"
2018-12-08 05:30:43 +00:00
namespace urde {
2018-11-17 05:05:57 +00:00
2018-12-13 07:39:16 +00:00
CBoneTracking::CBoneTracking(const CAnimData& animData, std::string_view bone,
float maxTrackingAngle, float angSpeed, bool parentIk)
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)
, x36_26_noParent(parentIk)
, x36_27_noParentOrigin(parentIk)
, x36_28_noHorizontalAim(parentIk)
, x36_29_parentIk(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;
auto layoutInfo = pb.CharLayoutInfo();
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;
zeus::CVector3f localDir = finalXf.transposeRotate(
(targetAct ? targetAct->GetAimPosition(mgr, 0.f) : *x24_targetPosition) - finalXf.origin).normalized();
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::CVector3f::skForward, localDir);
angle = std::min(angle, x1c_maxTrackingAngle);
localDir = zeus::CVector3f::slerp(zeus::CVector3f::skForward, localDir, angle);
}
float angle = zeus::CVector3f::getAngleDiff(x0_curRotation.transform(zeus::CVector3f::skForward), localDir);
float clampedAngle = std::min(angle, x18_time * x20_angSpeed);
if (clampedAngle > 1.0e-05f) {
x0_curRotation = zeus::CQuaternion::slerpShort(x0_curRotation,
zeus::CQuaternion::lookAt(zeus::CVector3f::skForward, zeus::CUnitVector3f(localDir), 2.f * M_PIF),
clampedAngle / angle);
}
pb.GetTreeMap()[x14_segId].x4_rotation = x0_curRotation;
animData.MarkPoseDirty();
} else if (x36_25_hasTrackedRotation) {
zeus::CQuaternion qb = pb.GetTreeMap()[x14_segId].x4_rotation;
float angle = zeus::CVector3f::getAngleDiff(x0_curRotation.transform(zeus::CVector3f::skForward),
qb.transform(zeus::CVector3f::skForward));
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
}
2018-12-08 05:30:43 +00:00
void CBoneTracking::SetActive(bool) { x36_24_active = true; }
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; }
2018-12-08 05:30:43 +00:00
} // namespace urde