2017-02-02 22:10:01 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* 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"
|
|
|
|
|
|
|
|
// Namespaces
|
|
|
|
using namespace openglframework;
|
|
|
|
using namespace collisiondetectionscene;
|
|
|
|
|
|
|
|
// Constructor
|
2017-11-02 21:58:41 +00:00
|
|
|
CollisionDetectionScene::CollisionDetectionScene(const std::string& name, EngineSettings& settings)
|
|
|
|
: SceneDemo(name, settings, SCENE_RADIUS, false), mMeshFolderPath("meshes/"),
|
2017-02-02 22:10:01 +00:00
|
|
|
mContactManager(mPhongShader, mMeshFolderPath),
|
2017-05-17 21:40:17 +00:00
|
|
|
mAreNormalsDisplayed(false) {
|
2017-02-02 22:10:01 +00:00
|
|
|
|
|
|
|
mSelectedShapeIndex = 0;
|
|
|
|
mIsContactPointsDisplayed = true;
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Create the dynamics world for the physics simulation
|
2017-11-02 21:58:41 +00:00
|
|
|
mPhysicsWorld = new rp3d::CollisionWorld();
|
2017-02-02 22:10:01 +00:00
|
|
|
|
2017-11-10 18:57:50 +00:00
|
|
|
#ifdef IS_PROFILING_ACTIVE
|
|
|
|
|
|
|
|
mPhysicsWorld->setProfilerName(name + "_profiler");
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2017-02-02 22:10:01 +00:00
|
|
|
// ---------- Sphere 1 ---------- //
|
|
|
|
|
|
|
|
// Create a sphere and a corresponding collision body in the dynamics world
|
2017-11-02 21:58:41 +00:00
|
|
|
mSphere1 = new Sphere(4, mPhysicsWorld, mMeshFolderPath);
|
2017-02-02 22:10:01 +00:00
|
|
|
mAllShapes.push_back(mSphere1);
|
|
|
|
|
|
|
|
// Set the color
|
|
|
|
mSphere1->setColor(mGreyColorDemo);
|
|
|
|
mSphere1->setSleepingColor(mRedColorDemo);
|
2017-11-02 21:58:41 +00:00
|
|
|
//mSphere1->setScaling(0.5f);
|
|
|
|
mPhysicsObjects.push_back(mSphere1);
|
2017-02-02 22:10:01 +00:00
|
|
|
|
|
|
|
// ---------- Sphere 2 ---------- //
|
|
|
|
|
|
|
|
// Create a sphere and a corresponding collision body in the dynamics world
|
2017-11-02 21:58:41 +00:00
|
|
|
mSphere2 = new Sphere(2, mPhysicsWorld, mMeshFolderPath);
|
2017-02-02 22:10:01 +00:00
|
|
|
mAllShapes.push_back(mSphere2);
|
|
|
|
|
|
|
|
// Set the color
|
|
|
|
mSphere2->setColor(mGreyColorDemo);
|
|
|
|
mSphere2->setSleepingColor(mRedColorDemo);
|
2017-11-02 21:58:41 +00:00
|
|
|
mPhysicsObjects.push_back(mSphere2);
|
2017-02-02 22:10:01 +00:00
|
|
|
|
2017-11-10 18:57:50 +00:00
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
// ---------- Capsule 1 ---------- //
|
2017-02-02 22:10:01 +00:00
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
// Create a cylinder and a corresponding collision body in the dynamics world
|
|
|
|
mCapsule1 = new Capsule(CAPSULE_RADIUS, CAPSULE_HEIGHT, mPhysicsWorld, mMeshFolderPath);
|
|
|
|
mAllShapes.push_back(mCapsule1);
|
2017-02-02 22:10:01 +00:00
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
// Set the color
|
|
|
|
mCapsule1->setColor(mGreyColorDemo);
|
|
|
|
mCapsule1->setSleepingColor(mRedColorDemo);
|
|
|
|
mPhysicsObjects.push_back(mCapsule1);
|
2017-02-02 22:10:01 +00:00
|
|
|
|
2017-02-27 18:06:28 +00:00
|
|
|
// ---------- Capsule 2 ---------- //
|
|
|
|
|
|
|
|
// Create a cylinder and a corresponding collision body in the dynamics world
|
2017-11-02 21:58:41 +00:00
|
|
|
mCapsule2 = new Capsule(CAPSULE_RADIUS, CAPSULE_HEIGHT, mPhysicsWorld, mMeshFolderPath);
|
2017-02-27 18:06:28 +00:00
|
|
|
mAllShapes.push_back(mCapsule2);
|
|
|
|
|
|
|
|
// Set the color
|
|
|
|
mCapsule2->setColor(mGreyColorDemo);
|
|
|
|
mCapsule2->setSleepingColor(mRedColorDemo);
|
2017-11-02 21:58:41 +00:00
|
|
|
mPhysicsObjects.push_back(mCapsule2);
|
2017-02-27 18:06:28 +00:00
|
|
|
|
2017-11-10 18:57:50 +00:00
|
|
|
// ---------- Concave Mesh ---------- //
|
|
|
|
|
|
|
|
// Create a convex mesh and a corresponding collision body in the dynamics world
|
|
|
|
mConcaveMesh = new ConcaveMesh(mPhysicsWorld, mMeshFolderPath + "city.obj");
|
|
|
|
mAllShapes.push_back(mConcaveMesh);
|
|
|
|
|
|
|
|
// Set the color
|
|
|
|
mConcaveMesh->setColor(mGreyColorDemo);
|
|
|
|
mConcaveMesh->setSleepingColor(mRedColorDemo);
|
|
|
|
mPhysicsObjects.push_back(mConcaveMesh);
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
// ---------- Box 1 ---------- //
|
2017-05-29 20:31:33 +00:00
|
|
|
|
2017-11-10 18:57:50 +00:00
|
|
|
// Create a cylinder and a corresponding collision body in the dynamics world
|
2017-11-02 21:58:41 +00:00
|
|
|
mBox1 = new Box(BOX_SIZE, mPhysicsWorld, mMeshFolderPath);
|
2017-11-10 18:57:50 +00:00
|
|
|
mAllShapes.push_back(mBox1);
|
2017-05-29 20:31:33 +00:00
|
|
|
|
2017-11-10 18:57:50 +00:00
|
|
|
// Set the color
|
|
|
|
mBox1->setColor(mGreyColorDemo);
|
|
|
|
mBox1->setSleepingColor(mRedColorDemo);
|
|
|
|
mPhysicsObjects.push_back(mBox1);
|
2017-05-29 20:31:33 +00:00
|
|
|
|
2017-11-10 18:57:50 +00:00
|
|
|
// ---------- Box 2 ---------- //
|
2017-05-29 20:31:33 +00:00
|
|
|
|
2017-11-10 18:57:50 +00:00
|
|
|
// Create a cylinder and a corresponding collision body in the dynamics world
|
2017-11-02 21:58:41 +00:00
|
|
|
mBox2 = new Box(openglframework::Vector3(3, 2, 5), mPhysicsWorld, mMeshFolderPath);
|
2017-11-10 18:57:50 +00:00
|
|
|
mAllShapes.push_back(mBox2);
|
2017-05-29 20:31:33 +00:00
|
|
|
|
2017-11-10 18:57:50 +00:00
|
|
|
// Set the color
|
|
|
|
mBox2->setColor(mGreyColorDemo);
|
|
|
|
mBox2->setSleepingColor(mRedColorDemo);
|
|
|
|
mPhysicsObjects.push_back(mBox2);
|
2017-05-29 20:31:33 +00:00
|
|
|
|
2017-02-02 22:10:01 +00:00
|
|
|
// ---------- Convex Mesh ---------- //
|
|
|
|
|
|
|
|
// Create a convex mesh and a corresponding collision body in the dynamics world
|
2017-11-02 21:58:41 +00:00
|
|
|
mConvexMesh = new ConvexMesh(mPhysicsWorld, mMeshFolderPath + "convexmesh.obj");
|
2017-11-10 18:57:50 +00:00
|
|
|
mAllShapes.push_back(mConvexMesh);
|
2017-02-02 22:10:01 +00:00
|
|
|
|
|
|
|
// Set the color
|
2017-08-18 15:51:10 +00:00
|
|
|
mConvexMesh->setColor(mGreyColorDemo);
|
|
|
|
mConvexMesh->setSleepingColor(mRedColorDemo);
|
2017-11-10 18:57:50 +00:00
|
|
|
mPhysicsObjects.push_back(mConvexMesh);
|
2017-02-02 22:10:01 +00:00
|
|
|
|
|
|
|
// ---------- Heightfield ---------- //
|
|
|
|
|
|
|
|
// Create a convex mesh and a corresponding collision body in the dynamics world
|
2017-11-02 21:58:41 +00:00
|
|
|
mHeightField = new HeightField(mPhysicsWorld);
|
2017-02-02 22:10:01 +00:00
|
|
|
|
|
|
|
// Set the color
|
2017-10-19 15:42:21 +00:00
|
|
|
mHeightField->setColor(mGreyColorDemo);
|
|
|
|
mHeightField->setSleepingColor(mRedColorDemo);
|
|
|
|
mPhysicsObjects.push_back(mHeightField);
|
2017-02-02 22:10:01 +00:00
|
|
|
|
|
|
|
mAllShapes[mSelectedShapeIndex]->setColor(mBlueColorDemo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset the scene
|
|
|
|
void CollisionDetectionScene::reset() {
|
|
|
|
|
2017-11-10 18:57:50 +00:00
|
|
|
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()));
|
2017-11-02 21:58:41 +00:00
|
|
|
mCapsule2->setTransform(rp3d::Transform(rp3d::Vector3(11, -8, 0), rp3d::Quaternion::identity()));
|
|
|
|
mBox1->setTransform(rp3d::Transform(rp3d::Vector3(-4, -7, 0), rp3d::Quaternion::identity()));
|
2017-11-10 18:57:50 +00:00
|
|
|
mBox2->setTransform(rp3d::Transform(rp3d::Vector3(0, 9, 0), rp3d::Quaternion::identity()));
|
2017-11-02 21:58:41 +00:00
|
|
|
mConvexMesh->setTransform(rp3d::Transform(rp3d::Vector3(-5, 0, 0), rp3d::Quaternion::identity()));
|
2017-11-10 18:57:50 +00:00
|
|
|
mConcaveMesh->setTransform(rp3d::Transform(rp3d::Vector3(0, 0, 0), rp3d::Quaternion::identity()));
|
2017-11-02 21:58:41 +00:00
|
|
|
mHeightField->setTransform(rp3d::Transform(rp3d::Vector3(0, -12, 0), rp3d::Quaternion::identity()));
|
2017-02-02 22:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destructor
|
|
|
|
CollisionDetectionScene::~CollisionDetectionScene() {
|
|
|
|
|
|
|
|
// Destroy the box rigid body from the dynamics world
|
2017-11-02 21:58:41 +00:00
|
|
|
//mPhysicsWorld->destroyCollisionBody(mBox->getCollisionBody());
|
2017-02-02 22:10:01 +00:00
|
|
|
//delete mBox;
|
|
|
|
|
|
|
|
// Destroy the spheres
|
2017-11-02 21:58:41 +00:00
|
|
|
mPhysicsWorld->destroyCollisionBody(mSphere1->getCollisionBody());
|
2017-02-02 22:10:01 +00:00
|
|
|
delete mSphere1;
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
mPhysicsWorld->destroyCollisionBody(mSphere2->getCollisionBody());
|
2017-02-02 22:10:01 +00:00
|
|
|
delete mSphere2;
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
mPhysicsWorld->destroyCollisionBody(mCapsule1->getCollisionBody());
|
2017-02-27 18:06:28 +00:00
|
|
|
delete mCapsule1;
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
mPhysicsWorld->destroyCollisionBody(mCapsule2->getCollisionBody());
|
2017-02-27 18:06:28 +00:00
|
|
|
delete mCapsule2;
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
mPhysicsWorld->destroyCollisionBody(mBox1->getCollisionBody());
|
2017-05-29 20:31:33 +00:00
|
|
|
delete mBox1;
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
mPhysicsWorld->destroyCollisionBody(mBox2->getCollisionBody());
|
2017-05-29 20:31:33 +00:00
|
|
|
delete mBox2;
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
mPhysicsWorld->destroyCollisionBody(mConvexMesh->getCollisionBody());
|
2017-08-18 15:51:10 +00:00
|
|
|
delete mConvexMesh;
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
mPhysicsWorld->destroyCollisionBody(mConcaveMesh->getCollisionBody());
|
2017-08-31 21:11:00 +00:00
|
|
|
delete mConcaveMesh;
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
mPhysicsWorld->destroyCollisionBody(mHeightField->getCollisionBody());
|
2017-10-19 15:42:21 +00:00
|
|
|
delete mHeightField;
|
|
|
|
|
2017-02-02 22:10:01 +00:00
|
|
|
mContactManager.resetPoints();
|
|
|
|
|
|
|
|
// Destroy the static data for the visual contact points
|
|
|
|
VisualContactPoint::destroyStaticData();
|
|
|
|
|
|
|
|
// Destroy the collision world
|
2017-11-02 21:58:41 +00:00
|
|
|
delete mPhysicsWorld;
|
2017-02-02 22:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Take a step for the simulation
|
|
|
|
void CollisionDetectionScene::update() {
|
|
|
|
|
|
|
|
mContactManager.resetPoints();
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
mPhysicsWorld->testCollision(&mContactManager);
|
2017-02-02 22:10:01 +00:00
|
|
|
|
|
|
|
SceneDemo::update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CollisionDetectionScene::selectNextShape() {
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
uint previousIndex = mSelectedShapeIndex;
|
2017-02-02 22:10:01 +00:00
|
|
|
mSelectedShapeIndex++;
|
|
|
|
if (mSelectedShapeIndex >= mAllShapes.size()) {
|
|
|
|
mSelectedShapeIndex = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mAllShapes[previousIndex]->setColor(mGreyColorDemo);
|
|
|
|
mAllShapes[mSelectedShapeIndex]->setColor(mBlueColorDemo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2017-05-29 20:31:33 +00:00
|
|
|
float stepDist = 0.2f;
|
|
|
|
float stepAngle = 15 * (3.14f / 180.0f);
|
2017-02-02 22:10:01 +00:00
|
|
|
|
|
|
|
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(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(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(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(-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(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(0, 0, -stepAngle) * transform.getOrientation());
|
|
|
|
mAllShapes[mSelectedShapeIndex]->setTransform(transform);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|