1 #include "ContactListener.h" 2 3 ContactListener *currentContactAddedListener = 0; 4 ContactListener *currentContactProcessedListener = 0; 5 ContactListener *currentContactDestroyedListener = 0; 6 ContactListener *currentContactStartedListener = 0; 7 ContactListener *currentContactEndedListener = 0; 8 9 inline bool ContactAddedListener_CB_wrapper_point(btManifoldPoint& cp,const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) { 10 return currentContactAddedListener->onContactAdded(cp, *colObj0Wrap, partId0, index0, *colObj1Wrap, partId1, index1); 11 } 12 inline bool ContactAddedListener_CB_object_point(btManifoldPoint& cp,const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) { 13 return currentContactAddedListener->onContactAdded(cp, colObj0Wrap->m_collisionObject, partId0, index0, colObj1Wrap->m_collisionObject, partId1, index1); 14 } 15 inline bool ContactAddedListener_CB_value_point(btManifoldPoint& cp,const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) { 16 return currentContactAddedListener->onContactAdded(cp, 17 ((GdxCollisionObjectBridge*)(colObj0Wrap->m_collisionObject->getUserPointer()))->userValue, 18 partId0, index0, 19 ((GdxCollisionObjectBridge*)(colObj1Wrap->m_collisionObject->getUserPointer()))->userValue, 20 partId1, index1); 21 } 22 23 inline bool ContactAddedListener_CB_wrapper_filter_point(btManifoldPoint& cp,const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) { 24 const bool match0 = gdxCheckFilter(colObj0Wrap->m_collisionObject, colObj1Wrap->m_collisionObject); 25 const bool match1 = gdxCheckFilter(colObj1Wrap->m_collisionObject, colObj0Wrap->m_collisionObject); 26 return (!match0 && !match1) ? false : 27 currentContactAddedListener->onContactAdded(cp, *colObj0Wrap, partId0, index0, match0, *colObj1Wrap, partId1, index1, match1); 28 } 29 inline bool ContactAddedListener_CB_object_filter_point(btManifoldPoint& cp,const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) { 30 const bool match0 = gdxCheckFilter(colObj0Wrap->m_collisionObject, colObj1Wrap->m_collisionObject); 31 const bool match1 = gdxCheckFilter(colObj1Wrap->m_collisionObject, colObj0Wrap->m_collisionObject); 32 return (!match0 && !match1) ? false : 33 currentContactAddedListener->onContactAdded(cp, colObj0Wrap->m_collisionObject, partId0, index0, match0, colObj1Wrap->m_collisionObject, partId1, index1, match1); 34 } 35 inline bool ContactAddedListener_CB_value_filter_point(btManifoldPoint& cp,const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) { 36 const bool match0 = gdxCheckFilter(colObj0Wrap->m_collisionObject, colObj1Wrap->m_collisionObject); 37 const bool match1 = gdxCheckFilter(colObj1Wrap->m_collisionObject, colObj0Wrap->m_collisionObject); 38 return (!match0 && !match1) ? false : 39 currentContactAddedListener->onContactAdded(cp, 40 ((GdxCollisionObjectBridge*)(colObj0Wrap->m_collisionObject->getUserPointer()))->userValue, 41 partId0, index0, match0, 42 ((GdxCollisionObjectBridge*)(colObj1Wrap->m_collisionObject->getUserPointer()))->userValue, 43 partId1, index1, match1); 44 } 45 46 inline bool ContactAddedListener_CB_wrapper(btManifoldPoint& cp,const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) { 47 return currentContactAddedListener->onContactAdded(*colObj0Wrap, partId0, index0, *colObj1Wrap, partId1, index1); 48 } 49 inline bool ContactAddedListener_CB_object(btManifoldPoint& cp,const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) { 50 return currentContactAddedListener->onContactAdded(colObj0Wrap->m_collisionObject, partId0, index0, colObj1Wrap->m_collisionObject, partId1, index1); 51 } 52 inline bool ContactAddedListener_CB_value(btManifoldPoint& cp,const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) { 53 return currentContactAddedListener->onContactAdded( 54 ((GdxCollisionObjectBridge*)(colObj0Wrap->m_collisionObject->getUserPointer()))->userValue, 55 partId0, index0, 56 ((GdxCollisionObjectBridge*)(colObj1Wrap->m_collisionObject->getUserPointer()))->userValue, 57 partId1, index1); 58 } 59 60 inline bool ContactAddedListener_CB_wrapper_filter(btManifoldPoint& cp,const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) { 61 const bool match0 = gdxCheckFilter(colObj0Wrap->m_collisionObject, colObj1Wrap->m_collisionObject); 62 const bool match1 = gdxCheckFilter(colObj1Wrap->m_collisionObject, colObj0Wrap->m_collisionObject); 63 return (!match0 && !match1) ? false : 64 currentContactAddedListener->onContactAdded(*colObj0Wrap, partId0, index0, match0, *colObj1Wrap, partId1, index1, match1); 65 } 66 inline bool ContactAddedListener_CB_object_filter(btManifoldPoint& cp,const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) { 67 const bool match0 = gdxCheckFilter(colObj0Wrap->m_collisionObject, colObj1Wrap->m_collisionObject); 68 const bool match1 = gdxCheckFilter(colObj1Wrap->m_collisionObject, colObj0Wrap->m_collisionObject); 69 return (!match0 && !match1) ? false : 70 currentContactAddedListener->onContactAdded(colObj0Wrap->m_collisionObject, partId0, index0, match0, colObj1Wrap->m_collisionObject, partId1, index1, match1); 71 } 72 inline bool ContactAddedListener_CB_value_filter(btManifoldPoint& cp,const btCollisionObjectWrapper* colObj0Wrap,int partId0,int index0,const btCollisionObjectWrapper* colObj1Wrap,int partId1,int index1) { 73 const bool match0 = gdxCheckFilter(colObj0Wrap->m_collisionObject, colObj1Wrap->m_collisionObject); 74 const bool match1 = gdxCheckFilter(colObj1Wrap->m_collisionObject, colObj0Wrap->m_collisionObject); 75 return (!match0 && !match1) ? false : 76 currentContactAddedListener->onContactAdded( 77 ((GdxCollisionObjectBridge*)(colObj0Wrap->m_collisionObject->getUserPointer()))->userValue, 78 partId0, index0, match0, 79 ((GdxCollisionObjectBridge*)(colObj1Wrap->m_collisionObject->getUserPointer()))->userValue, 80 partId1, index1, match1); 81 } 82 // ContactProcessed 83 inline bool ContactProcessedListener_CB_object_point(btManifoldPoint& cp,void *body0, void *body1) { 84 currentContactProcessedListener->onContactProcessed(cp, (btCollisionObject*)body0, (btCollisionObject*)body1); 85 return false; 86 } 87 inline bool ContactProcessedListener_CB_value_point(btManifoldPoint& cp,void *body0, void *body1) { 88 currentContactProcessedListener->onContactProcessed(cp, 89 ((GdxCollisionObjectBridge*)(((btCollisionObject*)body0)->getUserPointer()))->userValue, 90 ((GdxCollisionObjectBridge*)(((btCollisionObject*)body1)->getUserPointer()))->userValue); 91 return false; 92 } 93 inline bool ContactProcessedListener_CB_object_filter_point(btManifoldPoint& cp,void *body0, void *body1) { 94 const bool match0 = gdxCheckFilter((btCollisionObject*)body0, (btCollisionObject*)body1); 95 const bool match1 = gdxCheckFilter((btCollisionObject*)body1, (btCollisionObject*)body0); 96 if (match0 || match1) 97 currentContactProcessedListener->onContactProcessed(cp, (btCollisionObject*)body0, match0, (btCollisionObject*)body1, match1); 98 return false; 99 } 100 inline bool ContactProcessedListener_CB_value_filter_point(btManifoldPoint& cp,void *body0, void *body1) { 101 const bool match0 = gdxCheckFilter((btCollisionObject*)body0, (btCollisionObject*)body1); 102 const bool match1 = gdxCheckFilter((btCollisionObject*)body1, (btCollisionObject*)body0); 103 if (match0 || match1) 104 currentContactProcessedListener->onContactProcessed(cp, 105 ((GdxCollisionObjectBridge*)(((btCollisionObject*)body0)->getUserPointer()))->userValue, match0, 106 ((GdxCollisionObjectBridge*)(((btCollisionObject*)body1)->getUserPointer()))->userValue, match1); 107 return false; 108 } 109 inline bool ContactProcessedListener_CB_object(btManifoldPoint& cp,void *body0, void *body1) { 110 currentContactProcessedListener->onContactProcessed((btCollisionObject*)body0, (btCollisionObject*)body1); 111 return false; 112 } 113 inline bool ContactProcessedListener_CB_value(btManifoldPoint& cp,void *body0, void *body1) { 114 currentContactProcessedListener->onContactProcessed( 115 ((GdxCollisionObjectBridge*)(((btCollisionObject*)body0)->getUserPointer()))->userValue, 116 ((GdxCollisionObjectBridge*)(((btCollisionObject*)body1)->getUserPointer()))->userValue); 117 return false; 118 } 119 inline bool ContactProcessedListener_CB_object_filter(btManifoldPoint& cp,void *body0, void *body1) { 120 const bool match0 = gdxCheckFilter((btCollisionObject*)body0, (btCollisionObject*)body1); 121 const bool match1 = gdxCheckFilter((btCollisionObject*)body1, (btCollisionObject*)body0); 122 if (match0 || match1) 123 currentContactProcessedListener->onContactProcessed((btCollisionObject*)body0, match0, (btCollisionObject*)body1, match1); 124 return false; 125 } 126 inline bool ContactProcessedListener_CB_value_filter(btManifoldPoint& cp,void *body0, void *body1) { 127 const bool match0 = gdxCheckFilter((btCollisionObject*)body0, (btCollisionObject*)body1); 128 const bool match1 = gdxCheckFilter((btCollisionObject*)body1, (btCollisionObject*)body0); 129 if (match0 || match1) 130 currentContactProcessedListener->onContactProcessed( 131 ((GdxCollisionObjectBridge*)(((btCollisionObject*)body0)->getUserPointer()))->userValue, match0, 132 ((GdxCollisionObjectBridge*)(((btCollisionObject*)body1)->getUserPointer()))->userValue, match1); 133 return false; 134 } 135 // ContactDestroyed 136 inline bool ContactDestroyedListener_CB(void *userPersistentData) { 137 int val; 138 *(const void **)&val = userPersistentData; 139 currentContactDestroyedListener->onContactDestroyed(val); 140 return false; 141 } 142 // ContactStarted 143 inline void ContactStartedListener_CB_manifold(btPersistentManifold* const &manifold) { 144 currentContactStartedListener->onContactStarted(manifold); 145 } 146 inline void ContactStartedListener_CB_object(btPersistentManifold* const &manifold) { 147 currentContactStartedListener->onContactStarted(manifold->getBody0(), manifold->getBody1()); 148 } 149 inline void ContactStartedListener_CB_value(btPersistentManifold* const &manifold) { 150 currentContactStartedListener->onContactStarted( 151 ((GdxCollisionObjectBridge*)(manifold->getBody0()->getUserPointer()))->userValue, 152 ((GdxCollisionObjectBridge*)(manifold->getBody1()->getUserPointer()))->userValue); 153 } 154 inline void ContactStartedListener_CB_manifold_filter(btPersistentManifold* const &manifold) { 155 const bool match0 = gdxCheckFilter(manifold->getBody0(), manifold->getBody1()); 156 const bool match1 = gdxCheckFilter(manifold->getBody1(), manifold->getBody0()); 157 if (match0 || match1) 158 currentContactStartedListener->onContactStarted(manifold, match0, match1); 159 } 160 inline void ContactStartedListener_CB_object_filter(btPersistentManifold* const &manifold) { 161 const bool match0 = gdxCheckFilter(manifold->getBody0(), manifold->getBody1()); 162 const bool match1 = gdxCheckFilter(manifold->getBody1(), manifold->getBody0()); 163 if (match0 || match1) 164 currentContactStartedListener->onContactStarted(manifold->getBody0(), match0, manifold->getBody1(), match1); 165 } 166 inline void ContactStartedListener_CB_value_filter(btPersistentManifold* const &manifold) { 167 const bool match0 = gdxCheckFilter(manifold->getBody0(), manifold->getBody1()); 168 const bool match1 = gdxCheckFilter(manifold->getBody1(), manifold->getBody0()); 169 if (match0 || match1) 170 currentContactStartedListener->onContactStarted( 171 ((GdxCollisionObjectBridge*)(manifold->getBody0()->getUserPointer()))->userValue, match0, 172 ((GdxCollisionObjectBridge*)(manifold->getBody1()->getUserPointer()))->userValue, match1); 173 } 174 // ContactEnded 175 inline void ContactEndedListener_CB_manifold(btPersistentManifold* const &manifold) { 176 currentContactEndedListener->onContactEnded(manifold); 177 } 178 inline void ContactEndedListener_CB_object(btPersistentManifold* const &manifold) { 179 currentContactEndedListener->onContactEnded(manifold->getBody0(), manifold->getBody1()); 180 } 181 inline void ContactEndedListener_CB_value(btPersistentManifold* const &manifold) { 182 currentContactEndedListener->onContactEnded( 183 ((GdxCollisionObjectBridge*)(manifold->getBody0()->getUserPointer()))->userValue, 184 ((GdxCollisionObjectBridge*)(manifold->getBody1()->getUserPointer()))->userValue); 185 } 186 inline void ContactEndedListener_CB_manifold_filter(btPersistentManifold* const &manifold) { 187 const bool match0 = gdxCheckFilter(manifold->getBody0(), manifold->getBody1()); 188 const bool match1 = gdxCheckFilter(manifold->getBody1(), manifold->getBody0()); 189 if (match0 || match1) 190 currentContactEndedListener->onContactEnded(manifold, match0, match1); 191 } 192 inline void ContactEndedListener_CB_object_filter(btPersistentManifold* const &manifold) { 193 const bool match0 = gdxCheckFilter(manifold->getBody0(), manifold->getBody1()); 194 const bool match1 = gdxCheckFilter(manifold->getBody1(), manifold->getBody0()); 195 if (match0 || match1) 196 currentContactEndedListener->onContactEnded(manifold->getBody0(), match0, manifold->getBody1(), match1); 197 } 198 inline void ContactEndedListener_CB_value_filter(btPersistentManifold* const &manifold) { 199 const bool match0 = gdxCheckFilter(manifold->getBody0(), manifold->getBody1()); 200 const bool match1 = gdxCheckFilter(manifold->getBody1(), manifold->getBody0()); 201 if (match0 || match1) 202 currentContactEndedListener->onContactEnded( 203 ((GdxCollisionObjectBridge*)(manifold->getBody0()->getUserPointer()))->userValue, match0, 204 ((GdxCollisionObjectBridge*)(manifold->getBody1()->getUserPointer()))->userValue, match1); 205 } 206 207 ContactListener::ContactListener(bool dummy) 208 : events(0) {} 209 210 ContactListener::~ContactListener() { 211 disable(); 212 } 213 214 void ContactListener::setEvents(const int &events) { 215 this->events = events; 216 } 217 218 void ContactListener::enable() { 219 enableOnAdded(); 220 enableOnProcessed(); 221 enableOnDestroyed(); 222 enableOnStarted(); 223 enableOnEnded(); 224 } 225 226 void ContactListener::disable() { 227 disableOnAdded(); 228 disableOnProcessed(); 229 disableOnDestroyed(); 230 disableOnStarted(); 231 disableOnEnded(); 232 } 233 234 void ContactListener::enableOnAdded() { 235 const int e = (events >> SHIFT_ON_ADDED) & TYPE_MASK; 236 if (e == 0) 237 return; 238 const bool filter = (e & FILTERED) != 0; 239 const bool point = (e & INCLUDE_POINT) != 0; 240 const int by = (e & BY_MASK); 241 currentContactAddedListener = this; 242 if (by == BY_OBJECT) 243 gContactAddedCallback = filter ? 244 (point ? ContactAddedListener_CB_object_filter_point : ContactAddedListener_CB_object_filter) : 245 (point ? ContactAddedListener_CB_object_point : ContactAddedListener_CB_object); 246 else if (by == BY_VALUE) 247 gContactAddedCallback = filter ? 248 (point ? ContactAddedListener_CB_value_filter_point : ContactAddedListener_CB_value_filter) : 249 (point ? ContactAddedListener_CB_value_point : ContactAddedListener_CB_value); 250 else 251 gContactAddedCallback = filter ? 252 (point ? ContactAddedListener_CB_wrapper_filter_point: ContactAddedListener_CB_wrapper_filter) : 253 (point ? ContactAddedListener_CB_wrapper_point : ContactAddedListener_CB_wrapper); 254 } 255 256 void ContactListener::disableOnAdded() { 257 if (currentContactAddedListener == this) { 258 gContactAddedCallback = 0; 259 currentContactAddedListener = 0; 260 } 261 } 262 263 bool ContactListener::isOnAddedEnabled() { 264 return currentContactAddedListener == this; 265 } 266 267 void ContactListener::enableOnProcessed() { 268 const int e = (events >> SHIFT_ON_PROCESSED) & TYPE_MASK; 269 if (e == 0) 270 return; 271 const bool filter = (e & FILTERED) != 0; 272 const bool point = (e & INCLUDE_POINT) != 0; 273 const int by = (e & BY_MASK); 274 currentContactProcessedListener = this; 275 if (by == BY_VALUE) 276 gContactProcessedCallback = filter ? 277 (point ? ContactProcessedListener_CB_value_filter_point : ContactProcessedListener_CB_value_filter) : 278 (point ? ContactProcessedListener_CB_value_point : ContactProcessedListener_CB_value); 279 else 280 gContactProcessedCallback = filter ? 281 (point ? ContactProcessedListener_CB_object_filter_point : ContactProcessedListener_CB_object_filter) : 282 (point ? ContactProcessedListener_CB_object_point : ContactProcessedListener_CB_object); 283 } 284 285 void ContactListener::disableOnProcessed() { 286 if (currentContactProcessedListener == this) { 287 gContactProcessedCallback = 0; 288 currentContactProcessedListener = 0; 289 } 290 } 291 292 bool ContactListener::isOnProcessedEnabled() { 293 return currentContactProcessedListener == this; 294 } 295 296 void ContactListener::enableOnDestroyed() { 297 const int e = (events >> SHIFT_ON_DESTROYED) & TYPE_MASK; 298 if (e == 0) 299 return; 300 const bool filter = (e & FILTERED) != 0; 301 const bool point = (e & INCLUDE_POINT) != 0; 302 const int by = (e & BY_MASK); 303 currentContactDestroyedListener = this; 304 gContactDestroyedCallback = ContactDestroyedListener_CB; 305 } 306 307 void ContactListener::disableOnDestroyed() { 308 if (currentContactDestroyedListener == this) { 309 gContactDestroyedCallback = 0; 310 currentContactDestroyedListener = 0; 311 } 312 } 313 314 bool ContactListener::isOnDestroyedEnabled() { 315 return currentContactDestroyedListener == this; 316 } 317 318 void ContactListener::enableOnStarted() { 319 const int e = (events >> SHIFT_ON_STARTED) & TYPE_MASK; 320 if (e == 0) 321 return; 322 const bool filter = (e & FILTERED) != 0; 323 const bool point = (e & INCLUDE_POINT) != 0; 324 const int by = (e & BY_MASK); 325 currentContactStartedListener = this; 326 if (by == BY_OBJECT) 327 gContactStartedCallback = filter ? ContactStartedListener_CB_object_filter : ContactStartedListener_CB_object; 328 else if (by == BY_VALUE) 329 gContactStartedCallback = filter ? ContactStartedListener_CB_value_filter : ContactStartedListener_CB_value; 330 else 331 gContactStartedCallback = filter ? ContactStartedListener_CB_manifold_filter : ContactStartedListener_CB_manifold; 332 } 333 334 void ContactListener::disableOnStarted() { 335 if (currentContactStartedListener == this) { 336 gContactStartedCallback = 0; 337 currentContactStartedListener = 0; 338 } 339 } 340 341 bool ContactListener::isOnStartedEnabled() { 342 return currentContactStartedListener == this; 343 } 344 345 void ContactListener::enableOnEnded() { 346 const int e = (events >> SHIFT_ON_ENDED) & TYPE_MASK; 347 if (e == 0) 348 return; 349 const bool filter = (e & FILTERED) != 0; 350 const bool point = (e & INCLUDE_POINT) != 0; 351 const int by = (e & BY_MASK); 352 currentContactEndedListener = this; 353 if (by == BY_OBJECT) 354 gContactEndedCallback = filter ? ContactEndedListener_CB_object_filter : ContactEndedListener_CB_object; 355 else if (by == BY_VALUE) 356 gContactEndedCallback = filter ? ContactEndedListener_CB_value_filter : ContactEndedListener_CB_value; 357 else 358 gContactEndedCallback = filter ? ContactEndedListener_CB_manifold_filter : ContactEndedListener_CB_manifold; 359 } 360 361 void ContactListener::disableOnEnded() { 362 if (currentContactEndedListener == this) { 363 gContactEndedCallback = 0; 364 currentContactEndedListener = 0; 365 } 366 } 367 368 bool ContactListener::isOnEndedEnabled() { 369 return currentContactEndedListener == this; 370 }