summaryrefslogtreecommitdiff
path: root/src/KingSystem/Physics/Ragdoll/physRagdollController.cpp
blob: f8ded3a102e9d5c69bc25f70a8be10f53fb57b8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "KingSystem/Physics/Ragdoll/physRagdollController.h"
#include <Havok/Animation/Physics2012Bridge/Controller/RigidBody/hkaRagdollRigidBodyController.h>
#include <cmath>
#include <math/seadMathCalcCommon.h>
#include "KingSystem/Physics/Ragdoll/physRagdollControllerKeyList.h"
#include "KingSystem/Physics/Ragdoll/physRagdollInstance.h"
#include "KingSystem/Physics/System/physSystem.h"
#include "KingSystem/Utils/Debug.h"
#include "KingSystem/Utils/SafeDelete.h"

namespace ksys::phys {

static bool sForceDefaultWeights = false;

void RagdollController::forceDefaultWeights(bool force) {
    sForceDefaultWeights = force;
}

RagdollController::RagdollController() = default;

RagdollController::~RagdollController() {
    if (mRagdollRigidBodyCtrl)
        util::safeDelete(mRagdollRigidBodyCtrl);
    mConfiguredBoneWeights.freeBuffer();
    mEffectiveBoneWeights.freeBuffer();
    mMultipliers.freeBuffer();
}

bool RagdollController::init(const sead::SafeString& name, const sead::SafeString& system_key,
                             RagdollInstance* instance, sead::Heap* heap) {
    mName = name;
    mInstance = instance;
    util::PrintDebugFmt("creating RagdollController for %s", mName.cstr());
    mRagdollRigidBodyCtrl = new hkaRagdollRigidBodyController(instance->getHavokRagdollInstance());

    const int num_bones = instance->getRigidBodies_().size();

    mConfiguredBoneWeights.allocBufferAssert(num_bones, heap);
    mEffectiveBoneWeights.allocBufferAssert(num_bones, heap);
    mMultipliers.allocBufferAssert(num_bones, heap);

    for (int i = 0; i < num_bones; ++i) {
        mConfiguredBoneWeights[i] = 1.0f;
        mEffectiveBoneWeights[i] = 1.0f;
        mMultipliers[i] = 1.0f;
    }

    // Configure the controller.
    mRagdollRigidBodyCtrl->setBoneWeights(mEffectiveBoneWeights.getBufferPtr());
    if (System::instance()->getRagdollCtrlKeyList() != nullptr) {
        auto* config =
            System::instance()->getRagdollCtrlKeyList()->getControllerKeyByKey(system_key);
        if (config != nullptr) {
            const auto get_control_data = [this]() -> decltype(auto) {
                return mRagdollRigidBodyCtrl->m_controlDataPalette[0];
            };

            get_control_data().m_hierarchyGain = *config->hierarchy_gain;
            get_control_data().m_velocityDamping = *config->velocity_damping;
            get_control_data().m_accelerationGain = *config->acceleration_gain;
            get_control_data().m_velocityGain = *config->velocity_gain;
            get_control_data().m_positionGain = *config->position_gain;
            get_control_data().m_positionMaxLinearVelocity = *config->position_max_linear_velocity;
            get_control_data().m_positionMaxAngularVelocity =
                *config->position_max_angular_velocity;
            get_control_data().m_snapGain = *config->snap_gain;
            get_control_data().m_snapMaxLinearVelocity = *config->snap_max_linear_velocity;
            get_control_data().m_snapMaxAngularVelocity = *config->snap_max_angular_velocity;
            get_control_data().m_snapMaxLinearDistance = *config->snap_max_linear_distance;
            get_control_data().m_snapMaxAngularDistance = *config->snap_max_angular_distance;
        }
    }

    return true;
}

bool RagdollController::setBoneWeight(int index, float weight) {
    if (sForceDefaultWeights)
        return false;

    if (index < 0 || index >= mConfiguredBoneWeights.size())
        return false;

    if (std::isnan(weight))
        return false;

    weight = sead::Mathf::abs(weight);

    if (weight > sead::Mathf::maxNumber())
        return false;

    weight = sead::Mathf::clamp(weight, 0.0, 1.0);

    mConfiguredBoneWeights[index] = weight;
    recalculateEffectiveBoneWeight(index);
    return true;
}

bool RagdollController::setBoneWeight(const sead::SafeString& rigid_name, float weight) {
    int index = mInstance->getBoneIndexByName(rigid_name);
    return setBoneWeight(index, weight);
}

void RagdollController::setFactor(float factor) {
    if (sForceDefaultWeights)
        return;

    if (std::isnan(factor))
        return;

    if (sead::Mathf::abs(factor) > sead::Mathf::maxNumber())
        return;

    factor = sead::Mathf::clamp(factor, -1.0, 1.0);

    if (mFactor != factor) {
        // Change the factor and recalculate all effective bone weights.
        mFactor = factor;
        for (int i = 0, n = mConfiguredBoneWeights.size(); i < n; ++i) {
            recalculateEffectiveBoneWeight(i);
        }
    }
}

// NON_MATCHING
void RagdollController::recalculateEffectiveBoneWeight(int index) {
    const auto factor = [this] { return mFactor < 0 ? -mFactor : mFactor; };

    if (mFactor == 0.0) {
        mEffectiveBoneWeights[index] = mMultipliers[index] * mConfiguredBoneWeights[index];
    } else if (mFactor < 0.0) {
        mEffectiveBoneWeights[index] =
            mMultipliers[index] *
            (mConfiguredBoneWeights[index] + (0.0f - mConfiguredBoneWeights[index]) * factor());
    } else {
        mEffectiveBoneWeights[index] =
            mMultipliers[index] *
            (mConfiguredBoneWeights[index] + (1.0f - mConfiguredBoneWeights[index]) * factor());
    }
}

void RagdollController::reset() {
    reinitController();
    mFactor = -1.0f;
    setFactor(0.0f);
}

void RagdollController::reinitController() {
    mRagdollRigidBodyCtrl->reinitialize();
}

void RagdollController::resetMultipliers() {
    for (int i = 0, n = mMultipliers.size(); i < n; ++i) {
        if (mMultipliers[i] == 1.0)
            continue;

        mMultipliers[i] = 1.0;
        recalculateEffectiveBoneWeight(i);
    }
}

}  // namespace ksys::phys