bulletphysics / bullet3

Bullet Physics SDK: real-time collision detection and multi-physics simulation for VR, games, visual effects, robotics, machine learning etc.
http://bulletphysics.org
Other
12.65k stars 2.88k forks source link

btRaycastVehicle crash on assert fuzzyZero in btVector3 when updating friction #4437

Closed Hjaltesorgenfrei closed 1 year ago

Hjaltesorgenfrei commented 1 year ago

Hi, I get a crash on an assert when using btRaycastVehicle and running stepSimulation.

Crash happens at: https://github.com/bulletphysics/bullet3/blob/master/src/BulletDynamics/Vehicle/btRaycastVehicle.cpp#L533 when using the source code in the current master branch.

Not every

Callstack on crash:

vulkanologi.exe!btVector3::normalize() Line 305 (c:\code\Vulkanologi\third_party\bullet3\src\LinearMath\btVector3.h:305)
vulkanologi.exe!btRaycastVehicle::updateFriction(float timeStep) Line 533 (c:\code\Vulkanologi\third_party\bullet3\src\BulletDynamics\Vehicle\btRaycastVehicle.cpp:533)
vulkanologi.exe!btRaycastVehicle::updateVehicle(float step) Line 312 (c:\code\Vulkanologi\third_party\bullet3\src\BulletDynamics\Vehicle\btRaycastVehicle.cpp:312)
vulkanologi.exe!btRaycastVehicle::updateAction(btCollisionWorld * collisionWorld, float step) Line 82 (c:\code\Vulkanologi\third_party\bullet3\src\BulletDynamics\Vehicle\btRaycastVehicle.h:82)
vulkanologi.exe!btDiscreteDynamicsWorld::updateActions(float timeStep) Line 591 (c:\code\Vulkanologi\third_party\bullet3\src\BulletDynamics\Dynamics\btDiscreteDynamicsWorld.cpp:591)
vulkanologi.exe!btDiscreteDynamicsWorld::internalSingleStepSimulation(float timeStep) Line 491 (c:\code\Vulkanologi\third_party\bullet3\src\BulletDynamics\Dynamics\btDiscreteDynamicsWorld.cpp:491)
vulkanologi.exe!btDiscreteDynamicsWorld::stepSimulation(float timeStep, int maxSubSteps, float fixedTimeStep) Line 435 (c:\code\Vulkanologi\third_party\bullet3\src\BulletDynamics\Dynamics\btDiscreteDynamicsWorld.cpp:435)
vulkanologi.exe!PhysicsWorld::update(float dt) Line 46 (c:\code\Vulkanologi\src\Physics.cpp:46)
vulkanologi.exe!App::mainLoop() Line 424 (c:\code\Vulkanologi\src\Application.cpp:424)
vulkanologi.exe!App::run() Line 26 (c:\code\Vulkanologi\src\Application.cpp:26)
vulkanologi.exe!main() Line 542 (c:\code\Vulkanologi\src\Application.cpp:542)
vulkanologi.exe!invoke_main() Line 79 (d:\a01\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:79)
vulkanologi.exe!__scrt_common_main_seh() Line 288 (d:\a01\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288)
vulkanologi.exe!__scrt_common_main() Line 331 (d:\a01\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:331)
vulkanologi.exe!mainCRTStartup(void * __formal) Line 17 (d:\a01\_work\2\s\src\vctools\crt\vcstartup\src\startup\exe_main.cpp:17)
kernel32.dll!00007ff9d8c87614() (Unknown Source:0)
ntdll.dll!00007ff9da2626a1() (Unknown Source:0)

The crash from the assert does not happen immediately or with any consistency. But if I use the following values for the wheels the crash happens each time after the vehicle has settled after initial spawn in.

wheel.m_suspensionStiffness = 750.f;
wheel.m_wheelsDampingRelaxation = 200.f;
wheel.m_wheelsDampingCompression = 500.4f;
wheel.m_frictionSlip = 0.45f;
wheel.m_rollInfluence = 0.04f;

This issue occurs on both Linux 6.2.6-arch1-1 and Windows 10, when running on Intel.

Hjaltesorgenfrei commented 1 year ago

The problem seems to be that surfNormalWS gets a coordinate of -1 in the first position. When surfNormalWS.m_floats[0] == -1, it crashes due to m_axle[i] -= surfNormalWS * proj.

This value gets assigned at https://github.com/bulletphysics/bullet3/blob/0e59474e1cf1fd69e3ca512826cfbe15cd6a9cec/src/BulletDynamics/Vehicle/btRaycastVehicle.cpp#L187

Which is in turn assigned from btDefaultVehicleRaycaster::castRay, where a rayCallback.m_hitNormalWorld = (-1, 9.65595e-06, 1.66297e-05). But only after many steps of simulation with a ray hit result of (0, 1, 0).

The problem occurs even in if the only two objects in the dynamic world is a vehicle and a btStaticPlaneShape or a btBvhTriangleMeshShape.

It's only a small change when the normal changes to be negative, with the plane having a normal vector of (0, 1, 0) and a planeConstant of 0.

updateWheelInfo index: 0
from: -1.19337, 0.979308, 0.83563
to: -1.19112, -0.12069, 0.83605
hit normal: 0, 1, 0

updateWheelInfo index: 0
from: -1.19376, 0.979179, 0.835647
to: -1.19151, -0.120819, 0.836047
hit normal: -0.990617, -0.00207001, -0.136649

The problem seems to be that the ray from the wheel interacts with the chassis, which causes a problem. This can be avoided by setting the 0th bit in the chassis's mask, but that does not seem like a good solution.

Hjaltesorgenfrei commented 1 year ago

Fixed it by moving the wheels down 0.001f, as the chassis was a cube of size 1, and suspension was 1 long as well. Which meant they could potentially interact.