jmePhysicsSpace.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 JNI_VERSION_1_2
  50. vm->AttachCurrentThread((JNIEnv**) &env, NULL);
  51. #else
  52. vm->AttachCurrentThread(&env, NULL);
  53. #endif
  54. }
  55. JNIEnv* jmePhysicsSpace::getEnv() {
  56. attachThread();
  57. return this->env;
  58. }
  59. void jmePhysicsSpace::stepSimulation(jfloat tpf, jint maxSteps, jfloat accuracy) {
  60. dynamicsWorld->stepSimulation(tpf, maxSteps, accuracy);
  61. }
  62. btThreadSupportInterface* jmePhysicsSpace::createSolverThreadSupport(int maxNumThreads) {
  63. #ifdef _WIN32
  64. Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("solverThreads", SolverThreadFunc, SolverlsMemoryFunc, maxNumThreads);
  65. Win32ThreadSupport* threadSupport = new Win32ThreadSupport(threadConstructionInfo);
  66. threadSupport->startSPU();
  67. #elif defined (USE_PTHREADS)
  68. PosixThreadSupport::ThreadConstructionInfo constructionInfo("collision", SolverThreadFunc,
  69. SolverlsMemoryFunc, maxNumThreads);
  70. PosixThreadSupport* threadSupport = new PosixThreadSupport(constructionInfo);
  71. threadSupport->startSPU();
  72. #else
  73. SequentialThreadSupport::SequentialThreadConstructionInfo tci("solverThreads", SolverThreadFunc, SolverlsMemoryFunc);
  74. SequentialThreadSupport* threadSupport = new SequentialThreadSupport(tci);
  75. threadSupport->startSPU();
  76. #endif
  77. return threadSupport;
  78. }
  79. btThreadSupportInterface* jmePhysicsSpace::createDispatchThreadSupport(int maxNumThreads) {
  80. #ifdef _WIN32
  81. Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("solverThreads", processCollisionTask, createCollisionLocalStoreMemory, maxNumThreads);
  82. Win32ThreadSupport* threadSupport = new Win32ThreadSupport(threadConstructionInfo);
  83. threadSupport->startSPU();
  84. #elif defined (USE_PTHREADS)
  85. PosixThreadSupport::ThreadConstructionInfo solverConstructionInfo("solver", processCollisionTask,
  86. createCollisionLocalStoreMemory, maxNumThreads);
  87. PosixThreadSupport* threadSupport = new PosixThreadSupport(solverConstructionInfo);
  88. threadSupport->startSPU();
  89. #else
  90. SequentialThreadSupport::SequentialThreadConstructionInfo tci("solverThreads", processCollisionTask, createCollisionLocalStoreMemory);
  91. SequentialThreadSupport* threadSupport = new SequentialThreadSupport(tci);
  92. threadSupport->startSPU();
  93. #endif
  94. return threadSupport;
  95. }
  96. void jmePhysicsSpace::createPhysicsSpace(jfloat minX, jfloat minY, jfloat minZ, jfloat maxX, jfloat maxY, jfloat maxZ, jint broadphaseId, jboolean threading) {
  97. // collision configuration contains default setup for memory, collision setup
  98. btDefaultCollisionConstructionInfo cci;
  99. // if(threading){
  100. // cci.m_defaultMaxPersistentManifoldPoolSize = 32768;
  101. // }
  102. btCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration(cci);
  103. btVector3 min = btVector3(minX, minY, minZ);
  104. btVector3 max = btVector3(maxX, maxY, maxZ);
  105. btBroadphaseInterface* broadphase;
  106. switch (broadphaseId) {
  107. case 0:
  108. broadphase = new btSimpleBroadphase();
  109. break;
  110. case 1:
  111. broadphase = new btAxisSweep3(min, max);
  112. break;
  113. case 2:
  114. //TODO: 32bit!
  115. broadphase = new btAxisSweep3(min, max);
  116. break;
  117. case 3:
  118. broadphase = new btDbvtBroadphase();
  119. break;
  120. case 4:
  121. // broadphase = new btGpu3DGridBroadphase(
  122. // min, max,
  123. // 20, 20, 20,
  124. // 10000, 1000, 25);
  125. break;
  126. }
  127. btCollisionDispatcher* dispatcher;
  128. btConstraintSolver* solver;
  129. // use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded)
  130. if (threading) {
  131. btThreadSupportInterface* dispatchThreads = createDispatchThreadSupport(4);
  132. dispatcher = new SpuGatheringCollisionDispatcher(dispatchThreads, 4, collisionConfiguration);
  133. dispatcher->setDispatcherFlags(btCollisionDispatcher::CD_DISABLE_CONTACTPOOL_DYNAMIC_ALLOCATION);
  134. } else {
  135. dispatcher = new btCollisionDispatcher(collisionConfiguration);
  136. }
  137. // the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded)
  138. if (threading) {
  139. btThreadSupportInterface* solverThreads = createSolverThreadSupport(4);
  140. solver = new btParallelConstraintSolver(solverThreads);
  141. } else {
  142. solver = new btSequentialImpulseConstraintSolver;
  143. }
  144. //create dynamics world
  145. btDiscreteDynamicsWorld* world = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
  146. dynamicsWorld = world;
  147. dynamicsWorld->setWorldUserInfo(this);
  148. //parallel solver requires the contacts to be in a contiguous pool, so avoid dynamic allocation
  149. if (threading) {
  150. world->getSimulationIslandManager()->setSplitIslands(false);
  151. world->getSolverInfo().m_numIterations = 4;
  152. world->getSolverInfo().m_solverMode = SOLVER_SIMD + SOLVER_USE_WARMSTARTING; //+SOLVER_RANDMIZE_ORDER;
  153. world->getDispatchInfo().m_enableSPU = true;
  154. }
  155. broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(new btGhostPairCallback());
  156. dynamicsWorld->setGravity(btVector3(0, -9.81f, 0));
  157. struct jmeFilterCallback : public btOverlapFilterCallback {
  158. // return true when pairs need collision
  159. virtual bool needBroadphaseCollision(btBroadphaseProxy* proxy0, btBroadphaseProxy * proxy1) const {
  160. // bool collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0;
  161. // collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask);
  162. bool collides = (proxy0->m_collisionFilterGroup & proxy1->m_collisionFilterMask) != 0;
  163. collides = collides && (proxy1->m_collisionFilterGroup & proxy0->m_collisionFilterMask);
  164. if (collides) {
  165. btCollisionObject* co0 = (btCollisionObject*) proxy0->m_clientObject;
  166. btCollisionObject* co1 = (btCollisionObject*) proxy1->m_clientObject;
  167. jmeUserPointer *up0 = (jmeUserPointer*) co0 -> getUserPointer();
  168. jmeUserPointer *up1 = (jmeUserPointer*) co1 -> getUserPointer();
  169. if (up0 != NULL && up1 != NULL) {
  170. collides = (up0->group & up1->groups) != 0;
  171. collides = collides && (up1->group & up0->groups);
  172. //add some additional logic here that modified 'collides'
  173. return collides;
  174. }
  175. return false;
  176. }
  177. return collides;
  178. }
  179. };
  180. dynamicsWorld->getPairCache()->setOverlapFilterCallback(new jmeFilterCallback());
  181. dynamicsWorld->setInternalTickCallback(&jmePhysicsSpace::preTickCallback, static_cast<void *> (this), true);
  182. dynamicsWorld->setInternalTickCallback(&jmePhysicsSpace::postTickCallback, static_cast<void *> (this));
  183. if (gContactProcessedCallback == NULL) {
  184. gContactProcessedCallback = &jmePhysicsSpace::contactProcessedCallback;
  185. }
  186. }
  187. void jmePhysicsSpace::preTickCallback(btDynamicsWorld *world, btScalar timeStep) {
  188. jmePhysicsSpace* dynamicsWorld = (jmePhysicsSpace*) world->getWorldUserInfo();
  189. JNIEnv* env = dynamicsWorld->getEnv();
  190. jobject javaPhysicsSpace = env->NewLocalRef(dynamicsWorld->getJavaPhysicsSpace());
  191. if (javaPhysicsSpace != NULL) {
  192. env->CallVoidMethod(javaPhysicsSpace, jmeClasses::PhysicsSpace_preTick, timeStep);
  193. env->DeleteLocalRef(javaPhysicsSpace);
  194. if (env->ExceptionCheck()) {
  195. env->Throw(env->ExceptionOccurred());
  196. return;
  197. }
  198. }
  199. }
  200. void jmePhysicsSpace::postTickCallback(btDynamicsWorld *world, btScalar timeStep) {
  201. jmePhysicsSpace* dynamicsWorld = (jmePhysicsSpace*) world->getWorldUserInfo();
  202. JNIEnv* env = dynamicsWorld->getEnv();
  203. jobject javaPhysicsSpace = env->NewLocalRef(dynamicsWorld->getJavaPhysicsSpace());
  204. if (javaPhysicsSpace != NULL) {
  205. env->CallVoidMethod(javaPhysicsSpace, jmeClasses::PhysicsSpace_postTick, timeStep);
  206. env->DeleteLocalRef(javaPhysicsSpace);
  207. if (env->ExceptionCheck()) {
  208. env->Throw(env->ExceptionOccurred());
  209. return;
  210. }
  211. }
  212. }
  213. bool jmePhysicsSpace::contactProcessedCallback(btManifoldPoint &cp, void *body0, void *body1) {
  214. // printf("contactProcessedCallback %d %dn", body0, body1);
  215. btCollisionObject* co0 = (btCollisionObject*) body0;
  216. jmeUserPointer *up0 = (jmeUserPointer*) co0 -> getUserPointer();
  217. btCollisionObject* co1 = (btCollisionObject*) body1;
  218. jmeUserPointer *up1 = (jmeUserPointer*) co1 -> getUserPointer();
  219. if (up0 != NULL) {
  220. jmePhysicsSpace *dynamicsWorld = (jmePhysicsSpace *)up0->space;
  221. if (dynamicsWorld != NULL) {
  222. JNIEnv* env = dynamicsWorld->getEnv();
  223. jobject javaPhysicsSpace = env->NewLocalRef(dynamicsWorld->getJavaPhysicsSpace());
  224. if (javaPhysicsSpace != NULL) {
  225. jobject javaCollisionObject0 = env->NewLocalRef(up0->javaCollisionObject);
  226. jobject javaCollisionObject1 = env->NewLocalRef(up1->javaCollisionObject);
  227. env->CallVoidMethod(javaPhysicsSpace, jmeClasses::PhysicsSpace_addCollisionEvent, javaCollisionObject0, javaCollisionObject1, (jlong) & cp);
  228. env->DeleteLocalRef(javaPhysicsSpace);
  229. env->DeleteLocalRef(javaCollisionObject0);
  230. env->DeleteLocalRef(javaCollisionObject1);
  231. if (env->ExceptionCheck()) {
  232. env->Throw(env->ExceptionOccurred());
  233. return true;
  234. }
  235. }
  236. }
  237. }
  238. return true;
  239. }
  240. btDynamicsWorld* jmePhysicsSpace::getDynamicsWorld() {
  241. return dynamicsWorld;
  242. }
  243. jobject jmePhysicsSpace::getJavaPhysicsSpace() {
  244. return javaPhysicsSpace;
  245. }
  246. jmePhysicsSpace::~jmePhysicsSpace() {
  247. delete(dynamicsWorld);
  248. }