-
-
Notifications
You must be signed in to change notification settings - Fork 259
Expand file tree
/
Copy pathCollisionDetectionScene.cpp
More file actions
329 lines (265 loc) · 14.7 KB
/
CollisionDetectionScene.cpp
File metadata and controls
329 lines (265 loc) · 14.7 KB
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
/********************************************************************************
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
* Copyright (c) 2010-2016 Daniel Chappuis *
*********************************************************************************
* *
* 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. *
* *
********************************************************************************/
// Libraries
#include "CollisionDetectionScene.h"
#include "../../common/ResourceManager.h"
#include <reactphysics3d/constraint/ContactPoint.h>
#include <reactphysics3d/collision/ContactManifold.h>
// Namespaces
using namespace openglframework;
using namespace collisiondetectionscene;
// Constructor
CollisionDetectionScene::CollisionDetectionScene(const std::string& name, EngineSettings& settings, reactphysics3d::PhysicsCommon& physicsCommon)
: SceneDemo(name, settings, physicsCommon, false), mMeshFolderPath("meshes/"),
mContactManager(mPhongShader, mMeshFolderPath, mSnapshotsContactPoints),
mAreNormalsDisplayed(false) {
mSelectedShapeIndex = 0;
mAreContactPointsDisplayed = true;
mAreContactNormalsDisplayed = false;
mIsWireframeEnabled = true;
// Compute the radius and the center of the scene
openglframework::Vector3 center(0, 0, 0);
// Set the center of the scene
setScenePosition(center, SCENE_RADIUS);
setInitZoom(1.9);
resetCameraToViewAll();
rp3d::PhysicsWorld::WorldSettings worldSettings;
worldSettings.worldName = name;
// Create the physics world for the physics simulation
mPhysicsWorld = mPhysicsCommon.createPhysicsWorld(worldSettings);
// ---------- Sphere 1 ---------- //
// Create a sphere and a corresponding collision body in the physics world
mSphere1 = new Sphere(rp3d::BodyType::STATIC, false, 4, mPhysicsCommon, mPhysicsWorld, mMeshFolderPath);
mAllShapes.push_back(mSphere1);
// Set the color
mSphere1->setColor(mObjectColorDemo);
mSphere1->setSleepingColor(mSleepingColorDemo);
//mSphere1->setScaling(0.5f);
mPhysicsObjects.push_back(mSphere1);
// ---------- Sphere 2 ---------- //
// Create a sphere and a corresponding collision body in the physics world
mSphere2 = new Sphere(rp3d::BodyType::STATIC, false, 2, mPhysicsCommon, mPhysicsWorld, mMeshFolderPath);
mAllShapes.push_back(mSphere2);
// Set the color
mSphere2->setColor(mObjectColorDemo);
mSphere2->setSleepingColor(mSleepingColorDemo);
mPhysicsObjects.push_back(mSphere2);
// ---------- Capsule 1 ---------- //
// Create a cylinder and a corresponding collision body in the physics world
mCapsule1 = new Capsule(rp3d::BodyType::STATIC, false, CAPSULE_RADIUS, CAPSULE_HEIGHT, mPhysicsCommon, mPhysicsWorld, mMeshFolderPath);
mAllShapes.push_back(mCapsule1);
// Set the color
mCapsule1->setColor(mObjectColorDemo);
mCapsule1->setSleepingColor(mSleepingColorDemo);
mPhysicsObjects.push_back(mCapsule1);
// ---------- Capsule 2 ---------- //
// Create a cylinder and a corresponding collision body in the physics world
mCapsule2 = new Capsule(rp3d::BodyType::STATIC, false, CAPSULE_RADIUS, CAPSULE_HEIGHT, mPhysicsCommon, mPhysicsWorld, mMeshFolderPath);
mAllShapes.push_back(mCapsule2);
// Set the color
mCapsule2->setColor(mObjectColorDemo);
mCapsule2->setSleepingColor(mSleepingColorDemo);
mPhysicsObjects.push_back(mCapsule2);
// ---------- Concave Mesh ---------- //
// Create a convex mesh and a corresponding collision body in the physics world
mConcaveMesh = new ConcaveMesh(rp3d::BodyType::STATIC, false, mPhysicsCommon, mPhysicsWorld, ResourceManager::getMeshPath("castle.obj"), rp3d::Vector3(0.3, 0.3, 0.3));
mAllShapes.push_back(mConcaveMesh);
// Set the color
mConcaveMesh->setColor(mObjectColorDemo);
mConcaveMesh->setSleepingColor(mSleepingColorDemo);
mPhysicsObjects.push_back(mConcaveMesh);
// ---------- Box 1 ---------- //
// Create a cylinder and a corresponding collision body in the physics world
mBox1 = new Box(rp3d::BodyType::STATIC, false, BOX_SIZE, mPhysicsCommon, mPhysicsWorld, mMeshFolderPath);
mAllShapes.push_back(mBox1);
// Set the color
mBox1->setColor(mObjectColorDemo);
mBox1->setSleepingColor(mSleepingColorDemo);
mPhysicsObjects.push_back(mBox1);
// ---------- Box 2 ---------- //
// Create a cylinder and a corresponding collision body in the physics world
mBox2 = new Box(rp3d::BodyType::STATIC, false, openglframework::Vector3(3, 2, 5), mPhysicsCommon, mPhysicsWorld, mMeshFolderPath);
mAllShapes.push_back(mBox2);
// Set the color
mBox2->setColor(mObjectColorDemo);
mBox2->setSleepingColor(mSleepingColorDemo);
mPhysicsObjects.push_back(mBox2);
// ---------- Convex Mesh ---------- //
// Create a convex mesh and a corresponding collision body in the physics world
mConvexMesh = new ConvexMesh(rp3d::BodyType::STATIC, false, mPhysicsCommon, mPhysicsWorld, ResourceManager::getMeshPath("convexmesh.obj"));
mAllShapes.push_back(mConvexMesh);
// Set the color
mConvexMesh->setColor(mObjectColorDemo);
mConvexMesh->setSleepingColor(mSleepingColorDemo);
mPhysicsObjects.push_back(mConvexMesh);
// ---------- Heightfield ---------- //
// Create a convex mesh and a corresponding collision body in the physics world
mHeightField = new HeightField(rp3d::BodyType::STATIC, false, mPhysicsCommon, mPhysicsWorld);
// Set the color
mHeightField->setColor(mObjectColorDemo);
mHeightField->setSleepingColor(mSleepingColorDemo);
mPhysicsObjects.push_back(mHeightField);
mAllShapes[mSelectedShapeIndex]->setColor(mObjectColorDemo);
}
// Reset the scene
void CollisionDetectionScene::reset() {
SceneDemo::reset();
mSphere1->setTransform(rp3d::Transform(rp3d::Vector3(15, 5, 0), rp3d::Quaternion::identity()));
mSphere2->setTransform(rp3d::Transform(rp3d::Vector3(0, 6, 0), rp3d::Quaternion::identity()));
mCapsule1->setTransform(rp3d::Transform(rp3d::Vector3(-8, 7, 0), rp3d::Quaternion::identity()));
mCapsule2->setTransform(rp3d::Transform(rp3d::Vector3(11, -8, 0), rp3d::Quaternion::identity()));
mBox1->setTransform(rp3d::Transform(rp3d::Vector3(-4, -7, 0), rp3d::Quaternion::identity()));
mBox2->setTransform(rp3d::Transform(rp3d::Vector3(0, 9, 0), rp3d::Quaternion::identity()));
mConvexMesh->setTransform(rp3d::Transform(rp3d::Vector3(-5, 0, 0), rp3d::Quaternion::identity()));
mConcaveMesh->setTransform(rp3d::Transform(rp3d::Vector3(0, 15, 0), rp3d::Quaternion::identity()));
mHeightField->setTransform(rp3d::Transform(rp3d::Vector3(0, -22, 0), rp3d::Quaternion::identity()));
}
// Destructor
CollisionDetectionScene::~CollisionDetectionScene() {
delete mSphere1;
delete mSphere2;
delete mCapsule1;
delete mCapsule2;
delete mBox1;
delete mBox2;
delete mConvexMesh;
delete mConcaveMesh;
delete mHeightField;
// Destroy the static data for the visual contact points
VisualContactPoint::destroyStaticData();
// Destroy the physics world
mPhysicsCommon.destroyPhysicsWorld(mPhysicsWorld);
}
// Take a step for the simulation
void CollisionDetectionScene::update() {
// Compute debug rendering primitives
mPhysicsWorld->getDebugRenderer().reset();
mPhysicsWorld->getDebugRenderer().computeDebugRenderingPrimitives(*mPhysicsWorld);
mSnapshotsContactPoints.clear();
mPhysicsWorld->testCollision(mContactManager);
SceneDemo::update();
}
void CollisionDetectionScene::selectNextShape() {
uint previousIndex = mSelectedShapeIndex;
mSelectedShapeIndex++;
if (mSelectedShapeIndex >= mAllShapes.size()) {
mSelectedShapeIndex = 0;
}
mAllShapes[previousIndex]->setColor(mObjectColorDemo);
mAllShapes[mSelectedShapeIndex]->setColor(mSelectedObjectColorDemo);
}
// Called when a keyboard event occurs
bool CollisionDetectionScene::keyboardEvent(int key, int /*scancode*/, int action, int /*mods*/) {
// If the space key has been pressed
if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) {
selectNextShape();
return true;
}
float stepDist = 0.2f;
float stepAngle = 15 * (3.14f / 180.0f);
if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapes[mSelectedShapeIndex]->getTransform();
transform.setPosition(transform.getPosition() + rp3d::Vector3(stepDist, 0, 0));
mAllShapes[mSelectedShapeIndex]->setTransform(transform);
}
else if (key == GLFW_KEY_LEFT && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapes[mSelectedShapeIndex]->getTransform();
transform.setPosition(transform.getPosition() + rp3d::Vector3(-stepDist, 0, 0));
mAllShapes[mSelectedShapeIndex]->setTransform(transform);
}
else if (key == GLFW_KEY_UP && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapes[mSelectedShapeIndex]->getTransform();
transform.setPosition(transform.getPosition() + rp3d::Vector3(0, stepDist, 0));
mAllShapes[mSelectedShapeIndex]->setTransform(transform);
}
else if (key == GLFW_KEY_DOWN && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapes[mSelectedShapeIndex]->getTransform();
transform.setPosition(transform.getPosition() + rp3d::Vector3(0, -stepDist, 0));
mAllShapes[mSelectedShapeIndex]->setTransform(transform);
}
else if (key == GLFW_KEY_Z && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapes[mSelectedShapeIndex]->getTransform();
transform.setPosition(transform.getPosition() + rp3d::Vector3(0, 0, stepDist));
mAllShapes[mSelectedShapeIndex]->setTransform(transform);
}
else if (key == GLFW_KEY_H && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapes[mSelectedShapeIndex]->getTransform();
transform.setPosition(transform.getPosition() + rp3d::Vector3(0, 0, -stepDist));
mAllShapes[mSelectedShapeIndex]->setTransform(transform);
}
else if (key == GLFW_KEY_A && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapes[mSelectedShapeIndex]->getTransform();
transform.setOrientation(rp3d::Quaternion::fromEulerAngles(0, stepAngle, 0) * transform.getOrientation());
mAllShapes[mSelectedShapeIndex]->setTransform(transform);
}
else if (key == GLFW_KEY_D && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapes[mSelectedShapeIndex]->getTransform();
transform.setOrientation(rp3d::Quaternion::fromEulerAngles(0, -stepAngle, 0) * transform.getOrientation());
mAllShapes[mSelectedShapeIndex]->setTransform(transform);
}
else if (key == GLFW_KEY_W && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapes[mSelectedShapeIndex]->getTransform();
transform.setOrientation(rp3d::Quaternion::fromEulerAngles(stepAngle, 0, 0) * transform.getOrientation());
mAllShapes[mSelectedShapeIndex]->setTransform(transform);
}
else if (key == GLFW_KEY_S && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapes[mSelectedShapeIndex]->getTransform();
transform.setOrientation(rp3d::Quaternion::fromEulerAngles(-stepAngle, 0, 0) * transform.getOrientation());
mAllShapes[mSelectedShapeIndex]->setTransform(transform);
}
else if (key == GLFW_KEY_F && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapes[mSelectedShapeIndex]->getTransform();
transform.setOrientation(rp3d::Quaternion::fromEulerAngles(0, 0, stepAngle) * transform.getOrientation());
mAllShapes[mSelectedShapeIndex]->setTransform(transform);
}
else if (key == GLFW_KEY_G && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapes[mSelectedShapeIndex]->getTransform();
transform.setOrientation(rp3d::Quaternion::fromEulerAngles(0, 0, -stepAngle) * transform.getOrientation());
mAllShapes[mSelectedShapeIndex]->setTransform(transform);
}
return false;
}
// This method is called when some contacts occur
void ContactManager::onContact(const CallbackData& callbackData) {
// For each contact pair
for (uint p=0; p < callbackData.getNbContactPairs(); p++) {
ContactPair contactPair = callbackData.getContactPair(p);
// For each contact point of the contact pair
for (uint c=0; c < contactPair.getNbContactPoints(); c++) {
ContactPoint contactPoint = contactPair.getContactPoint(c);
// Contact normal
rp3d::Vector3 normal = contactPoint.getWorldNormal();
openglframework::Vector3 contactNormal(normal.x, normal.y, normal.z);
rp3d::Vector3 point1 = contactPoint.getLocalPointOnCollider1();
point1 = contactPair.getCollider1()->getLocalToWorldTransform() * point1;
openglframework::Vector3 position1(point1.x, point1.y, point1.z);
mContactPoints.push_back(SceneContactPoint(position1, contactNormal, openglframework::Color::red()));
rp3d::Vector3 point2 = contactPoint.getLocalPointOnCollider2();
point2 = contactPair.getCollider2()->getLocalToWorldTransform() * point2;
openglframework::Vector3 position2(point2.x, point2.y, point2.z);
mContactPoints.push_back(SceneContactPoint(position2, contactNormal, openglframework::Color::blue()));
}
}
}