2
0

jmePhysicsSpace.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (c) 2009-2010 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "jmePhysicsSpace.h"
  33. #include "jmeBulletUtil.h"
  34. #include <stdio.h>
  35. /**
  36. * Author: Normen Hansen
  37. */
  38. jmePhysicsSpace::jmePhysicsSpace(JNIEnv* env, jobject javaSpace) {
  39. //TODO: global ref? maybe not -> cleaning, rather callback class?
  40. this->javaPhysicsSpace = env->NewWeakGlobalRef(javaSpace);
  41. this->env = env;
  42. env->GetJavaVM(&vm);
  43. if (env->ExceptionCheck()) {
  44. env->Throw(env->ExceptionOccurred());
  45. return;
  46. }
  47. }
  48. void jmePhysicsSpace::attachThread() {
  49. #ifdef ANDROID
  50. vm->AttachCurrentThread((JNIEnv**) &env, NULL);
  51. #elif defined (JNI_VERSION_1_2)
  52. vm->AttachCurrentThread((void**) &env, NULL);
  53. #else
  54. vm->AttachCurrentThread(&env, NULL);
  55. #endif
  56. }
  57. JNIEnv* jmePhysicsSpace::getEnv() {
  58. attachThread();
  59. return this->env;
  60. }
  61. void jmePhysicsSpace::stepSimulation(jfloat tpf, jint maxSteps, jfloat accuracy) {
  62. dynamicsWorld->stepSimulation(tpf, maxSteps, accuracy);
  63. }
  64. btThreadSupportInterface* jmePhysicsSpace::createSolverThreadSupport(int maxNumThreads) {
  65. #ifdef _WIN32
  66. Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("solverThreads", SolverThreadFunc, SolverlsMemoryFunc, maxNumThreads);
  67. Win32ThreadSupport* threadSupport = new Win32ThreadSupport(threadConstructionInfo);
  68. threadSupport->startSPU();
  69. #elif defined (USE_PTHREADS)
  70. PosixThreadSupport::ThreadConstructionInfo constructionInfo("collision", SolverThreadFunc,
  71. SolverlsMemoryFunc, maxNumThreads);
  72. PosixThreadSupport* threadSupport = new PosixThreadSupport(constructionInfo);
  73. threadSupport->startSPU();
  74. #else
  75. SequentialThreadSupport::SequentialThreadConstructionInfo tci("solverThreads", SolverThreadFunc, SolverlsMemoryFunc);
  76. SequentialThreadSupport* threadSupport = new SequentialThreadSupport(tci);
  77. threadSupport->startSPU();
  78. #endif
  79. return threadSupport;
  80. }
  81. btThreadSupportInterface* jmePhysicsSpace::createDispatchThreadSupport(int maxNumThreads) {
  82. #ifdef _WIN32
  83. Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("solverThreads", processCollisionTask, createCollisionLocalStoreMemory, maxNumThreads);
  84. Win32ThreadSupport* threadSupport = new Win32ThreadSupport(threadConstructionInfo);
  85. threadSupport->startSPU();
  86. #elif defined (USE_PTHREADS)
  87. PosixThreadSupport::ThreadConstructionInfo solverConstructionInfo("solver", processCollisionTask,
  88. createCollisionLocalStoreMemory, maxNumThreads);
  89. PosixThreadSupport* threadSupport = new PosixThreadSupport(solverConstructionInfo);
  90. threadSupport->startSPU();
  91. #else
  92. SequentialThreadSupport::SequentialThreadConstructionInfo tci("solverThreads", processCollisionTask, createCollisionLocalStoreMemory);
  93. SequentialThreadSupport* threadSupport = new SequentialThreadSupport(tci);
  94. threadSupport->startSPU();
  95. #endif
  96. return threadSupport;
  97. }
  98. void jmePhysicsSpace::createPhysicsSpace(jfloat minX, jfloat minY, jfloat minZ, jfloat maxX, jfloat maxY, jfloat maxZ, jint broadphaseId, jboolean threading) {
  99. // collision configuration contains default setup for memory, collision setup
  100. btDefaultCollisionConstructionInfo cci;
  101. // if(threading){
  102. // cci.m_defaultMaxPersistentManifoldPoolSize = 32768;
  103. // }
  104. btCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(cci);
  105. btVector3 min = btVector3(minX, minY, minZ);
  106. btVector3 max = btVector3(maxX, maxY, maxZ);
  107. btBroadphaseInterface* broadphase;
  108. switch (broadphaseId) {
  109. case 0:
  110. broadphase = new btSimpleBroadphase();
  111. break;
  112. case 1:
  113. broadphase = new btAxisSweep3(min, max);
  114. break;
  115. case 2:
  116. //TODO: 32bit!
  117. broadphase = new btAxisSweep3(min, max);
  118. break;
  119. case 3:
  120. broadphase = new btDbvtBroadphase();
  121. break;
  122. case 4:
  123. // broadphase = new btGpu3DGridBroadphase(
  124. // min, max,
  125. // 20, 20, 20,
  126. // 10000, 1000, 25);
  127. break;
  128. }
  129. btCollisionDispatcher* dispatcher;
  130. btConstraintSolver* solver;
  131. // use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
  132. if (threading) {
  133. btThreadSupportInterface* dispatchThreads = createDispatchThreadSupport(4);
  134. dispatcher = new SpuGatheringCollisionDispatcher(dispatchThreads, 4, collisionConfiguration);
  135. dispatcher->setDispatcherFlags(btCollisionDispatcher::CD_DISABLE_CONTACTPOOL_DYNAMIC_ALLOCATION);
  136. } else {
  137. dispatcher = new btCollisionDispatcher(collisionConfiguration);
  138. }
  139. // the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
  140. if (threading) {
  141. btThreadSupportInterface* solverThreads = createSolverThreadSupport(4);
  142. solver = new btParallelConstraintSolver(solverThreads);
  143. } else {
  144. solver = new btSequentialImpulseConstraintSolver;
  145. }
  146. //create dynamics world
  147. btDiscreteDynamicsWorld* world = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
  148. dynamicsWorld = world;
  149. dynamicsWorld->setWorldUserInfo(this);
  150. //parallel solver requires the contacts to be in a contiguous pool, so avoid dynamic allocation
  151. if (threading) {
  152. world->getSimulationIslandManager()->setSplitIslands(false);
  153. world->getSolverInfo().m_numIterations = 4;
  154. world->getSolverInfo().m_solverMode = SOLVER_SIMD + SOLVER_USE_WARMSTARTING; //+SOLVER_RANDMIZE_ORDER;
  155. world->getDispatchInfo().m_enableSPU = true;
  156. }
  157. broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
  158. dynamicsWorld->setGravity(btVector3(0, -9.81f, 0));
  159. struct jmeFilterCallback : public btOverlapFilterCallback {
  160. // return true when pairs need collision
  161. virtual bool needBroadphaseCollision(btBroadphaseProxy* proxy0, btBroadphaseProxy * proxy1) const {
  162. // bool collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0;
  163. // collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask);
  164. bool collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0;
  165. collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask);
  166. if (collides) {
  167. btCollisionObject* co0 = (btCollisionObject*) proxy0->m_clientObject;
  168. btCollisionObject* co1 = (btCollisionObject*) proxy1->m_clientObject;
  169. jmeUserPointer *up0 = (jmeUserPointer*) co0 -> getUserPointer();
  170. jmeUserPointer *up1 = (jmeUserPointer*) co1 -> getUserPointer();
  171. if (up0 != NULL && up1 != NULL) {
  172. collides = (up0->group & up1->groups) != 0;
  173. collides = collides && (up1->group & up0->groups);
  174. //add some additional logic here that modified 'collides'
  175. return collides;
  176. }
  177. return false;
  178. }
  179. return collides;
  180. }
  181. };
  182. dynamicsWorld->getPairCache()->setOverlapFilterCallback(new jmeFilterCallback());
  183. dynamicsWorld->setInternalTickCallback(&jmePhysicsSpace::preTickCallback, static_cast<void *> (this), true);
  184. dynamicsWorld->setInternalTickCallback(&jmePhysicsSpace::postTickCallback, static_cast<void *> (this));
  185. if (gContactProcessedCallback == NULL) {
  186. gContactProcessedCallback = &jmePhysicsSpace::contactProcessedCallback;
  187. }
  188. }
  189. void jmePhysicsSpace::preTickCallback(btDynamicsWorld *world, btScalar timeStep) {
  190. jmePhysicsSpace* dynamicsWorld = (jmePhysicsSpace*) world->getWorldUserInfo();
  191. JNIEnv* env = dynamicsWorld->getEnv();
  192. jobject javaPhysicsSpace = env->NewLocalRef(dynamicsWorld->getJavaPhysicsSpace());
  193. if (javaPhysicsSpace != NULL) {
  194. env->CallVoidMethod(javaPhysicsSpace, jmeClasses::PhysicsSpace_preTick, timeStep);
  195. env->DeleteLocalRef(javaPhysicsSpace);
  196. if (env->ExceptionCheck()) {
  197. env->Throw(env->ExceptionOccurred());
  198. return;
  199. }
  200. }
  201. }
  202. void jmePhysicsSpace::postTickCallback(btDynamicsWorld *world, btScalar timeStep) {
  203. jmePhysicsSpace* dynamicsWorld = (jmePhysicsSpace*) world->getWorldUserInfo();
  204. JNIEnv* env = dynamicsWorld->getEnv();
  205. jobject javaPhysicsSpace = env->NewLocalRef(dynamicsWorld->getJavaPhysicsSpace());
  206. if (javaPhysicsSpace != NULL) {
  207. env->CallVoidMethod(javaPhysicsSpace, jmeClasses::PhysicsSpace_postTick, timeStep);
  208. env->DeleteLocalRef(javaPhysicsSpace);
  209. if (env->ExceptionCheck()) {
  210. env->Throw(env->ExceptionOccurred());
  211. return;
  212. }
  213. }
  214. }
  215. bool jmePhysicsSpace::contactProcessedCallback(btManifoldPoint &cp, void *body0, void *body1) {
  216. // printf("contactProcessedCallback %d %dn", body0, body1);
  217. btCollisionObject* co0 = (btCollisionObject*) body0;
  218. jmeUserPointer *up0 = (jmeUserPointer*) co0 -> getUserPointer();
  219. btCollisionObject* co1 = (btCollisionObject*) body1;
  220. jmeUserPointer *up1 = (jmeUserPointer*) co1 -> getUserPointer();
  221. if (up0 != NULL) {
  222. jmePhysicsSpace *dynamicsWorld = (jmePhysicsSpace *)up0->space;
  223. if (dynamicsWorld != NULL) {
  224. JNIEnv* env = dynamicsWorld->getEnv();
  225. jobject javaPhysicsSpace = env->NewLocalRef(dynamicsWorld->getJavaPhysicsSpace());
  226. if (javaPhysicsSpace != NULL) {
  227. jobject javaCollisionObject0 = env->NewLocalRef(up0->javaCollisionObject);
  228. jobject javaCollisionObject1 = env->NewLocalRef(up1->javaCollisionObject);
  229. env->CallVoidMethod(javaPhysicsSpace, jmeClasses::PhysicsSpace_addCollisionEvent, javaCollisionObject0, javaCollisionObject1, (jlong) & cp);
  230. env->DeleteLocalRef(javaPhysicsSpace);
  231. env->DeleteLocalRef(javaCollisionObject0);
  232. env->DeleteLocalRef(javaCollisionObject1);
  233. if (env->ExceptionCheck()) {
  234. env->Throw(env->ExceptionOccurred());
  235. return true;
  236. }
  237. }
  238. }
  239. }
  240. return true;
  241. }
  242. btDynamicsWorld* jmePhysicsSpace::getDynamicsWorld() {
  243. return dynamicsWorld;
  244. }
  245. jobject jmePhysicsSpace::getJavaPhysicsSpace() {
  246. return javaPhysicsSpace;
  247. }
  248. jmePhysicsSpace::~jmePhysicsSpace() {
  249. delete(dynamicsWorld);
  250. }