DanielChappuis / reactphysics3d

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

Fix bug that breaks transforming a Collider on a CollisionBody #274

Closed mhwdvs closed 10 months ago

mhwdvs commented 2 years ago

Hi there, I've been implementing CollisionBodys and Colliders in a game engine, but during testing I struggled to apply transformations to colliders. After calling setLocalToBodyTransform(), I would get the following error:

Assertion failed: mMapEntityToComponentIndex.containsKey(bodyEntity), file C:\(...)\reactphysics3d\include\reactphysics3d/components/RigidBodyComponents.h, line 418

That references this assertion:

// Return true if the body is sleeping
RP3D_FORCE_INLINE bool RigidBodyComponents::getIsSleeping(Entity bodyEntity) const {

    assert(mMapEntityToComponentIndex.containsKey(bodyEntity));

    return mIsSleeping[mMapEntityToComponentIndex[bodyEntity]];
}

I found it odd that I was hitting an assertion in a RigidBody method despite not utilising RigidBodys. I believe this is due to some undefined behavior, since static_cast isn't guaranteed to return nullptr if the body isn't a RigidBody, so it then proceeds to try to call some methods it probably shouldn't :D

Switching to dynamic_cast has fixed my issue.

DanielChappuis commented 2 years ago

Thanks a lot for taking the time to report to fix this. You are right the _staticcast seems wrong here.