summaryrefslogtreecommitdiff
path: root/src/KingSystem/Physics/RigidBody/Shape/Box/physBoxShape.cpp
blob: 1ded03e0d73a4f7ed27d343cc4f1908ddb25c7ac (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include "KingSystem/Physics/RigidBody/Shape/Box/physBoxShape.h"
#include <Havok/Physics2012/Collide/Shape/Convex/Box/hkpBoxShape.h>
#include <Havok/Physics2012/Collide/Shape/Convex/ConvexTransform/hkpConvexTransformShape.h>
#include "KingSystem/Physics/physConversions.h"
#include "KingSystem/Utils/HeapUtil.h"
#include "KingSystem/Utils/SafeDelete.h"

namespace ksys::phys {

BoxShape* BoxShape::make(const BoxShapeParam& param, sead::Heap* heap) {
    hkpBoxShape* box = nullptr;
    if (auto* storage = util::allocStorage<hkpBoxShape>(heap)) {
        const auto radius = param.convex_radius;
        const hkVector4f half_extents{sead::Mathf::clampMin(param.extents.x / 2 - radius, 0.001),
                                      sead::Mathf::clampMin(param.extents.y / 2 - radius, 0.001),
                                      sead::Mathf::clampMin(param.extents.z / 2 - radius, 0.001)};
        box = new (storage) hkpBoxShape(half_extents, radius);
    }

    hkpConvexTransformShape* transform_shape = nullptr;
    if (auto* storage = util::allocStorage<hkpConvexTransformShape>(heap)) {
        sead::Quatf rotation;
        rotation.setRPY(param.rotate.x, param.rotate.y, param.rotate.z);
        hkQsTransformf transform{toHkVec4(param.translate), toHkQuat(rotation)};
        transform_shape = new (storage) hkpConvexTransformShape(box, transform);
    }

    if (!box || !transform_shape) {
        if (box)
            delete reinterpret_cast<u8*>(box);
        if (transform_shape)
            delete reinterpret_cast<u8*>(transform_shape);
        return nullptr;
    }

    return new (heap) BoxShape(param, box, transform_shape);
}

BoxShape* BoxShape::clone(sead::Heap* heap) const {
    BoxShapeParam param;
    param.extents = mExtents;
    param.translate = mTranslate;
    param.rotate = mRotate;
    auto* cloned = make(param, heap);
    cloned->setMaterialMask(mMaterialMask);
    return cloned;
}

BoxShape::BoxShape(const BoxShapeParam& param, hkpBoxShape* shape,
                   hkpConvexTransformShape* transform_shape)
    : mExtents(param.extents), mTranslate(param.translate), mRotate(param.rotate),
      mHavokShape(shape), mMaterialMask(param.common.getMaterialMask()),
      mTransformShape(transform_shape) {
    const bool no_transform =
        mTranslate == sead::Vector3f(0, 0, 0) && mRotate == sead::Vector3f(0, 0, 0);
    mFlags.change(Flag::HasTransform, !no_transform);

    if (param.common.item_code_disable_stick)
        mMaterialMask.getData().setCustomFlag(MaterialMaskData::CustomFlag::_0);

    setMaterialMask(mMaterialMask);
}

BoxShape::~BoxShape() {
    util::deallocateObjectUnsafe(mHavokShape);
    util::deallocateObjectUnsafe(mTransformShape);
}

bool BoxShape::setExtents(const sead::Vector3f& extents) {
    if (mExtents == extents)
        return false;

    mExtents = extents;
    mFlags.set(Flag::Dirty);
    return true;
}

bool BoxShape::setTranslate(const sead::Vector3f& translate) {
    if (mTranslate == translate)
        return false;

    mTranslate = translate;

    const bool had_transform = mFlags.isOn(Flag::HasTransform);
    const bool no_transform =
        mTranslate == sead::Vector3f(0, 0, 0) && mRotate == sead::Vector3f(0, 0, 0);

    mFlags.change(Flag::HasTransform, !no_transform);
    mFlags.change(Flag::DirtyTransform, had_transform != !no_transform);
    mFlags.set(Flag::Dirty);

    return true;
}

void BoxShape::setMaterialMask(const MaterialMask& mask) {
    mMaterialMask = mask;

    if (mHavokShape)
        mHavokShape->setUserData(mask.getRawData());

    if (mTransformShape)
        mTransformShape->setUserData(mask.getRawData());
}

float BoxShape::getVolume() const {
    return mExtents.x * mExtents.y * mExtents.z;
}

hkpShape* BoxShape::getHavokShape() {
    if (mFlags.isOn(Flag::HasTransform))
        return mTransformShape;
    return mHavokShape;
}

const hkpShape* BoxShape::getHavokShape() const {
    if (mFlags.isOn(Flag::HasTransform))
        return mTransformShape;
    return mHavokShape;
}

const hkpShape* BoxShape::updateHavokShape() {
    if (mFlags.isOn(Flag::Dirty)) {
        {
            const auto radius = mHavokShape->getRadius();
            const sead::Vector3f half_extents{
                sead::Mathf::clampMin(mExtents.x / 2 - radius, 0.001),
                sead::Mathf::clampMin(mExtents.y / 2 - radius, 0.001),
                sead::Mathf::clampMin(mExtents.z / 2 - radius, 0.001)};
            const auto ref_count = mHavokShape->getReferenceCount();
            mHavokShape = new (mHavokShape) hkpBoxShape(toHkVec4(half_extents), radius);
            mHavokShape->setReferenceCount(ref_count);
        }
        {
            sead::Quatf rotation;
            rotation.setRPY(mRotate.x, mRotate.y, mRotate.z);
            hkQsTransformf transform{toHkVec4(mTranslate), toHkQuat(rotation)};
            const auto ref_count = mTransformShape->getReferenceCount();
            mTransformShape = new (mTransformShape) hkpConvexTransformShape(mHavokShape, transform);
            mTransformShape->setReferenceCount(ref_count);
        }

        setMaterialMask(mMaterialMask);

        mFlags.reset(Flag::Dirty);
    }

    if (mFlags.isOn(Flag::DirtyTransform)) {
        mFlags.reset(Flag::DirtyTransform);
        return getHavokShapeConst();
    }

    return nullptr;
}

void BoxShape::setScale(float scale) {
    const auto scaled_extents = mExtents * scale;
    if (mExtents != scaled_extents) {
        mExtents = scaled_extents;
        mFlags.set(Flag::Dirty);
    }
    setTranslate(mTranslate * scale);
}

void BoxShape::getTranslate(sead::Vector3f* out, const hkTransformf& transform) const {
    hkVector4f translate;
    if (mFlags.isOn(Flag::HasTransform)) {
        hkVector4f transformed;
        transformed.setTransformedPos(transform, toHkVec4(mTranslate));
        translate = transformed;
    } else {
        translate = transform.getTranslation();
    }
    storeToVec3(out, translate);
}

}  // namespace ksys::phys