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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
#include "KingSystem/Physics/RigidBody/Shape/Polytope/physPolytopeShape.h"
#include <Havok/Common/Base/Types/Geometry/hkStridedVertices.h>
#include <Havok/Physics2012/Collide/Shape/Convex/ConvexTransform/hkpConvexTransformShape.h>
#include <Havok/Physics2012/Collide/Shape/Convex/ConvexVertices/hkpConvexVerticesShape.h>
#include <math/seadMathCalcCommon.h>
#include <prim/seadScopedLock.h>
#include "KingSystem/Physics/physHeapUtil.h"
#include "KingSystem/Utils/HeapUtil.h"
#include "KingSystem/Utils/SafeDelete.h"
namespace ksys::phys {
PolytopeShape* PolytopeShape::make(const PolytopeShapeParam& param, sead::Heap* heap) {
auto* shape = new (heap) PolytopeShape(param);
if (!shape->init(param, heap)) {
delete shape;
return nullptr;
}
return shape;
}
PolytopeShape* PolytopeShape::clone(sead::Heap* heap) const {
PolytopeShapeParam param;
param.vertex_num = u16(mVertices.size());
auto* cloned = make(param, heap);
for (int i = 0; i < int(param.vertex_num); ++i) {
cloned->setVertex(i, getVertex(i));
}
cloned->setNumVertices(getNumVertices());
cloned->updateHavokShape();
cloned->setMaterialMask(getMaterialMask());
return cloned;
}
bool PolytopeShape::setVertex(int vertex_idx, const sead::Vector3f& vertex) {
if (mFlags.isOn(Flag::HasCustomScale)) {
mScale = 1.0;
mFlags.reset(Flag::HasCustomScale);
mFlags.set(Flag::_4);
mFlags.set(Flag::_10);
}
if (vertex != mVertices[vertex_idx]) {
mVertices[vertex_idx] = vertex;
mFlags.set(Flag::_1 | Flag::InvalidVolume);
mVolume = -1.0;
return true;
}
return false;
}
void PolytopeShape::setNumVertices(u16 num) {
if (mNumVertices == num)
return;
mNumVertices = num;
mVolume = -1.0;
mFlags.set(Flag::_1 | Flag::InvalidVolume);
}
PolytopeShape::PolytopeShape(const PolytopeShapeParam& param)
: mMaterialMask(param.common.getMaterialMask()), mNumVertices(param.vertex_num) {
if (param.common.item_code_disable_stick)
mMaterialMask.getData().setCustomFlag(MaterialMaskData::CustomFlag::_0);
setMaterialMask(mMaterialMask);
}
PolytopeShape::~PolytopeShape() {
deleteRefCountedHavokObject(mHavokShape);
util::deallocateObjectUnsafe(mTransformShape);
mVertices.freeBuffer();
}
hkpShape* PolytopeShape::getHavokShape() {
if (mFlags.isOn(Flag::HasCustomScale))
return mTransformShape;
return mHavokShape;
}
const hkpShape* PolytopeShape::getHavokShape() const {
if (mFlags.isOn(Flag::HasCustomScale))
return mTransformShape;
return mHavokShape;
}
const hkpShape* PolytopeShape::updateHavokShape() {
bool return_new_shape = false;
if (mFlags.isOn(Flag::_1)) {
auto lock = sead::makeScopedLock(mCS);
mHavokShape->setConnectivity(nullptr, false);
hkStridedVertices vertices;
vertices.set(mVertices.getBufferPtr(), mNumVertices);
if (auto* shape = new hkpConvexVerticesShape(vertices)) {
// TODO
for (int i = 0, n = shape->getReferenceCount(); i < n; ++i)
shape->removeReference();
}
if (mFlags.isOn(Flag::InvalidVolume)) {
if (auto* connectivity = mHavokShape->getConnectivity()) {
// TODO: recalculate volume
mVolume = {};
} else {
// TODO
}
mFlags.reset(Flag::InvalidVolume);
}
mFlags.reset(Flag::_1);
}
if (mFlags.isOn(Flag::_4)) {
auto lock = sead::makeScopedLock(mCS);
hkQsTransform transform;
transform.setIdentity();
transform.m_scale.setAll(mScale);
const auto ref_count = mTransformShape->getReferenceCount();
mTransformShape = new (mTransformShape) hkpConvexTransformShape(
mHavokShape, transform, hkpShapeContainer::REFERENCE_POLICY_IGNORE);
mTransformShape->setReferenceCount(ref_count);
mFlags.reset(Flag::_4);
}
setMaterialMask(mMaterialMask);
if (mFlags.isOn(Flag::_10)) {
mFlags.reset(Flag::_10);
} else if (!return_new_shape) {
return nullptr;
}
return getHavokShapeConst();
}
void PolytopeShape::setScale(float scale) {
const bool invalid_volume = mFlags.isOn(Flag::InvalidVolume);
const float volume = mVolume;
mScale *= scale;
mFlags.set(Flag::_4);
if (sead::Mathf::equalsEpsilon(mScale, 1.0)) {
mScale = 1.0;
}
// Rescale all vertices.
for (int i = 0, n = int(mNumVertices); i < n; ++i) {
mVertices[i] = scale * getVertex(i);
}
const bool had_custom_scale = mFlags.isOn(Flag::HasCustomScale);
const bool has_custom_scale = mScale != 1.0;
mFlags.change(Flag::HasCustomScale, has_custom_scale);
mFlags.change(Flag::_10, had_custom_scale != has_custom_scale);
if (!invalid_volume) {
setVolume(scale * scale * scale * volume);
}
}
void PolytopeShape::setVolume(float volume) {
mVolume = volume;
mFlags.reset(Flag::InvalidVolume);
}
float PolytopeShape::getVolume() const {
return mVolume;
}
void PolytopeShape::setMaterialMask(const MaterialMask& mask) {
mMaterialMask = mask;
if (mHavokShape)
mHavokShape->setUserData(mask.getRawData());
if (mTransformShape)
mTransformShape->setUserData(mask.getRawData());
}
bool PolytopeShape::init(const PolytopeShapeParam& param, sead::Heap* heap) {
mVertices.allocBufferAssert(param.vertex_num, heap);
mNumVertices = param.vertex_num;
for (int i = 0; i < int(param.vertex_num); ++i) {
sead::Mathf::sinCosIdx(&mVertices[i].y, &mVertices[i].z,
(sead::Mathu::maxNumber() / mNumVertices) * i);
mVertices[i].x = static_cast<float>(i % 2);
}
// Alloc the vertices shape.
hkStridedVertices vertices;
vertices.set(mVertices.getBufferPtr(), mNumVertices);
mHavokShape = new hkpConvexVerticesShape(vertices);
if (!mHavokShape) {
return false;
}
// Alloc the transform shape.
if (auto* storage = util::allocStorage<hkpConvexTransformShape>(heap)) {
mTransformShape =
new (storage) hkpConvexTransformShape(mHavokShape, hkQsTransform::IdentityInitializer(),
hkpShapeContainer::REFERENCE_POLICY_IGNORE);
}
setMaterialMask(mMaterialMask);
return true;
}
} // namespace ksys::phys
|