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
void PhysicsObject::resetTransform(const rp3d::Transform& transform) {
void PhysicsObject::setTransform(const rp3d::Transform& transform) {
// Reset the transform
mBody->setTransform(transform);

View File

@ -68,8 +68,11 @@ class PhysicsObject : public openglframework::Mesh {
/// Set the sleeping color of the box
void setSleepingColor(const openglframework::Color& color);
/// Set the position of the box
void resetTransform(const rp3d::Transform& transform);
/// Get the 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
reactphysics3d::CollisionBody* getCollisionBody();
@ -91,6 +94,11 @@ inline void PhysicsObject::setSleepingColor(const openglframework::Color& 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
inline rp3d::CollisionBody* PhysicsObject::getCollisionBody() {
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
mSphere1 = new Sphere(6, position1, mCollisionWorld, mMeshFolderPath);
mAllShapesObjects.push_back(mSphere1);
mAllShapesPhysicsObjects.push_back(mSphere1);
mAllShapes.push_back(mSphere1);
// Set the color
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
mSphere2 = new Sphere(4, position2, mCollisionWorld, mMeshFolderPath);
mAllShapesObjects.push_back(mSphere2);
mAllShapesPhysicsObjects.push_back(mSphere2);
mAllShapes.push_back(mSphere2);
// Set the color
mSphere2->setColor(mGreyColorDemo);
@ -139,7 +137,7 @@ CollisionDetectionScene::CollisionDetectionScene(const std::string& name)
// Create the VBO and VAO to render the lines
createVBOAndVAO(mPhongShader);
mAllShapesPhysicsObjects[mSelectedShapeIndex]->setColor(mBlueColorDemo);
mAllShapes[mSelectedShapeIndex]->setColor(mBlueColorDemo);
}
// Reset the scene
@ -332,12 +330,12 @@ void CollisionDetectionScene::selectNextShape() {
int previousIndex = mSelectedShapeIndex;
mSelectedShapeIndex++;
if (mSelectedShapeIndex >= mAllShapesPhysicsObjects.size()) {
if (mSelectedShapeIndex >= mAllShapes.size()) {
mSelectedShapeIndex = 0;
}
mAllShapesPhysicsObjects[previousIndex]->setColor(mGreyColorDemo);
mAllShapesPhysicsObjects[mSelectedShapeIndex]->setColor(mBlueColorDemo);
mAllShapes[previousIndex]->setColor(mGreyColorDemo);
mAllShapes[mSelectedShapeIndex]->setColor(mBlueColorDemo);
}
// Called when a keyboard event occurs
@ -349,10 +347,68 @@ bool CollisionDetectionScene::keyboardEvent(int key, int scancode, int action, i
return true;
}
float stepDist = 0.5f;
float stepAngle = 20 * (3.14f / 180.0f);
if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS) {
rp3d::Transform transform = mAllShapesPhysicsObjects[mSelectedShapeIndex]->getCollisionBody()->getTransform();
transform.setPosition(transform.getPosition() + rp3d::Vector3(1, 0, 0));
mAllShapesObjects[mSelectedShapeIndex]->translateWorld(openglframework::Vector3(1, 0, 0));
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;

View File

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

View File

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

View File

@ -175,7 +175,7 @@ void ConcaveMeshScene::reset() {
// Reset the transform
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 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::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
rp3d::Transform transform(rp3d::Vector3(0, 0, 0), rp3d::Quaternion::identity());
mHeightField->resetTransform(transform);
mHeightField->setTransform(transform);
float heightFieldWidth = 10.0f;
float stepDist = heightFieldWidth / (NB_BOXES + 1);
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::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);
// 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;
}
@ -207,7 +207,7 @@ void JointsScene::reset() {
rp3d::Transform transformBottomBox(initPosition, initOrientation);
// Create a box and a corresponding rigid in the dynamics world
mSliderJointBottomBox->resetTransform(transformBottomBox);
mSliderJointBottomBox->setTransform(transformBottomBox);
// Position of the box
openglframework::Vector3 positionBox2(0, 4.2f, 0);
@ -216,7 +216,7 @@ void JointsScene::reset() {
rp3d::Transform transformTopBox(initPosition, initOrientation);
// Create a box and a corresponding rigid in the dynamics world
mSliderJointTopBox->resetTransform(transformTopBox);
mSliderJointTopBox->setTransform(transformTopBox);
// --------------- Propeller Hinge joint --------------- //
@ -227,7 +227,7 @@ void JointsScene::reset() {
rp3d::Transform transformHingeBox(initPosition, initOrientation);
// Create a box and a corresponding rigid in the dynamics world
mPropellerBox->resetTransform(transformHingeBox);
mPropellerBox->setTransform(transformHingeBox);
// --------------- Fixed joint --------------- //
@ -238,7 +238,7 @@ void JointsScene::reset() {
rp3d::Transform transformFixedBox1(initPosition, initOrientation);
// Create a box and a corresponding rigid in the dynamics world
mFixedJointBox1->resetTransform(transformFixedBox1);
mFixedJointBox1->setTransform(transformFixedBox1);
// Position of the box
positionBox2 = openglframework::Vector3(-5, 7, 0);
@ -247,7 +247,7 @@ void JointsScene::reset() {
rp3d::Transform transformFixedBox2(initPosition, initOrientation);
// 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

View File

@ -162,12 +162,6 @@ class TestbedApplication : public Screen {
/// Set the variable to know if we need to take a single physics step
void toggleTakeSinglePhysicsStep();
/// Enable/Disable shadow mapping
void enableShadows(bool enable);
/// Display/Hide contact points
void displayContactPoints(bool display);
public :
// -------------------- 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
inline void TestbedApplication::enableVSync(bool enable) {
mIsVSyncEnabled = enable;