Panda3D
 All Classes Functions Variables Enumerations
bulletContactCallbacks.h
1 // Filename: bulletContactCallbacks.h
2 // Created by: enn0x (10Apr10)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef __BULLET_CONTACT_CALLBACKS_H__
16 #define __BULLET_CONTACT_CALLBACKS_H__
17 
18 #include "pandabase.h"
19 
20 #include "bullet_includes.h"
21 #include "bulletWorld.h"
22 #include "bulletContactCallbackData.h"
23 #include "config_bullet.h" // required for: bullet_cat.debug()
24 
25 #include "event.h"
26 #include "eventQueue.h"
27 #include "eventParameter.h"
28 #include "pandaNode.h"
29 
31  PT(PandaNode) node0;
32  PT(PandaNode) node1;
33 };
34 
35 ////////////////////////////////////////////////////////////////////
36 // Function: contact_added_callback
37 // Description:
38 ////////////////////////////////////////////////////////////////////
39 static bool
40 contact_added_callback(btManifoldPoint &cp,
41 #if BT_BULLET_VERSION >= 281
42  const btCollisionObjectWrapper *wrap0,
43 #else
44  const btCollisionObject *obj0,
45 #endif
46  int id0,
47  int index0,
48 #if BT_BULLET_VERSION >= 281
49  const btCollisionObjectWrapper *wrap1,
50 #else
51  const btCollisionObject *obj1,
52 #endif
53  int id1,
54  int index1) {
55 
56  if (cp.m_userPersistentData == NULL) {
57 
58 #if BT_BULLET_VERSION >= 281
59  PT(PandaNode) node0 = (PandaNode *)wrap0->getCollisionObject()->getUserPointer();
60  PT(PandaNode) node1 = (PandaNode *)wrap1->getCollisionObject()->getUserPointer();
61 #else
62  PT(PandaNode) node0 = (PandaNode *)obj0->getUserPointer();
63  PT(PandaNode) node1 = (PandaNode *)obj1->getUserPointer();
64 #endif
65 
66  bullet_cat.debug() << "contact added: " << cp.m_userPersistentData << endl;
67 
68  // Gather persistent data
70  data->node0 = node0;
71  data->node1 = node1;
72 
73  cp.m_userPersistentData = (void *)data;
74 
75  // Send event
76  if (bullet_enable_contact_events) {
77 
78  Event *event = new Event("bullet-contact-added");
79  event->add_parameter(EventParameter(node0));
80  event->add_parameter(EventParameter(node1));
81 
82  EventQueue::get_global_event_queue()->queue_event(event);
83  }
84 
85  // Callback
86  if (bullet_contact_added_callback) {
87 
88  BulletManifoldPoint mp(cp);
89  BulletContactCallbackData cbdata(mp, node0, node1, id0, id1, index0, index1);
90 
91  bullet_contact_added_callback->do_callback(&cbdata);
92  }
93  }
94 
95  return true;
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: contact_processed_callback
100 // Description:
101 ////////////////////////////////////////////////////////////////////
102 static bool
103 contact_processed_callback(btManifoldPoint &cp,
104  void *body0,
105  void *body1) {
106 
107 /*
108  btCollisionObject *obj0 = (btCollisionObject *)body0;
109  btCollisionObject *obj1 = (btCollisionObject *)body1;
110 
111  int flags0 = obj0->getCollisionFlags();
112  int flags1 = obj1->getCollisionFlags();
113 
114  if ((flags0 & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK)
115  || (flags1 & btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK)) {
116 
117  // do something...
118  }
119 */
120 
121  return false;
122 }
123 
124 ////////////////////////////////////////////////////////////////////
125 // Function: contact_destroyed_callback
126 // Description:
127 ////////////////////////////////////////////////////////////////////
128 static bool
129 contact_destroyed_callback(void *userPersistentData) {
130 
131  bullet_cat.debug() << "contact removed: " << userPersistentData << endl;
132 
133  UserPersitentData *data = (UserPersitentData *)userPersistentData;
134 
135  // Send event
136  if (bullet_enable_contact_events) {
137 
138  Event *event = new Event("bullet-contact-destroyed");
139  event->add_parameter(EventParameter(data->node0));
140  event->add_parameter(EventParameter(data->node1));
141 
142  EventQueue::get_global_event_queue()->queue_event(event);
143  }
144 
145  // Delete persitent data
146  delete data;
147 
148  return false;
149 }
150 
151 #endif // __BULLET_CONTACT_CALLBACKS_H__
A basic node of the scene graph or data graph.
Definition: pandaNode.h:72
An optional parameter associated with an event.
static EventQueue * get_global_event_queue()
Returns a pointer to the one global EventQueue object.
Definition: eventQueue.I:24
A named event, possibly with parameters.
Definition: event.h:36