DanielChappuis / reactphysics3d

Open source C++ physics engine library in 3D

Home Page:http://www.reactphysics3d.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Capsule vs triangle edge, no contact if edge parallel

programingman opened this issue · comments

Hi Daniel,
Thanks for all your work on this library, I'm really enjoying using it.

I'm just running in to an issue with capsule vs concave mesh. I use a capsule to find contacts for character collisions, but I get no contacts at corners of walls. as you can see in this example:
image

I've tried stepping through the code, and it seems like it if the capsule is parallel with the edge of the triangle it will make two initial contact points at the poles of the capsule, and then clip those points against the triangle, which results in 0 contact points.
I don't understand the GJK and SAT code well enough to come up with a solution sorry.

I've tried in both the release and now in develop branch.
Here's a modified version of "CollisionDetectionScene.cpp" from develop branch that demonstrates the issue. If you move the capsule so the center is no longer outside the clipping planes of the triangle, or if you rotate the capsule, it will find contact points.

/********************************************************************************
* 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 <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, SCENE_RADIUS, 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, mMeshFolderPath + "cube.obj", rp3d::Vector3(3.0, 3.0, 3.0));
    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, mMeshFolderPath + "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(3.1, 0.5, 3.1), 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, 0, 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()));
        }
    }
}

Thanks again!

Thanks a lot for reporting this. It seems to be a bug indeed. I will try to reproduce this on my side.

I have merged a fix for this issue into the 'develop' branch. This will be part of the next release of the library. Thanks a lot for reporting this issue and for your very complete code that helped me to reproduce it.

Thanks Daniel, nice work! I'm excited for the next release! 👍