DanielChappuis / reactphysics3d

Open source C++ physics engine library in 3D
http://www.reactphysics3d.com
zlib License
1.54k stars 223 forks source link

Crash when using Trigger colliders in debug build #237

Closed MrLetsplay2003 closed 2 years ago

MrLetsplay2003 commented 2 years ago

I wanted to use a RigidBody with a trigger collider in my project, but whenever I create a dynamic body and call myCollider->setIsTrigger(true), the program crashes on collision with another object (normal collisions without the setIsTrigger call work fine).

I've created the following simple project to demonstrate this problem:

test.cpp:

#include <iostream>
#include <reactphysics3d/reactphysics3d.h>

using namespace reactphysics3d;

class TestListener: public EventListener {

    void onTrigger(const OverlapCallback::CallbackData& callbackData) {
        std::cout << "Trigger callback" << std::endl;
    }

};

int main() {
    PhysicsCommon *common = new PhysicsCommon();
    PhysicsWorld::WorldSettings settings;
    PhysicsWorld *world = common->createPhysicsWorld(settings);

    RigidBody *body1 = world->createRigidBody(Transform(Vector3(0.0, 0.0, 0.0), Quaternion::identity()));
    body1->setType(BodyType::STATIC);
    BoxShape *boxShape = common->createBoxShape(Vector3(100.0, 0.5, 100.0));
    body1->addCollider(boxShape, Transform::identity());

    RigidBody *body2 = world->createRigidBody(Transform(Vector3(0.0, 5.0, 0.0), Quaternion::identity()));
    body2->setType(BodyType::DYNAMIC);
    CapsuleShape *capShape = common->createCapsuleShape(1, 2);
    Collider *c = body2->addCollider(capShape, Transform::identity());
    c->setIsTrigger(true);

    world->setEventListener(new TestListener());

    while(true) {
        std::cout << body2->getTransform().to_string() << std::endl;
        world->update(1.0f / 20);
    }
}

I'm using the newest version in the master branch (at commit 4bbbaa7) I'm compiling the program + reactphysics3d on Linux using CMake and a very simple CMakeLists.txt.

This issue only occurs when compiling the binary as a debug build (configured using cmake -DCMAKE_BUILD_TYPE=Debug ..), but works fine when compiled as a normal release build.

CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)

project(Test VERSION 1.0)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

add_subdirectory(dependencies/reactphysics3d) # Contains the cloned source from GitHub

add_compile_options(-Wall)

add_executable(test src/test.cpp)
target_link_libraries(test PUBLIC reactphysics3d)
set(CMAKE_VERBOSE_MAKEFILE on)

The output of the program before crashing is:

Transform(Vector3(0.000000,5.000000,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,4.975475,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,4.926425,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,4.852850,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,4.754750,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,4.632125,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,4.484975,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,4.313300,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,4.117100,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,3.896375,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,3.651125,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,3.381350,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,3.087050,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,2.768225,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
Transform(Vector3(0.000000,2.424875,0.000000),Quaternion(0.000000,0.000000,0.000000,1.000000))
test: /path/to/the/project/dependencies/reactphysics3d/src/systems/CollisionDetectionSystem.cpp:861: void reactphysics3d::CollisionDetectionSystem::createContacts(): Assertion `mWorld->mProcessContactPairsOrderIslands.size() == (*mCurrentContactPairs).size()' failed.
Aborted

Is this a bug or am I just doing something wrong when creating the bodies?

DanielChappuis commented 2 years ago

Thanks a lot for reporting this issue with a lot of details.

Indeed this assert is wrong when a collider is a trigger. I have removed this assert in this commit in the master branch.