Working on collision detection scene

This commit is contained in:
Daniel Chappuis 2017-01-28 15:23:05 +01:00
parent 0b0975769b
commit adeac94506
9 changed files with 96 additions and 49 deletions

View File

@ -69,7 +69,7 @@ openglframework::Matrix4 PhysicsObject::computeTransform(float interpolationFact
} }
// Reset the transform // Reset the transform
void PhysicsObject::resetTransform(const rp3d::Transform& transform) { void PhysicsObject::setTransform(const rp3d::Transform& transform) {
// Reset the transform // Reset the transform
mBody->setTransform(transform); mBody->setTransform(transform);

View File

@ -68,8 +68,11 @@ class PhysicsObject : public openglframework::Mesh {
/// Set the sleeping color of the box /// Set the sleeping color of the box
void setSleepingColor(const openglframework::Color& color); void setSleepingColor(const openglframework::Color& color);
/// Set the position of the box /// Get the transform
void resetTransform(const rp3d::Transform& transform); const rp3d::Transform& getTransform() const;
/// Set the transform
void setTransform(const rp3d::Transform& transform);
/// Return a pointer to the collision body of the box /// Return a pointer to the collision body of the box
reactphysics3d::CollisionBody* getCollisionBody(); reactphysics3d::CollisionBody* getCollisionBody();
@ -91,6 +94,11 @@ inline void PhysicsObject::setSleepingColor(const openglframework::Color& color)
mSleepingColor = color; mSleepingColor = color;
} }
// Get the transform
inline const rp3d::Transform& PhysicsObject::getTransform() const {
return mBody->getTransform();
}
// Return a pointer to the collision body of the box // Return a pointer to the collision body of the box
inline rp3d::CollisionBody* PhysicsObject::getCollisionBody() { inline rp3d::CollisionBody* PhysicsObject::getCollisionBody() {
return mBody; return mBody;

View File

@ -54,8 +54,7 @@ CollisionDetectionScene::CollisionDetectionScene(const std::string& name)
// Create a sphere and a corresponding collision body in the dynamics world // Create a sphere and a corresponding collision body in the dynamics world
mSphere1 = new Sphere(6, position1, mCollisionWorld, mMeshFolderPath); mSphere1 = new Sphere(6, position1, mCollisionWorld, mMeshFolderPath);
mAllShapesObjects.push_back(mSphere1); mAllShapes.push_back(mSphere1);
mAllShapesPhysicsObjects.push_back(mSphere1);
// Set the color // Set the color
mSphere1->setColor(mGreyColorDemo); mSphere1->setColor(mGreyColorDemo);
@ -66,8 +65,7 @@ CollisionDetectionScene::CollisionDetectionScene(const std::string& name)
// Create a sphere and a corresponding collision body in the dynamics world // Create a sphere and a corresponding collision body in the dynamics world
mSphere2 = new Sphere(4, position2, mCollisionWorld, mMeshFolderPath); mSphere2 = new Sphere(4, position2, mCollisionWorld, mMeshFolderPath);
mAllShapesObjects.push_back(mSphere2); mAllShapes.push_back(mSphere2);
mAllShapesPhysicsObjects.push_back(mSphere2);
// Set the color // Set the color
mSphere2->setColor(mGreyColorDemo); mSphere2->setColor(mGreyColorDemo);
@ -139,7 +137,7 @@ CollisionDetectionScene::CollisionDetectionScene(const std::string& name)
// Create the VBO and VAO to render the lines // Create the VBO and VAO to render the lines
createVBOAndVAO(mPhongShader); createVBOAndVAO(mPhongShader);
mAllShapesPhysicsObjects[mSelectedShapeIndex]->setColor(mBlueColorDemo); mAllShapes[mSelectedShapeIndex]->setColor(mBlueColorDemo);
} }
// Reset the scene // Reset the scene
@ -332,12 +330,12 @@ void CollisionDetectionScene::selectNextShape() {
int previousIndex = mSelectedShapeIndex; int previousIndex = mSelectedShapeIndex;
mSelectedShapeIndex++; mSelectedShapeIndex++;
if (mSelectedShapeIndex >= mAllShapesPhysicsObjects.size()) { if (mSelectedShapeIndex >= mAllShapes.size()) {
mSelectedShapeIndex = 0; mSelectedShapeIndex = 0;
} }
mAllShapesPhysicsObjects[previousIndex]->setColor(mGreyColorDemo); mAllShapes[previousIndex]->setColor(mGreyColorDemo);
mAllShapesPhysicsObjects[mSelectedShapeIndex]->setColor(mBlueColorDemo); mAllShapes[mSelectedShapeIndex]->setColor(mBlueColorDemo);
} }
// Called when a keyboard event occurs // Called when a keyboard event occurs
@ -349,10 +347,68 @@ bool CollisionDetectionScene::keyboardEvent(int key, int scancode, int action, i
return true; return true;
} }
float stepDist = 0.5f;
float stepAngle = 20 * (3.14f / 180.0f);
if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS) { if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapesPhysicsObjects[mSelectedShapeIndex]->getCollisionBody()->getTransform(); rp3d::Transform transform = mAllShapes[mSelectedShapeIndex]->getTransform();
transform.setPosition(transform.getPosition() + rp3d::Vector3(1, 0, 0)); transform.setPosition(transform.getPosition() + rp3d::Vector3(stepDist, 0, 0));
mAllShapesObjects[mSelectedShapeIndex]->translateWorld(openglframework::Vector3(1, 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; return false;

View File

@ -146,8 +146,7 @@ class CollisionDetectionScene : public SceneDemo {
//ConcaveMesh* mConcaveMesh; //ConcaveMesh* mConcaveMesh;
//HeightField* mHeightField; //HeightField* mHeightField;
std::vector<openglframework::Object3D*> mAllShapesObjects; std::vector<PhysicsObject*> mAllShapes;
std::vector<PhysicsObject*> mAllShapesPhysicsObjects;
int mSelectedShapeIndex; int mSelectedShapeIndex;

View File

@ -524,7 +524,7 @@ void CollisionShapesScene::reset() {
rp3d::Transform transform(initPosition, initOrientation); rp3d::Transform transform(initPosition, initOrientation);
// Reset the transform // Reset the transform
mDumbbells[i]->resetTransform(transform); mDumbbells[i]->setTransform(transform);
} }
// Create all the boxes of the scene // Create all the boxes of the scene
@ -542,7 +542,7 @@ void CollisionShapesScene::reset() {
rp3d::Transform transform(initPosition, initOrientation); rp3d::Transform transform(initPosition, initOrientation);
// Reset the transform // Reset the transform
mBoxes[i]->resetTransform(transform); mBoxes[i]->setTransform(transform);
} }
// Create all the spheres of the scene // Create all the spheres of the scene
@ -560,7 +560,7 @@ void CollisionShapesScene::reset() {
rp3d::Transform transform(initPosition, initOrientation); rp3d::Transform transform(initPosition, initOrientation);
// Reset the transform // Reset the transform
mSpheres[i]->resetTransform(transform); mSpheres[i]->setTransform(transform);
} }
// Create all the cones of the scene // Create all the cones of the scene
@ -578,7 +578,7 @@ void CollisionShapesScene::reset() {
rp3d::Transform transform(initPosition, initOrientation); rp3d::Transform transform(initPosition, initOrientation);
// Reset the transform // Reset the transform
mCones[i]->resetTransform(transform); mCones[i]->setTransform(transform);
} }
// Create all the cylinders of the scene // Create all the cylinders of the scene
@ -596,7 +596,7 @@ void CollisionShapesScene::reset() {
rp3d::Transform transform(initPosition, initOrientation); rp3d::Transform transform(initPosition, initOrientation);
// Reset the transform // Reset the transform
mCylinders[i]->resetTransform(transform); mCylinders[i]->setTransform(transform);
} }
// Create all the capsules of the scene // Create all the capsules of the scene
@ -614,7 +614,7 @@ void CollisionShapesScene::reset() {
rp3d::Transform transform(initPosition, initOrientation); rp3d::Transform transform(initPosition, initOrientation);
// Reset the transform // Reset the transform
mCapsules[i]->resetTransform(transform); mCapsules[i]->setTransform(transform);
} }
// Create all the convex meshes of the scene // Create all the convex meshes of the scene
@ -632,6 +632,6 @@ void CollisionShapesScene::reset() {
rp3d::Transform transform(initPosition, initOrientation); rp3d::Transform transform(initPosition, initOrientation);
// Reset the transform // Reset the transform
mConvexMeshes[i]->resetTransform(transform); mConvexMeshes[i]->setTransform(transform);
} }
} }

View File

@ -175,7 +175,7 @@ void ConcaveMeshScene::reset() {
// Reset the transform // Reset the transform
rp3d::Transform transform(rp3d::Vector3::zero(), rp3d::Quaternion::identity()); rp3d::Transform transform(rp3d::Vector3::zero(), rp3d::Quaternion::identity());
mConcaveMesh->resetTransform(transform); mConcaveMesh->setTransform(transform);
for (int i=0; i<NB_BOXES_X; i++) { for (int i=0; i<NB_BOXES_X; i++) {
for (int j=0; j<NB_BOXES_Z; j++) { for (int j=0; j<NB_BOXES_Z; j++) {
@ -184,7 +184,7 @@ void ConcaveMeshScene::reset() {
rp3d::Vector3 boxPosition(-NB_BOXES_X * BOX_SIZE * BOXES_SPACE / 2 + i * BOX_SIZE * BOXES_SPACE, 30, -NB_BOXES_Z * BOX_SIZE * BOXES_SPACE / 2 + j * BOX_SIZE * BOXES_SPACE); rp3d::Vector3 boxPosition(-NB_BOXES_X * BOX_SIZE * BOXES_SPACE / 2 + i * BOX_SIZE * BOXES_SPACE, 30, -NB_BOXES_Z * BOX_SIZE * BOXES_SPACE / 2 + j * BOX_SIZE * BOXES_SPACE);
rp3d::Transform boxTransform(boxPosition, rp3d::Quaternion::identity()); rp3d::Transform boxTransform(boxPosition, rp3d::Quaternion::identity());
mBoxes[i * NB_BOXES_Z + j]->resetTransform(boxTransform); mBoxes[i * NB_BOXES_Z + j]->setTransform(boxTransform);
} }
} }

View File

@ -171,13 +171,13 @@ void HeightFieldScene::reset() {
// Reset the transform // Reset the transform
rp3d::Transform transform(rp3d::Vector3(0, 0, 0), rp3d::Quaternion::identity()); rp3d::Transform transform(rp3d::Vector3(0, 0, 0), rp3d::Quaternion::identity());
mHeightField->resetTransform(transform); mHeightField->setTransform(transform);
float heightFieldWidth = 10.0f; float heightFieldWidth = 10.0f;
float stepDist = heightFieldWidth / (NB_BOXES + 1); float stepDist = heightFieldWidth / (NB_BOXES + 1);
for (int i=0; i<NB_BOXES; i++) { for (int i=0; i<NB_BOXES; i++) {
rp3d::Vector3 boxPosition(-heightFieldWidth * 0.5f + i * stepDist , 14 + 6.0f * i, -heightFieldWidth * 0.5f + i * stepDist); rp3d::Vector3 boxPosition(-heightFieldWidth * 0.5f + i * stepDist , 14 + 6.0f * i, -heightFieldWidth * 0.5f + i * stepDist);
rp3d::Transform boxTransform(boxPosition, rp3d::Quaternion::identity()); rp3d::Transform boxTransform(boxPosition, rp3d::Quaternion::identity());
mBoxes[i]->resetTransform(boxTransform); mBoxes[i]->setTransform(boxTransform);
} }
} }

View File

@ -193,7 +193,7 @@ void JointsScene::reset() {
rp3d::Transform transform(initPosition, initOrientation); rp3d::Transform transform(initPosition, initOrientation);
// Create a box and a corresponding rigid in the dynamics world // Create a box and a corresponding rigid in the dynamics world
mBallAndSocketJointChainBoxes[i]->resetTransform(transform); mBallAndSocketJointChainBoxes[i]->setTransform(transform);
positionBox.y -= boxDimension.y + 0.5f; positionBox.y -= boxDimension.y + 0.5f;
} }
@ -207,7 +207,7 @@ void JointsScene::reset() {
rp3d::Transform transformBottomBox(initPosition, initOrientation); rp3d::Transform transformBottomBox(initPosition, initOrientation);
// Create a box and a corresponding rigid in the dynamics world // Create a box and a corresponding rigid in the dynamics world
mSliderJointBottomBox->resetTransform(transformBottomBox); mSliderJointBottomBox->setTransform(transformBottomBox);
// Position of the box // Position of the box
openglframework::Vector3 positionBox2(0, 4.2f, 0); openglframework::Vector3 positionBox2(0, 4.2f, 0);
@ -216,7 +216,7 @@ void JointsScene::reset() {
rp3d::Transform transformTopBox(initPosition, initOrientation); rp3d::Transform transformTopBox(initPosition, initOrientation);
// Create a box and a corresponding rigid in the dynamics world // Create a box and a corresponding rigid in the dynamics world
mSliderJointTopBox->resetTransform(transformTopBox); mSliderJointTopBox->setTransform(transformTopBox);
// --------------- Propeller Hinge joint --------------- // // --------------- Propeller Hinge joint --------------- //
@ -227,7 +227,7 @@ void JointsScene::reset() {
rp3d::Transform transformHingeBox(initPosition, initOrientation); rp3d::Transform transformHingeBox(initPosition, initOrientation);
// Create a box and a corresponding rigid in the dynamics world // Create a box and a corresponding rigid in the dynamics world
mPropellerBox->resetTransform(transformHingeBox); mPropellerBox->setTransform(transformHingeBox);
// --------------- Fixed joint --------------- // // --------------- Fixed joint --------------- //
@ -238,7 +238,7 @@ void JointsScene::reset() {
rp3d::Transform transformFixedBox1(initPosition, initOrientation); rp3d::Transform transformFixedBox1(initPosition, initOrientation);
// Create a box and a corresponding rigid in the dynamics world // Create a box and a corresponding rigid in the dynamics world
mFixedJointBox1->resetTransform(transformFixedBox1); mFixedJointBox1->setTransform(transformFixedBox1);
// Position of the box // Position of the box
positionBox2 = openglframework::Vector3(-5, 7, 0); positionBox2 = openglframework::Vector3(-5, 7, 0);
@ -247,7 +247,7 @@ void JointsScene::reset() {
rp3d::Transform transformFixedBox2(initPosition, initOrientation); rp3d::Transform transformFixedBox2(initPosition, initOrientation);
// Create a box and a corresponding rigid in the dynamics world // Create a box and a corresponding rigid in the dynamics world
mFixedJointBox2->resetTransform(transformFixedBox2); mFixedJointBox2->setTransform(transformFixedBox2);
} }
// Create the boxes and joints for the Ball-and-Socket joint example // Create the boxes and joints for the Ball-and-Socket joint example

View File

@ -162,12 +162,6 @@ class TestbedApplication : public Screen {
/// Set the variable to know if we need to take a single physics step /// Set the variable to know if we need to take a single physics step
void toggleTakeSinglePhysicsStep(); void toggleTakeSinglePhysicsStep();
/// Enable/Disable shadow mapping
void enableShadows(bool enable);
/// Display/Hide contact points
void displayContactPoints(bool display);
public : public :
// -------------------- Methods -------------------- // // -------------------- Methods -------------------- //
@ -252,16 +246,6 @@ inline void TestbedApplication::toggleTakeSinglePhysicsStep() {
} }
} }
// Enable/Disable shadow mapping
inline void TestbedApplication::enableShadows(bool enable) {
mIsShadowMappingEnabled = enable;
}
/// Display/Hide contact points
inline void TestbedApplication::displayContactPoints(bool display) {
mIsContactPointsDisplayed = display;
}
// Enable/Disable Vertical synchronization // Enable/Disable Vertical synchronization
inline void TestbedApplication::enableVSync(bool enable) { inline void TestbedApplication::enableVSync(bool enable) {
mIsVSyncEnabled = enable; mIsVSyncEnabled = enable;