فهرست منبع

Bullet: contact added callback

enn0x 13 سال پیش
والد
کامیت
8d66921d79

+ 3 - 0
panda/src/bullet/Sources.pp

@@ -21,6 +21,7 @@
     bulletConeShape.h bulletConeShape.I \
     bulletConeTwistConstraint.h bulletConeTwistConstraint.I \
     bulletConstraint.h bulletConstraint.I \
+    bulletContactCallbackData.h bulletContactCallbackData.I \
     bulletContactCallbacks.h \
     bulletContactResult.h bulletContactResult.I \
     bulletConvexHullShape.h bulletConvexHullShape.I \
@@ -67,6 +68,7 @@
     bulletConeShape.cxx \
     bulletConeTwistConstraint.cxx \
     bulletConstraint.cxx \
+    bulletContactCallbackData.cxx \
     bulletContactResult.cxx \
     bulletConvexHullShape.cxx \
     bulletConvexPointCloudShape.cxx \
@@ -111,6 +113,7 @@
     bulletConeShape.h bulletConeShape.I \
     bulletConeTwistConstraint.h bulletConeTwistConstraint.I \
     bulletConstraint.h bulletConstraint.I \
+    bulletContactCallbackData.h bulletContactCallbackData.I \
     bulletContactCallbacks.h \
     bulletContactResult.h bulletContactResult.I \
     bulletConvexHullShape.h bulletConvexHullShape.I \

+ 60 - 0
panda/src/bullet/bulletContactCallbackData.I

@@ -0,0 +1,60 @@
+// Filename: bulletContactCallbackData.I
+// Created by:  enn0x (22Nov12)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) Carnegie Mellon University.  All rights reserved.
+//
+// All use of this software is subject to the terms of the revised BSD
+// license.  You should have received a copy of this license along
+// with this source code in a file named "LICENSE."
+//
+////////////////////////////////////////////////////////////////////
+
+////////////////////////////////////////////////////////////////////
+//     Function: BulletContactCallbackData::Constructor
+//       Access: Published
+//  Description:
+////////////////////////////////////////////////////////////////////
+INLINE BulletContactCallbackData::
+BulletContactCallbackData(BulletManifoldPoint &mp, PandaNode *node0, PandaNode *node1) :
+  _node0(node0),
+  _node1(node1),
+  _mp(mp) {
+
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: BulletContactCallbackData::get_node_0
+//       Access: Published
+//  Description:
+////////////////////////////////////////////////////////////////////
+INLINE PandaNode *BulletContactCallbackData::
+get_node_0() const {
+
+  return _node0;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: BulletContactCallbackData::get_node_1
+//       Access: Published
+//  Description:
+////////////////////////////////////////////////////////////////////
+INLINE PandaNode *BulletContactCallbackData::
+get_node_1() const {
+
+  return _node1;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: BulletContactCallbackData::get_manifold
+//       Access: Published
+//  Description:
+////////////////////////////////////////////////////////////////////
+INLINE BulletManifoldPoint &BulletContactCallbackData::
+get_manifold() const {
+
+  return _mp;
+}
+

+ 31 - 0
panda/src/bullet/bulletContactCallbackData.cxx

@@ -0,0 +1,31 @@
+// Filename: bulletContactCallbackData.cxx
+// Created by:  enn0x (22Nov12)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) Carnegie Mellon University.  All rights reserved.
+//
+// All use of this software is subject to the terms of the revised BSD
+// license.  You should have received a copy of this license along
+// with this source code in a file named "LICENSE."
+//
+////////////////////////////////////////////////////////////////////
+
+#include "bulletContactCallbackData.h"
+
+TypeHandle BulletContactCallbackData::_type_handle;
+
+/*
+////////////////////////////////////////////////////////////////////
+//     Function: BulletContactCallbackData::enable_feedback
+//       Access: Published
+//  Description:
+////////////////////////////////////////////////////////////////////
+void BulletContactCallbackData::
+enable_feedback(bool value) {
+
+  ptr()->enableFeedback(value);
+}
+*/
+

+ 70 - 0
panda/src/bullet/bulletContactCallbackData.h

@@ -0,0 +1,70 @@
+// Filename: bulletContactCallbackData.h
+// Created by:  enn0x (22Nov12)
+//
+////////////////////////////////////////////////////////////////////
+//
+// PANDA 3D SOFTWARE
+// Copyright (c) Carnegie Mellon University.  All rights reserved.
+//
+// All use of this software is subject to the terms of the revised BSD
+// license.  You should have received a copy of this license along
+// with this source code in a file named "LICENSE."
+//
+////////////////////////////////////////////////////////////////////
+
+#ifndef __BULLET_CONTACT_CALLBACK_DATA_H__
+#define __BULLET_CONTACT_CALLBACK_DATA_H__
+
+#include "pandabase.h"
+#include "callbackData.h"
+#include "callbackObject.h"
+
+#include "bullet_includes.h"
+#include "bullet_utils.h"
+#include "bulletManifoldPoint.h"
+
+////////////////////////////////////////////////////////////////////
+//       Class : BulletContactCallbackData
+// Description : 
+////////////////////////////////////////////////////////////////////
+class EXPCL_PANDABULLET BulletContactCallbackData : public CallbackData {
+
+PUBLISHED:
+  INLINE BulletContactCallbackData(BulletManifoldPoint &mp, 
+                                   PandaNode *node0, 
+                                   PandaNode *node1);
+
+  PandaNode *get_node_0() const;
+  PandaNode *get_node_1() const;
+  BulletManifoldPoint &get_manifold() const;
+
+private:
+  PandaNode *_node0;
+  PandaNode *_node1;
+  BulletManifoldPoint &_mp;
+
+////////////////////////////////////////////////////////////////////
+public:
+  static TypeHandle get_class_type() {
+    return _type_handle;
+  }
+  static void init_type() {
+    CallbackData::init_type();
+    register_type(_type_handle, "BulletContactCallbackData", 
+                  CallbackData::get_class_type());
+  }
+  virtual TypeHandle get_type() const {
+    return get_class_type();
+  }
+  virtual TypeHandle force_init_type() {
+    init_type();
+    return get_class_type();
+  }
+
+private:
+  static TypeHandle _type_handle;
+};
+
+#include "bulletContactCallbackData.I"
+
+#endif // __BULLET_CONTACT_CALLBACK_DATA_H__

+ 12 - 2
panda/src/bullet/bulletContactCallbacks.h

@@ -18,7 +18,8 @@
 #include "pandabase.h"
 
 #include "bullet_includes.h"
-
+#include "bulletWorld.h"
+#include "bulletContactCallbackData.h"
 #include "config_bullet.h" // required for: bullet_cat.debug()
 
 #include "event.h"
@@ -67,6 +68,15 @@ contact_added_callback(btManifoldPoint &cp,
 
       EventQueue::get_global_event_queue()->queue_event(event);
     }
+
+    // Callback
+    if (bullet_contact_added_callback) {
+
+      BulletManifoldPoint mp(cp);
+      BulletContactCallbackData cbdata(mp, node0, node1);
+
+      bullet_contact_added_callback->do_callback(&cbdata);
+    }
   }
 
   return true;
@@ -83,7 +93,7 @@ contact_processed_callback(btManifoldPoint &cp,
 
 /*
   btCollisionObject *obj0 = (btCollisionObject *)body0;
-  btCollisionObject *colobj1Obj1 = (btCollisionObject *)body1;
+  btCollisionObject *obj1 = (btCollisionObject *)body1;
 
   int flags0 = obj0->getCollisionFlags();
   int flags1 = obj1->getCollisionFlags();

+ 2 - 1
panda/src/bullet/bulletContactResult.I

@@ -43,7 +43,8 @@ get_node1() const {
 INLINE const BulletManifoldPoint *BulletContact::
 get_manifold_point() const {
 
-  return new BulletManifoldPoint(_mp);
+  btManifoldPoint &mp = const_cast<btManifoldPoint &>(_mp);
+  return new BulletManifoldPoint(mp);
 }
 
 ////////////////////////////////////////////////////////////////////

+ 0 - 24
panda/src/bullet/bulletManifoldPoint.I

@@ -22,18 +22,6 @@ INLINE BulletManifoldPoint::
 
 }
 
-////////////////////////////////////////////////////////////////////
-//     Function: BulletManifoldPoint::is_const
-//       Access: Published
-//  Description: Returns TRUE if this instance of BulletManifoldPoint
-//               can not be modified.
-////////////////////////////////////////////////////////////////////
-INLINE bool BulletManifoldPoint::
-is_const() const {
-
-  return _const;
-}
-
 ////////////////////////////////////////////////////////////////////
 //     Function: BulletManifoldPoint::set_lateral_friction_initialized
 //       Access: Published
@@ -42,7 +30,6 @@ is_const() const {
 INLINE void BulletManifoldPoint::
 set_lateral_friction_initialized(bool value) {
 
-  nassertv(!_const);
   _pt.m_lateralFrictionInitialized = value;
 }
 
@@ -65,7 +52,6 @@ get_lateral_friction_initialized() const {
 INLINE void BulletManifoldPoint::
 set_lateral_friction_dir1(const LVecBase3 &dir) {
 
-  nassertv(!_const);
   _pt.m_lateralFrictionDir1 = LVecBase3_to_btVector3(dir);
 }
 
@@ -88,7 +74,6 @@ get_lateral_friction_dir1() const {
 INLINE void BulletManifoldPoint::
 set_lateral_friction_dir2(const LVecBase3 &dir) {
 
-  nassertv(!_const);
   _pt.m_lateralFrictionDir2 = LVecBase3_to_btVector3(dir);
 }
 
@@ -111,7 +96,6 @@ get_lateral_friction_dir2() const {
 INLINE void BulletManifoldPoint::
 set_contact_motion1(PN_stdfloat value) {
 
-  nassertv(!_const);
   _pt.m_contactMotion1 = (btScalar)value;
 }
 
@@ -134,7 +118,6 @@ get_contact_motion1() const {
 INLINE void BulletManifoldPoint::
 set_contact_motion2(PN_stdfloat value) {
 
-  nassertv(!_const);
   _pt.m_contactMotion2 = (btScalar)value;
 }
 
@@ -157,7 +140,6 @@ get_contact_motion2() const {
 INLINE void BulletManifoldPoint::
 set_combined_friction(PN_stdfloat value) {
 
-  nassertv(!_const);
   _pt.m_combinedFriction = (btScalar)value;
 }
 
@@ -180,7 +162,6 @@ get_combined_friction() const {
 INLINE void BulletManifoldPoint::
 set_combined_restitution(PN_stdfloat value) {
 
-  nassertv(!_const);
   _pt.m_combinedRestitution = (btScalar)value;
 }
 
@@ -203,7 +184,6 @@ get_combined_restitution() const {
 INLINE void BulletManifoldPoint::
 set_applied_impulse(PN_stdfloat value) {
 
-  nassertv(!_const);
   _pt.m_appliedImpulse = (btScalar)value;
 }
 
@@ -215,7 +195,6 @@ set_applied_impulse(PN_stdfloat value) {
 INLINE void BulletManifoldPoint::
 set_applied_impulse_lateral1(PN_stdfloat value) {
 
-  nassertv(!_const);
   _pt.m_appliedImpulseLateral1 = (btScalar)value;
 }
 
@@ -238,7 +217,6 @@ get_applied_impulse_lateral1() const {
 INLINE void BulletManifoldPoint::
 set_applied_impulse_lateral2(PN_stdfloat value) {
 
-  nassertv(!_const);
   _pt.m_appliedImpulseLateral2 = (btScalar)value;
 }
 
@@ -261,7 +239,6 @@ get_applied_impulse_lateral2() const {
 INLINE void BulletManifoldPoint::
 set_contact_cfm1(PN_stdfloat value) {
 
-  nassertv(!_const);
   _pt.m_contactCFM1 = (btScalar)value;
 }
 
@@ -284,7 +261,6 @@ get_contact_cfm1() const {
 INLINE void BulletManifoldPoint::
 set_contact_cfm2(PN_stdfloat value) {
 
-  nassertv(!_const);
   _pt.m_contactCFM2 = (btScalar)value;
 }
 

+ 1 - 13
panda/src/bullet/bulletManifoldPoint.cxx

@@ -21,22 +21,10 @@
 ////////////////////////////////////////////////////////////////////
 BulletManifoldPoint::
 BulletManifoldPoint(btManifoldPoint &pt) 
- : _pt(pt), _const(false) {
+ : _pt(pt) {
 
 }
 
-////////////////////////////////////////////////////////////////////
-//     Function: BulletManifoldPoint::Constructor
-//       Access: Public
-//  Description:
-////////////////////////////////////////////////////////////////////
-BulletManifoldPoint::
-BulletManifoldPoint(const btManifoldPoint &pt) {
-
-  _pt = (btManifoldPoint &)pt;
-  _const = true;
-}
-
 ////////////////////////////////////////////////////////////////////
 //     Function: BulletManifoldPoint::get_lift_time
 //       Access: Published

+ 1 - 5
panda/src/bullet/bulletManifoldPoint.h

@@ -45,8 +45,6 @@ PUBLISHED:
   int get_index0() const;
   int get_index1() const;
 
-  INLINE bool is_const() const;
-
   INLINE void set_lateral_friction_initialized(bool value);
   INLINE void set_lateral_friction_dir1(const LVecBase3 &dir);
   INLINE void set_lateral_friction_dir2(const LVecBase3 &dir);
@@ -73,12 +71,10 @@ PUBLISHED:
   INLINE PN_stdfloat get_contact_cfm2() const;
 
 public:
-  BulletManifoldPoint(const btManifoldPoint &pt);
   BulletManifoldPoint(btManifoldPoint &pt);
 
 private:
-  btManifoldPoint _pt;
-  bool _const;
+  btManifoldPoint &_pt;
 };
 
 #include "bulletManifoldPoint.I"

+ 32 - 0
panda/src/bullet/bulletWorld.cxx

@@ -37,6 +37,8 @@ PStatCollector BulletWorld::_pstat_debug("App:Bullet:DoPhysics:Debug");
 PStatCollector BulletWorld::_pstat_p2b("App:Bullet:DoPhysics:SyncP2B");
 PStatCollector BulletWorld::_pstat_b2p("App:Bullet:DoPhysics:SyncB2P");
 
+PT(CallbackObject) bullet_contact_added_callback;
+
 ////////////////////////////////////////////////////////////////////
 //     Function: BulletWorld::Constructor
 //       Access: Published
@@ -867,6 +869,36 @@ get_group_collision_flag(unsigned int group1, unsigned int group2) const {
   return _filter_cb2._collide[group1].get_bit(group2);
 }
 
+////////////////////////////////////////////////////////////////////
+//     Function: BulletWorld::set_contact_added_callback
+//       Access: Published
+//  Description:
+////////////////////////////////////////////////////////////////////
+void BulletWorld::
+set_contact_added_callback(CallbackObject *obj) {
+
+  _world->getSolverInfo().m_solverMode |= SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION;
+  _world->getSolverInfo().m_solverMode |= SOLVER_USE_2_FRICTION_DIRECTIONS;
+  _world->getSolverInfo().m_solverMode |= SOLVER_ENABLE_FRICTION_DIRECTION_CACHING;
+
+  bullet_contact_added_callback = obj;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: BulletWorld::clear_contact_added_callback
+//       Access: Published
+//  Description:
+////////////////////////////////////////////////////////////////////
+void BulletWorld::
+clear_contact_added_callback() {
+
+  _world->getSolverInfo().m_solverMode &= ~SOLVER_DISABLE_VELOCITY_DEPENDENT_FRICTION_DIRECTION;
+  _world->getSolverInfo().m_solverMode &= ~SOLVER_USE_2_FRICTION_DIRECTIONS;
+  _world->getSolverInfo().m_solverMode &= ~SOLVER_ENABLE_FRICTION_DIRECTION_CACHING;
+
+  bullet_contact_added_callback = NULL;
+}
+
 #ifdef HAVE_PYTHON
 ////////////////////////////////////////////////////////////////////
 //     Function: BulletWorld::set_python_filter_callback

+ 7 - 0
panda/src/bullet/bulletWorld.h

@@ -35,6 +35,7 @@
 #include "typedReferenceCount.h"
 #include "transformState.h"
 #include "pandaNode.h"
+#include "callbackObject.h"
 #include "collideMask.h"
 #include "luse.h"
 
@@ -47,6 +48,8 @@ class BulletPersistentManifold;
 class BulletShape;
 class BulletSoftBodyWorldInfo;
 
+extern PT(CallbackObject) bullet_contact_added_callback;
+
 ////////////////////////////////////////////////////////////////////
 //       Class : BulletWorld
 // Description : 
@@ -135,6 +138,10 @@ PUBLISHED:
   void set_group_collision_flag(unsigned int group1, unsigned int group2, bool enable);
   bool get_group_collision_flag(unsigned int group1, unsigned int group2) const;
 
+  // Callbacks
+  void set_contact_added_callback(CallbackObject *obj);
+  void clear_contact_added_callback();
+
   // Configuration
   enum BroadphaseAlgorithm {
     BA_sweep_and_prune,

+ 1 - 0
panda/src/bullet/bullet_utils.h

@@ -55,6 +55,7 @@ enum BulletUpAxis {
 };
 
 EXPCL_PANDABULLET BulletUpAxis get_default_up_axis();
+
 END_PUBLISH
 
 #include "bullet_utils.I"

+ 2 - 4
panda/src/bullet/config_bullet.cxx

@@ -21,6 +21,7 @@
 #include "bulletCharacterControllerNode.h"
 #include "bulletConeShape.h"
 #include "bulletConeTwistConstraint.h"
+#include "bulletContactCallbackData.h"
 #include "bulletConstraint.h"
 #include "bulletConvexHullShape.h"
 #include "bulletConvexPointCloudShape.h"
@@ -144,7 +145,6 @@ init_libbullet() {
   initialized = true;
 
   // Initialize types
-  //
   BulletBaseCharacterControllerNode::init_type();
   BulletBodyNode::init_type();
   BulletBoxShape::init_type();
@@ -152,6 +152,7 @@ init_libbullet() {
   BulletCharacterControllerNode::init_type();
   BulletConeShape::init_type();
   BulletConeTwistConstraint::init_type();
+  BulletContactCallbackData::init_type();
   BulletConstraint::init_type();
   BulletConvexHullShape::init_type();
   BulletConvexPointCloudShape::init_type();
@@ -176,18 +177,15 @@ init_libbullet() {
   BulletWorld::init_type();
 
   // Custom contact callbacks
-  //
   gContactAddedCallback = contact_added_callback;
   gContactProcessedCallback = contact_processed_callback;
   gContactDestroyedCallback = contact_destroyed_callback;
 
   // Initialize notification category
-  //
   bullet_cat.init();
   bullet_cat.debug() << "initialize module" << endl;
 
   // Register the Bullet system
-  //
   PandaSystem *ps = PandaSystem::get_global_ptr();
   ps->add_system("Bullet");
 }

+ 1 - 0
panda/src/bullet/p3bullet_composite.cxx

@@ -10,6 +10,7 @@
 #include "bulletConeShape.cxx"
 #include "bulletConeTwistConstraint.cxx"
 #include "bulletConstraint.cxx"
+#include "bulletContactCallbackData.cxx"
 #include "bulletContactResult.cxx"
 #include "bulletConvexHullShape.cxx"
 #include "bulletConvexPointCloudShape.cxx"