-
Notifications
You must be signed in to change notification settings - Fork 98
/
Physics_SoftBody.cpp
342 lines (255 loc) · 9.13 KB
/
Physics_SoftBody.cpp
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "StdAfx.h"
#if 0
#include "convert.h"
#include "Physics_SoftBody.h"
#include "Physics_Environment.h"
#include "Physics_Object.h"
#include "BulletSoftBody/btSoftBodyHelpers.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
void ConvertNodeToHL(const btSoftBody::Node *node, softbodynode_t &nodeOut) {
ConvertPosToHL(node->m_x, nodeOut.pos);
ConvertPosToHL(node->m_v, nodeOut.vel);
nodeOut.invMass = node->m_im;
}
void ConvertNodeToBull(const softbodynode_t &node, btSoftBody::Node &nodeOut) {
ConvertPosToBull(node.pos, nodeOut.m_x);
ConvertPosToBull(node.vel, nodeOut.m_v);
nodeOut.m_im = node.invMass;
}
void ConvertFaceToHL(const btSoftBody::Face *face, softbodyface_t &faceOut) {
for (int i = 0; i < 3; i++) {
btSoftBody::Node *node = face->m_n[i];
ConvertNodeToHL(node, faceOut.nodes[i]);
}
ConvertDirectionToHL(face->m_normal, faceOut.normal);
}
void ConvertLinkToHL(const btSoftBody::Link *link, softbodylink_t &linkOut) {
for (int i = 0; i < 2; i++) {
btSoftBody::Node *node = link->m_n[i];
ConvertNodeToHL(node, linkOut.nodes[i]);
}
}
/*************************
* CLASS CPhysicsSoftBody
*************************/
CPhysicsSoftBody::CPhysicsSoftBody() {
m_pEnv = NULL;
m_pSoftBody = NULL;
}
CPhysicsSoftBody::~CPhysicsSoftBody() {
m_pEnv->GetBulletEnvironment()->removeSoftBody(m_pSoftBody);
delete m_pSoftBody;
}
bool CPhysicsSoftBody::IsAsleep() const {
return m_pSoftBody->getActivationState() == ISLAND_SLEEPING || m_pSoftBody->getActivationState() == DISABLE_SIMULATION;
}
void CPhysicsSoftBody::SetTotalMass(float fMass, bool bFromFaces) {
m_pSoftBody->setTotalMass(fMass, bFromFaces);
}
void CPhysicsSoftBody::Anchor(int node, IPhysicsObject *pObj) {
m_pSoftBody->appendAnchor(node, ((CPhysicsObject *)pObj)->GetObject());
}
int CPhysicsSoftBody::GetNodeCount() const {
return m_pSoftBody->m_nodes.size();
}
int CPhysicsSoftBody::GetFaceCount() const {
return m_pSoftBody->m_faces.size();
}
int CPhysicsSoftBody::GetLinkCount() const {
return m_pSoftBody->m_links.size();
}
softbodynode_t CPhysicsSoftBody::GetNode(int i) const {
Assert(i >= 0 && i < m_pSoftBody->m_nodes.size());
btSoftBody::Node &node = m_pSoftBody->m_nodes[i];
softbodynode_t out;
ConvertNodeToHL(&node, out);
return out;
}
softbodylink_t CPhysicsSoftBody::GetLink(int i) const {
Assert(i >= 0 && i < m_pSoftBody->m_links.size());
btSoftBody::Link &link = m_pSoftBody->m_links[i];
softbodylink_t out;
ConvertLinkToHL(&link, out);
// Find the node ids with an educated guess (this will be some huge number if something bad happened)
for (int i = 0; i < 2; i++) {
out.nodeIndexes[i] = (int)(link.m_n[i] - &m_pSoftBody->m_nodes[0]);
}
return out;
}
softbodyface_t CPhysicsSoftBody::GetFace(int i) const {
Assert(i >= 0 && i < m_pSoftBody->m_faces.size());
btSoftBody::Face &face = m_pSoftBody->m_faces[i];
softbodyface_t out;
ConvertFaceToHL(&face, out);
// Find the node ids with an educated guess
for (int i = 0; i < 3; i++) {
out.nodeIndexes[i] = (int)(face.m_n[i] - &m_pSoftBody->m_nodes[0]);
}
return out;
}
void CPhysicsSoftBody::SetNode(int i, softbodynode_t &node) {
Assert(i >= 0 && i < m_pSoftBody->m_nodes.size());
btSoftBody::Node &bnode = m_pSoftBody->m_nodes[i];
ConvertNodeToBull(node, bnode);
}
void CPhysicsSoftBody::AddNode(const Vector &pos, float mass) {
btVector3 btpos;
ConvertPosToBull(pos, btpos);
m_pSoftBody->appendNode(btpos, mass);
}
void CPhysicsSoftBody::AddLink(int node1, int node2, bool bCheckExist) {
m_pSoftBody->appendLink(node1, node2, NULL, bCheckExist);
}
void CPhysicsSoftBody::GetAABB(Vector *mins, Vector *maxs) const {
if (!mins && !maxs) return;
btVector3 btmins, btmaxs;
m_pSoftBody->getAabb(btmins, btmaxs);
Vector tMins, tMaxs;
ConvertAABBToHL(btmins, btmaxs, tMins, tMaxs);
if (mins)
*mins = tMins;
if (maxs)
*maxs = tMaxs;
}
void CPhysicsSoftBody::RemoveNode(int i) {
Assert(i >= 0 && i < m_pSoftBody->m_nodes.size());
m_pSoftBody->pointersToIndices();
//m_pSoftBody->m_nodes.remove(m_pSoftBody->m_nodes[i]);
m_pSoftBody->indicesToPointers();
}
void CPhysicsSoftBody::RemoveLink(int i) {
Assert(i >= 0 && i < m_pSoftBody->m_links.size());
btSoftBody::Link &link = m_pSoftBody->m_links[i];
m_pSoftBody->pointersToIndices();
// TODO: Need to update the indices of anything referencing
// the link that is swapped to this position
//m_pSoftBody->m_links.remove(link);
m_pSoftBody->indicesToPointers();
}
void CPhysicsSoftBody::RemoveFace(int i) {
Assert(i >= 0 && i < m_pSoftBody->m_faces.size());
btSoftBody::Face &face = m_pSoftBody->m_faces[i];
m_pSoftBody->pointersToIndices();
//m_pSoftBody->m_faces.remove(face);
m_pSoftBody->indicesToPointers();
}
void CPhysicsSoftBody::RayTest(Ray_t &ray, trace_t *pTrace) const {
if (!pTrace) return;
btVector3 start, end;
ConvertPosToBull(ray.m_Start, start);
ConvertPosToBull(ray.m_Start + ray.m_Delta, end);
btSoftBody::sRayCast rayCast;
m_pSoftBody->rayTest(start, end, rayCast);
pTrace->fraction = rayCast.fraction;
pTrace->startpos = ray.m_Start + ray.m_StartOffset;
pTrace->endpos = ray.m_Start + ray.m_StartOffset + (ray.m_Delta * rayCast.fraction);
}
void CPhysicsSoftBody::BoxTest(Ray_t &ray, trace_t *pTrace) const {
if (!pTrace) return;
btVector3 start, end;
ConvertPosToBull(ray.m_Start, start);
ConvertPosToBull(ray.m_Start + ray.m_Delta, end);
NOT_IMPLEMENTED
}
void CPhysicsSoftBody::Transform(const matrix3x4_t &mat) {
btTransform trans;
ConvertMatrixToBull(mat, trans);
m_pSoftBody->transform(trans);
}
void CPhysicsSoftBody::Transform(const Vector *vec, const QAngle *ang) {
if (!vec && !ang) return;
if (vec) {
btVector3 bVec;
ConvertPosToBull(*vec, bVec);
m_pSoftBody->translate(bVec);
}
if (ang) {
btQuaternion quat;
ConvertRotationToBull(*ang, quat);
m_pSoftBody->rotate(quat);
}
}
void CPhysicsSoftBody::Scale(const Vector &scale) {
// No conversion
btVector3 btScale;
btScale.setX(scale.x);
btScale.setY(scale.z);
btScale.setZ(scale.y);
m_pSoftBody->scale(btScale);
}
void CPhysicsSoftBody::Init(CPhysicsEnvironment *pEnv, btSoftBody *pSoftBody, const softbodyparams_t *pParams) {
m_pEnv = pEnv;
m_pSoftBody = pSoftBody;
m_pSoftBody->m_cfg.collisions |= btSoftBody::fCollision::VF_SS;
m_pSoftBody->m_cfg.viterations = 1;
pEnv->GetBulletEnvironment()->addSoftBody(m_pSoftBody);
}
btSoftBody *CPhysicsSoftBody::GetSoftBody() {
return m_pSoftBody;
}
/*************************
* CREATION FUNCTIONS
*************************/
CPhysicsSoftBody *CreateSoftBody(CPhysicsEnvironment *pEnv) {
btSoftBody *pSoftBody = new btSoftBody(&pEnv->GetSoftBodyWorldInfo());
CPhysicsSoftBody *pPhysBody = new CPhysicsSoftBody;
return pPhysBody;
}
CPhysicsSoftBody *CreateSoftBodyFromTriMesh(CPhysicsEnvironment *pEnv, const Vector *vertices, int numVertices, const int *indices, int numIndices, const Vector &position, const QAngle &angles, const softbodyparams_t *pParams) {
btVector3 *bullVerts = new btVector3[numVertices];
// Make sure numIndices is evenly divisible by 3
Assert(numIndices % 3 == 0);
for (int i = 0; i < numVertices; i++) {
ConvertPosToBull(vertices[i], bullVerts[i]);
}
/*
btSoftBody *pSoftBody = new btSoftBody(&pEnv->GetSoftBodyWorldInfo(), numVertices, bullVerts, NULL);
for (int i = 0; i < numIndices; i++) {
}
*/
delete [] bullVerts;
NOT_IMPLEMENTED
return NULL;
}
CPhysicsSoftBody *CreateSoftBodyFromVertices(CPhysicsEnvironment *pEnv, const Vector *vertices, int numVertices, const softbodyparams_t *pParams) {
btVector3 *bullVerts = new btVector3[numVertices];
for (int i = 0; i < numVertices; i++) {
ConvertPosToBull(vertices[i], bullVerts[i]);
}
btSoftBodyWorldInfo &wi = pEnv->GetSoftBodyWorldInfo();
btSoftBody *pSoftBody = btSoftBodyHelpers::CreateFromConvexHull(wi, bullVerts, numVertices);
delete [] bullVerts;
CPhysicsSoftBody *pBody = new CPhysicsSoftBody;
pBody->Init(pEnv, pSoftBody, pParams);
return pBody;
}
CPhysicsSoftBody *CreateSoftBodyRope(CPhysicsEnvironment *pEnv, const Vector &position, const Vector &end, int resolution, const softbodyparams_t *pParams) {
btSoftBodyWorldInfo &wi = pEnv->GetSoftBodyWorldInfo();
btVector3 btStart, btEnd;
ConvertPosToBull(position, btStart);
ConvertPosToBull(end, btEnd);
// Last parameter is fixed sides of the soft body (which we don't set - dev can set these elsewhere)
btSoftBody *pSoftBody = btSoftBodyHelpers::CreateRope(wi, btStart, btEnd, resolution, 0);
if (pParams) {
pSoftBody->setTotalMass(pParams->totalMass);
}
CPhysicsSoftBody *pPhysBody = new CPhysicsSoftBody;
pPhysBody->Init(pEnv, pSoftBody, pParams);
return pPhysBody;
}
CPhysicsSoftBody *CreateSoftBodyPatch(CPhysicsEnvironment *pEnv, const Vector *corners, int resx, int resy, const softbodyparams_t *pParams) {
btSoftBodyWorldInfo &wi = pEnv->GetSoftBodyWorldInfo();
btVector3 bcorners[4];
for (int i = 0; i < 4; i++) {
ConvertPosToBull(corners[i], bcorners[i]);
}
btSoftBody *pSoftBody = btSoftBodyHelpers::CreatePatch(wi, bcorners[0], bcorners[1], bcorners[2], bcorners[3], resx, resy, 0, false);
if (pParams) {
pSoftBody->setTotalMass(pParams->totalMass);
}
CPhysicsSoftBody *pPhysBody = new CPhysicsSoftBody;
pPhysBody->Init(pEnv, pSoftBody, pParams);
return pPhysBody;
}
#endif