From 8c40ff39d7ef7278878b4da4a55f67d69b62be57 Mon Sep 17 00:00:00 2001 From: rdb Date: Fri, 23 Aug 2019 12:04:26 +0200 Subject: [PATCH] bullet: fix contact added callback behavior The API seems to imply that the callback is per-world, when it is actually a global setting. This fixes it to truly behave per-world. This also fixes a crash on shutdown caused by the callback destructing when the Python interpreter is already shutting down. Fixes #712 --- panda/src/bullet/bulletWorld.cxx | 8 ++++++-- panda/src/bullet/bulletWorld.h | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/panda/src/bullet/bulletWorld.cxx b/panda/src/bullet/bulletWorld.cxx index 930fe10111..6fa0381949 100644 --- a/panda/src/bullet/bulletWorld.cxx +++ b/panda/src/bullet/bulletWorld.cxx @@ -222,6 +222,8 @@ int BulletWorld:: do_physics(PN_stdfloat dt, int max_substeps, PN_stdfloat stepsize) { LightMutexHolder holder(get_global_lock()); + bullet_contact_added_callback = _contact_added_callback_obj; + _pstat_physics.start(); int num_substeps = clamp(int(dt / stepsize), 1, max_substeps); @@ -249,6 +251,8 @@ do_physics(PN_stdfloat dt, int max_substeps, PN_stdfloat stepsize) { _pstat_physics.stop(); + bullet_contact_added_callback.clear(); + return n; } @@ -1146,7 +1150,7 @@ set_contact_added_callback(CallbackObject *obj) { _world->getSolverInfo().m_solverMode |= SOLVER_USE_2_FRICTION_DIRECTIONS; _world->getSolverInfo().m_solverMode |= SOLVER_ENABLE_FRICTION_DIRECTION_CACHING; - bullet_contact_added_callback = obj; + _contact_added_callback_obj = obj; } /** @@ -1160,7 +1164,7 @@ clear_contact_added_callback() { _world->getSolverInfo().m_solverMode &= ~SOLVER_USE_2_FRICTION_DIRECTIONS; _world->getSolverInfo().m_solverMode &= ~SOLVER_ENABLE_FRICTION_DIRECTION_CACHING; - bullet_contact_added_callback = nullptr; + _contact_added_callback_obj = nullptr; } /** diff --git a/panda/src/bullet/bulletWorld.h b/panda/src/bullet/bulletWorld.h index 67607923dd..bc032f1934 100644 --- a/panda/src/bullet/bulletWorld.h +++ b/panda/src/bullet/bulletWorld.h @@ -272,6 +272,7 @@ private: btOverlapFilterCallback *_filter_cb; PT(CallbackObject) _tick_callback_obj; + PT(CallbackObject) _contact_added_callback_obj; PT(BulletDebugNode) _debug;