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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2006 Erwin Coumans https://bulletphysics.org
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "MultiThreadedDemo.h"
#include "CommonRigidBodyMTBase.h"
#include "../CommonInterfaces/CommonParameterInterface.h"
#include "btBulletDynamicsCommon.h"
#include "btBulletCollisionCommon.h"
#include "LinearMath/btAlignedObjectArray.h"
#include <stdio.h> //printf debugging
#include <algorithm>
#include <cmath>
static btScalar gSliderStackRows = 1.0f;
static btScalar gSliderStackColumns = 1.0f;
static btScalar gSliderStackHeight = 15.0f;
static btScalar gSliderStackWidth = 8.0f;
static btScalar gSliderGroundHorizontalAmplitude = 0.0f;
static btScalar gSliderGroundVerticalAmplitude = 0.0f;
static btScalar gSliderGroundTilt = 0.0f;
static btScalar gSliderRollingFriction = 0.0f;
static bool gSpheresNotBoxes = false;
static void boolPtrButtonCallback(int buttonId, bool buttonState, void* userPointer)
{
if (bool* val = static_cast<bool*>(userPointer))
{
*val = !*val;
}
}
/// MultiThreadedDemo shows how to setup and use multithreading
class MultiThreadedDemo : public CommonRigidBodyMTBase
{
static const int kUpAxis = 1;
btRigidBody* localCreateRigidBody(btScalar mass, const btTransform& worldTransform, btCollisionShape* colSape);
btVector3 m_cameraTargetPos;
float m_cameraPitch;
float m_cameraYaw;
float m_cameraDist;
btRigidBody* m_groundBody;
btTransform m_groundStartXf;
float m_groundMovePhase;
float m_prevRollingFriction;
void createStack(const btTransform& trans, btCollisionShape* boxShape, const btVector3& halfBoxSize, int size, int width);
void createSceneObjects();
void destroySceneObjects();
public:
BT_DECLARE_ALIGNED_ALLOCATOR();
MultiThreadedDemo(struct GUIHelperInterface* helper);
virtual ~MultiThreadedDemo() {}
btQuaternion getGroundRotation() const
{
btScalar tilt = gSliderGroundTilt * SIMD_2_PI / 360.0f;
return btQuaternion(btVector3(1.0f, 0.0f, 0.0f), tilt);
}
struct TestSumBody : public btIParallelSumBody
{
virtual btScalar sumLoop(int iBegin, int iEnd) const BT_OVERRIDE
{
btScalar sum = 0.0f;
for (int i = iBegin; i < iEnd; ++i)
{
if (i > 0)
{
sum += 1.0f / btScalar(i);
}
}
return sum;
}
};
virtual void stepSimulation(float deltaTime) BT_OVERRIDE
{
if (m_dynamicsWorld)
{
if (m_prevRollingFriction != gSliderRollingFriction)
{
m_prevRollingFriction = gSliderRollingFriction;
btCollisionObjectArray& objArray = m_dynamicsWorld->getCollisionObjectArray();
for (int i = 0; i < objArray.size(); ++i)
{
btCollisionObject* obj = objArray[i];
obj->setRollingFriction(gSliderRollingFriction);
}
}
if (m_groundBody)
{
// update ground
const float cyclesPerSecond = 1.0f;
m_groundMovePhase += cyclesPerSecond * deltaTime;
m_groundMovePhase -= std::floor(m_groundMovePhase); // keep phase between 0 and 1
btTransform xf = m_groundStartXf;
float gndHOffset = btSin(m_groundMovePhase * SIMD_2_PI) * gSliderGroundHorizontalAmplitude;
float gndHVel = btCos(m_groundMovePhase * SIMD_2_PI) * gSliderGroundHorizontalAmplitude * cyclesPerSecond * SIMD_2_PI; // d(gndHOffset)/dt
float gndVOffset = btSin(m_groundMovePhase * SIMD_2_PI) * gSliderGroundVerticalAmplitude;
float gndVVel = btCos(m_groundMovePhase * SIMD_2_PI) * gSliderGroundVerticalAmplitude * cyclesPerSecond * SIMD_2_PI; // d(gndVOffset)/dt
btVector3 offset(0, 0, 0);
btVector3 vel(0, 0, 0);
int horizAxis = 2;
offset[horizAxis] = gndHOffset;
vel[horizAxis] = gndHVel;
offset[kUpAxis] = gndVOffset;
vel[kUpAxis] = gndVVel;
xf.setOrigin(xf.getOrigin() + offset);
xf.setRotation(getGroundRotation());
m_groundBody->setWorldTransform(xf);
m_groundBody->setLinearVelocity(vel);
}
// always step by 1/60 for benchmarking
m_dynamicsWorld->stepSimulation(1.0f / 60.0f, 0);
}
#if 0
{
// test parallelSum
TestSumBody testSumBody;
float testSum = btParallelSum( 1, 10000000, 10000, testSumBody );
printf( "sum = %f\n", testSum );
}
#endif
}
virtual void initPhysics() BT_OVERRIDE;
virtual void resetCamera() BT_OVERRIDE
{
m_guiHelper->resetCamera(m_cameraDist,
m_cameraYaw,
m_cameraPitch,
m_cameraTargetPos.x(),
m_cameraTargetPos.y(),
m_cameraTargetPos.z());
}
};
MultiThreadedDemo::MultiThreadedDemo(struct GUIHelperInterface* helper)
: CommonRigidBodyMTBase(helper)
{
m_groundBody = NULL;
m_groundMovePhase = 0.0f;
m_cameraTargetPos = btVector3(0.0f, 0.0f, 0.0f);
m_cameraPitch = -30.0f;
m_cameraYaw = 90.0f;
m_cameraDist = 48.0f;
m_prevRollingFriction = -1.0f;
helper->setUpAxis(kUpAxis);
}
void MultiThreadedDemo::initPhysics()
{
createEmptyDynamicsWorld();
m_dynamicsWorld->setGravity(btVector3(0, -10, 0));
{
SliderParams slider("Stack height", &gSliderStackHeight);
slider.m_minVal = 1.0f;
slider.m_maxVal = 30.0f;
slider.m_clampToIntegers = true;
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
{
SliderParams slider("Stack width", &gSliderStackWidth);
slider.m_minVal = 1.0f;
slider.m_maxVal = 30.0f;
slider.m_clampToIntegers = true;
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
{
SliderParams slider("Stack rows", &gSliderStackRows);
slider.m_minVal = 1.0f;
slider.m_maxVal = 20.0f;
slider.m_clampToIntegers = true;
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
{
SliderParams slider("Stack columns", &gSliderStackColumns);
slider.m_minVal = 1.0f;
slider.m_maxVal = 20.0f;
slider.m_clampToIntegers = true;
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
{
// horizontal ground shake
SliderParams slider("Ground horiz amp", &gSliderGroundHorizontalAmplitude);
slider.m_minVal = 0.0f;
slider.m_maxVal = 1.0f;
slider.m_clampToNotches = false;
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
{
// vertical ground shake
SliderParams slider("Ground vert amp", &gSliderGroundVerticalAmplitude);
slider.m_minVal = 0.0f;
slider.m_maxVal = 1.0f;
slider.m_clampToNotches = false;
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
{
// ground tilt
SliderParams slider("Ground tilt", &gSliderGroundTilt);
slider.m_minVal = -45.0f;
slider.m_maxVal = 45.0f;
slider.m_clampToNotches = false;
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
{
// rolling friction
SliderParams slider("Rolling friction", &gSliderRollingFriction);
slider.m_minVal = 0.0f;
slider.m_maxVal = 1.0f;
slider.m_clampToNotches = false;
m_guiHelper->getParameterInterface()->registerSliderFloatParameter(slider);
}
{
ButtonParams button("Spheres not boxes", 0, false);
button.m_initialState = gSpheresNotBoxes;
button.m_userPointer = &gSpheresNotBoxes;
button.m_callback = boolPtrButtonCallback;
m_guiHelper->getParameterInterface()->registerButtonParameter(button);
}
createSceneObjects();
m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);
}
btRigidBody* MultiThreadedDemo::localCreateRigidBody(btScalar mass, const btTransform& startTransform, btCollisionShape* shape)
{
btRigidBody* body = createRigidBody(mass, startTransform, shape);
if (mass > 0.0f)
{
// prevent bodies from sleeping to make profiling/benchmarking easier
body->forceActivationState(DISABLE_DEACTIVATION);
}
return body;
}
void MultiThreadedDemo::createStack(const btTransform& parentTrans, btCollisionShape* boxShape, const btVector3& halfBoxSize, int height, int width)
{
btTransform trans;
trans.setIdentity();
trans.setRotation(parentTrans.getRotation());
float halfBoxHeight = halfBoxSize.y();
float halfBoxWidth = halfBoxSize.x();
btVector3 offset = btVector3(0, 0, -halfBoxSize.z() * (width - 1));
for (int iZ = 0; iZ < width; iZ++)
{
offset += btVector3(0, 0, halfBoxSize.z() * 2.0f);
for (int iY = 0; iY < height; iY++)
{
// This constructs a row, from left to right
int rowSize = height - iY;
for (int iX = 0; iX < rowSize; iX++)
{
btVector3 pos = offset + btVector3(halfBoxWidth * (1 + iX * 2 - rowSize),
halfBoxHeight * (1 + iY * 2),
0.0f);
trans.setOrigin(parentTrans(pos));
btScalar mass = 1.f;
btRigidBody* body = localCreateRigidBody(mass, trans, boxShape);
body->setFriction(1.0f);
body->setRollingFriction(gSliderRollingFriction);
}
}
}
}
void MultiThreadedDemo::createSceneObjects()
{
{
// create ground box
m_groundStartXf.setOrigin(btVector3(0.f, -3.f, 0.f));
m_groundStartXf.setRotation(getGroundRotation());
//either use heightfield or triangle mesh
btVector3 groundExtents(400, 400, 400);
groundExtents[kUpAxis] = 3;
btCollisionShape* groundShape = new btBoxShape(groundExtents);
m_collisionShapes.push_back(groundShape);
//create ground object
m_groundBody = createKinematicBody(m_groundStartXf, groundShape);
m_groundBody->forceActivationState(DISABLE_DEACTIVATION);
m_groundBody->setFriction(1.0f);
}
{
// create walls of cubes
const btVector3 halfExtents = btVector3(0.5f, 0.25f, 0.5f);
int numStackRows = btMax(1, int(gSliderStackRows));
int numStackCols = btMax(1, int(gSliderStackColumns));
int stackHeight = int(gSliderStackHeight);
int stackWidth = int(gSliderStackWidth);
float stackZSpacing = 2.0f + stackWidth * halfExtents.x() * 2.0f;
float stackXSpacing = 20.0f;
btBoxShape* boxShape = new btBoxShape(halfExtents);
m_collisionShapes.push_back(boxShape);
btSphereShape* sphereShape = new btSphereShape(0.5f);
m_collisionShapes.push_back(sphereShape);
btCollisionShape* shape = boxShape;
if (gSpheresNotBoxes)
{
shape = sphereShape;
}
btTransform groundTrans;
groundTrans.setIdentity();
groundTrans.setRotation(getGroundRotation());
for (int iX = 0; iX < numStackCols; ++iX)
{
for (int iZ = 0; iZ < numStackRows; ++iZ)
{
btVector3 center = btVector3(iX * stackXSpacing, 0.0f, (iZ - numStackRows / 2) * stackZSpacing);
btTransform trans = groundTrans;
trans.setOrigin(groundTrans(center));
createStack(trans, shape, halfExtents, stackHeight, stackWidth);
}
}
}
#if 0
if ( false )
{
// destroyer ball
btTransform sphereTrans;
sphereTrans.setIdentity();
sphereTrans.setOrigin( btVector3( 0, 2, 40 ) );
btSphereShape* ball = new btSphereShape( 2.f );
m_collisionShapes.push_back( ball );
btRigidBody* ballBody = localCreateRigidBody( 10000.f, sphereTrans, ball );
ballBody->setLinearVelocity( btVector3( 0, 0, -10 ) );
}
#endif
m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);
}
CommonExampleInterface* MultiThreadedDemoCreateFunc(struct CommonExampleOptions& options)
{
return new MultiThreadedDemo(options.m_guiHelper);
}
|