com_jme3_bullet_PhysicsSpace.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Copyright (c) 2009-2012 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 "com_jme3_bullet_PhysicsSpace.h"
  33. #include "jmePhysicsSpace.h"
  34. #include "jmeBulletUtil.h"
  35. /**
  36. * Author: Normen Hansen
  37. */
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /*
  42. * Class: com_jme3_bullet_PhysicsSpace
  43. * Method: createPhysicsSpace
  44. * Signature: (FFFFFFI)J
  45. */
  46. JNIEXPORT jlong JNICALL Java_com_jme3_bullet_PhysicsSpace_createPhysicsSpace
  47. (JNIEnv * env, jobject object, jfloat minX, jfloat minY, jfloat minZ, jfloat maxX, jfloat maxY, jfloat maxZ, jint broadphase, jboolean threading) {
  48. jmeClasses::initJavaClasses(env);
  49. jmePhysicsSpace* space = new jmePhysicsSpace(env, object);
  50. if (space == NULL) {
  51. jclass newExc = env->FindClass("java/lang/NullPointerException");
  52. env->ThrowNew(newExc, "The physics space has not been created.");
  53. return 0;
  54. }
  55. space->createPhysicsSpace(minX, minY, minZ, maxX, maxY, maxZ, broadphase, threading);
  56. return reinterpret_cast<jlong>(space);
  57. }
  58. /*
  59. * Class: com_jme3_bullet_PhysicsSpace
  60. * Method: stepSimulation
  61. * Signature: (JFIF)V
  62. */
  63. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_stepSimulation
  64. (JNIEnv * env, jobject object, jlong spaceId, jfloat tpf, jint maxSteps, jfloat accuracy) {
  65. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  66. if (space == NULL) {
  67. jclass newExc = env->FindClass("java/lang/NullPointerException");
  68. env->ThrowNew(newExc, "The physics space does not exist.");
  69. return;
  70. }
  71. space->stepSimulation(tpf, maxSteps, accuracy);
  72. }
  73. /*
  74. * Class: com_jme3_bullet_PhysicsSpace
  75. * Method: addCollisionObject
  76. * Signature: (JJ)V
  77. */
  78. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_addCollisionObject
  79. (JNIEnv * env, jobject object, jlong spaceId, jlong objectId) {
  80. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  81. btCollisionObject* collisionObject = reinterpret_cast<btCollisionObject*>(objectId);
  82. if (space == NULL) {
  83. jclass newExc = env->FindClass("java/lang/NullPointerException");
  84. env->ThrowNew(newExc, "The physics space does not exist.");
  85. return;
  86. }
  87. if (collisionObject == NULL) {
  88. jclass newExc = env->FindClass("java/lang/NullPointerException");
  89. env->ThrowNew(newExc, "The collision object does not exist.");
  90. return;
  91. }
  92. jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer();
  93. userPointer -> space = space;
  94. space->getDynamicsWorld()->addCollisionObject(collisionObject);
  95. }
  96. /*
  97. * Class: com_jme3_bullet_PhysicsSpace
  98. * Method: removeCollisionObject
  99. * Signature: (JJ)V
  100. */
  101. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_removeCollisionObject
  102. (JNIEnv * env, jobject object, jlong spaceId, jlong objectId) {
  103. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  104. btCollisionObject* collisionObject = reinterpret_cast<btCollisionObject*>(objectId);
  105. if (space == NULL) {
  106. jclass newExc = env->FindClass("java/lang/NullPointerException");
  107. env->ThrowNew(newExc, "The physics space does not exist.");
  108. return;
  109. }
  110. if (collisionObject == NULL) {
  111. jclass newExc = env->FindClass("java/lang/NullPointerException");
  112. env->ThrowNew(newExc, "The collision object does not exist.");
  113. return;
  114. }
  115. space->getDynamicsWorld()->removeCollisionObject(collisionObject);
  116. jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer();
  117. userPointer -> space = NULL;
  118. }
  119. /*
  120. * Class: com_jme3_bullet_PhysicsSpace
  121. * Method: addRigidBody
  122. * Signature: (JJ)V
  123. */
  124. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_addRigidBody
  125. (JNIEnv * env, jobject object, jlong spaceId, jlong rigidBodyId) {
  126. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  127. btRigidBody* collisionObject = reinterpret_cast<btRigidBody*>(rigidBodyId);
  128. if (space == NULL) {
  129. jclass newExc = env->FindClass("java/lang/NullPointerException");
  130. env->ThrowNew(newExc, "The physics space does not exist.");
  131. return;
  132. }
  133. if (collisionObject == NULL) {
  134. jclass newExc = env->FindClass("java/lang/NullPointerException");
  135. env->ThrowNew(newExc, "The collision object does not exist.");
  136. return;
  137. }
  138. jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer();
  139. userPointer -> space = space;
  140. space->getDynamicsWorld()->addRigidBody(collisionObject);
  141. }
  142. /*
  143. * Class: com_jme3_bullet_PhysicsSpace
  144. * Method: removeRigidBody
  145. * Signature: (JJ)V
  146. */
  147. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_removeRigidBody
  148. (JNIEnv * env, jobject object, jlong spaceId, jlong rigidBodyId) {
  149. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  150. btRigidBody* collisionObject = reinterpret_cast<btRigidBody*>(rigidBodyId);
  151. if (space == NULL) {
  152. jclass newExc = env->FindClass("java/lang/NullPointerException");
  153. env->ThrowNew(newExc, "The physics space does not exist.");
  154. return;
  155. }
  156. if (collisionObject == NULL) {
  157. jclass newExc = env->FindClass("java/lang/NullPointerException");
  158. env->ThrowNew(newExc, "The collision object does not exist.");
  159. return;
  160. }
  161. jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer();
  162. userPointer -> space = NULL;
  163. space->getDynamicsWorld()->removeRigidBody(collisionObject);
  164. }
  165. /*
  166. * Class: com_jme3_bullet_PhysicsSpace
  167. * Method: addCharacterObject
  168. * Signature: (JJ)V
  169. */
  170. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_addCharacterObject
  171. (JNIEnv * env, jobject object, jlong spaceId, jlong objectId) {
  172. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  173. btCollisionObject* collisionObject = reinterpret_cast<btCollisionObject*>(objectId);
  174. if (space == NULL) {
  175. jclass newExc = env->FindClass("java/lang/NullPointerException");
  176. env->ThrowNew(newExc, "The physics space does not exist.");
  177. return;
  178. }
  179. if (collisionObject == NULL) {
  180. jclass newExc = env->FindClass("java/lang/NullPointerException");
  181. env->ThrowNew(newExc, "The collision object does not exist.");
  182. return;
  183. }
  184. jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer();
  185. userPointer -> space = space;
  186. space->getDynamicsWorld()->addCollisionObject(collisionObject,
  187. btBroadphaseProxy::CharacterFilter,
  188. btBroadphaseProxy::StaticFilter | btBroadphaseProxy::DefaultFilter
  189. );
  190. }
  191. /*
  192. * Class: com_jme3_bullet_PhysicsSpace
  193. * Method: removeCharacterObject
  194. * Signature: (JJ)V
  195. */
  196. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_removeCharacterObject
  197. (JNIEnv * env, jobject object, jlong spaceId, jlong objectId) {
  198. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  199. btCollisionObject* collisionObject = reinterpret_cast<btCollisionObject*>(objectId);
  200. if (space == NULL) {
  201. jclass newExc = env->FindClass("java/lang/NullPointerException");
  202. env->ThrowNew(newExc, "The physics space does not exist.");
  203. return;
  204. }
  205. if (collisionObject == NULL) {
  206. jclass newExc = env->FindClass("java/lang/NullPointerException");
  207. env->ThrowNew(newExc, "The collision object does not exist.");
  208. return;
  209. }
  210. jmeUserPointer *userPointer = (jmeUserPointer*)collisionObject->getUserPointer();
  211. userPointer -> space = NULL;
  212. space->getDynamicsWorld()->removeCollisionObject(collisionObject);
  213. }
  214. /*
  215. * Class: com_jme3_bullet_PhysicsSpace
  216. * Method: addAction
  217. * Signature: (JJ)V
  218. */
  219. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_addAction
  220. (JNIEnv * env, jobject object, jlong spaceId, jlong objectId) {
  221. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  222. btActionInterface* actionObject = reinterpret_cast<btActionInterface*>(objectId);
  223. if (space == NULL) {
  224. jclass newExc = env->FindClass("java/lang/NullPointerException");
  225. env->ThrowNew(newExc, "The physics space does not exist.");
  226. return;
  227. }
  228. if (actionObject == NULL) {
  229. jclass newExc = env->FindClass("java/lang/NullPointerException");
  230. env->ThrowNew(newExc, "The action object does not exist.");
  231. return;
  232. }
  233. space->getDynamicsWorld()->addAction(actionObject);
  234. }
  235. /*
  236. * Class: com_jme3_bullet_PhysicsSpace
  237. * Method: removeAction
  238. * Signature: (JJ)V
  239. */
  240. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_removeAction
  241. (JNIEnv * env, jobject object, jlong spaceId, jlong objectId) {
  242. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  243. btActionInterface* actionObject = reinterpret_cast<btActionInterface*>(objectId);
  244. if (space == NULL) {
  245. jclass newExc = env->FindClass("java/lang/NullPointerException");
  246. env->ThrowNew(newExc, "The physics space does not exist.");
  247. return;
  248. }
  249. if (actionObject == NULL) {
  250. jclass newExc = env->FindClass("java/lang/NullPointerException");
  251. env->ThrowNew(newExc, "The action object does not exist.");
  252. return;
  253. }
  254. space->getDynamicsWorld()->removeAction(actionObject);
  255. }
  256. /*
  257. * Class: com_jme3_bullet_PhysicsSpace
  258. * Method: addVehicle
  259. * Signature: (JJ)V
  260. */
  261. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_addVehicle
  262. (JNIEnv * env, jobject object, jlong spaceId, jlong objectId) {
  263. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  264. btActionInterface* actionObject = reinterpret_cast<btActionInterface*>(objectId);
  265. if (space == NULL) {
  266. jclass newExc = env->FindClass("java/lang/NullPointerException");
  267. env->ThrowNew(newExc, "The physics space does not exist.");
  268. return;
  269. }
  270. if (actionObject == NULL) {
  271. jclass newExc = env->FindClass("java/lang/NullPointerException");
  272. env->ThrowNew(newExc, "The vehicle object does not exist.");
  273. return;
  274. }
  275. space->getDynamicsWorld()->addVehicle(actionObject);
  276. }
  277. /*
  278. * Class: com_jme3_bullet_PhysicsSpace
  279. * Method: removeVehicle
  280. * Signature: (JJ)V
  281. */
  282. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_removeVehicle
  283. (JNIEnv * env, jobject object, jlong spaceId, jlong objectId) {
  284. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  285. btActionInterface* actionObject = reinterpret_cast<btActionInterface*>(objectId);
  286. if (space == NULL) {
  287. jclass newExc = env->FindClass("java/lang/NullPointerException");
  288. env->ThrowNew(newExc, "The physics space does not exist.");
  289. return;
  290. }
  291. if (actionObject == NULL) {
  292. jclass newExc = env->FindClass("java/lang/NullPointerException");
  293. env->ThrowNew(newExc, "The action object does not exist.");
  294. return;
  295. }
  296. space->getDynamicsWorld()->removeVehicle(actionObject);
  297. }
  298. /*
  299. * Class: com_jme3_bullet_PhysicsSpace
  300. * Method: addConstraint
  301. * Signature: (JJ)V
  302. */
  303. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_addConstraint
  304. (JNIEnv * env, jobject object, jlong spaceId, jlong objectId) {
  305. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  306. btTypedConstraint* constraint = reinterpret_cast<btTypedConstraint*>(objectId);
  307. if (space == NULL) {
  308. jclass newExc = env->FindClass("java/lang/NullPointerException");
  309. env->ThrowNew(newExc, "The physics space does not exist.");
  310. return;
  311. }
  312. if (constraint == NULL) {
  313. jclass newExc = env->FindClass("java/lang/NullPointerException");
  314. env->ThrowNew(newExc, "The constraint object does not exist.");
  315. return;
  316. }
  317. space->getDynamicsWorld()->addConstraint(constraint);
  318. }
  319. /*
  320. * Class: com_jme3_bullet_PhysicsSpace
  321. * Method: addConstraint
  322. * Signature: (JJZ)V
  323. */
  324. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_addConstraintC
  325. (JNIEnv * env, jobject object, jlong spaceId, jlong objectId, jboolean collision) {
  326. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  327. btTypedConstraint* constraint = reinterpret_cast<btTypedConstraint*>(objectId);
  328. if (space == NULL) {
  329. jclass newExc = env->FindClass("java/lang/NullPointerException");
  330. env->ThrowNew(newExc, "The physics space does not exist.");
  331. return;
  332. }
  333. if (constraint == NULL) {
  334. jclass newExc = env->FindClass("java/lang/NullPointerException");
  335. env->ThrowNew(newExc, "The constraint object does not exist.");
  336. return;
  337. }
  338. space->getDynamicsWorld()->addConstraint(constraint, collision);
  339. }
  340. /*
  341. * Class: com_jme3_bullet_PhysicsSpace
  342. * Method: removeConstraint
  343. * Signature: (JJ)V
  344. */
  345. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_removeConstraint
  346. (JNIEnv * env, jobject object, jlong spaceId, jlong objectId) {
  347. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  348. btTypedConstraint* constraint = reinterpret_cast<btTypedConstraint*>(objectId);
  349. if (space == NULL) {
  350. jclass newExc = env->FindClass("java/lang/NullPointerException");
  351. env->ThrowNew(newExc, "The physics space does not exist.");
  352. return;
  353. }
  354. if (constraint == NULL) {
  355. jclass newExc = env->FindClass("java/lang/NullPointerException");
  356. env->ThrowNew(newExc, "The constraint object does not exist.");
  357. return;
  358. }
  359. space->getDynamicsWorld()->removeConstraint(constraint);
  360. }
  361. /*
  362. * Class: com_jme3_bullet_PhysicsSpace
  363. * Method: setGravity
  364. * Signature: (JLcom/jme3/math/Vector3f;)V
  365. */
  366. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_setGravity
  367. (JNIEnv * env, jobject object, jlong spaceId, jobject vector) {
  368. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  369. if (space == NULL) {
  370. jclass newExc = env->FindClass("java/lang/NullPointerException");
  371. env->ThrowNew(newExc, "The physics space does not exist.");
  372. return;
  373. }
  374. btVector3 gravity = btVector3();
  375. jmeBulletUtil::convert(env, vector, &gravity);
  376. space->getDynamicsWorld()->setGravity(gravity);
  377. }
  378. /*
  379. * Class: com_jme3_bullet_PhysicsSpace
  380. * Method: initNativePhysics
  381. * Signature: ()V
  382. */
  383. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_initNativePhysics
  384. (JNIEnv * env, jclass clazz) {
  385. jmeClasses::initJavaClasses(env);
  386. }
  387. /*
  388. * Class: com_jme3_bullet_PhysicsSpace
  389. * Method: finalizeNative
  390. * Signature: (J)V
  391. */
  392. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_finalizeNative
  393. (JNIEnv * env, jobject object, jlong spaceId) {
  394. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  395. if (space == NULL) {
  396. return;
  397. }
  398. delete(space);
  399. }
  400. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_rayTest_1native
  401. (JNIEnv * env, jobject object, jobject from, jobject to, jlong spaceId, jobject resultlist, jint flags) {
  402. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*> (spaceId);
  403. if (space == NULL) {
  404. jclass newExc = env->FindClass("java/lang/NullPointerException");
  405. env->ThrowNew(newExc, "The physics space does not exist.");
  406. return;
  407. }
  408. struct AllRayResultCallback : public btCollisionWorld::RayResultCallback {
  409. AllRayResultCallback(const btVector3& rayFromWorld, const btVector3 & rayToWorld) : m_rayFromWorld(rayFromWorld), m_rayToWorld(rayToWorld) {
  410. }
  411. jobject resultlist;
  412. JNIEnv* env;
  413. btVector3 m_rayFromWorld; //used to calculate hitPointWorld from hitFraction
  414. btVector3 m_rayToWorld;
  415. btVector3 m_hitNormalWorld;
  416. btVector3 m_hitPointWorld;
  417. virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult& rayResult, bool normalInWorldSpace) {
  418. if (normalInWorldSpace) {
  419. m_hitNormalWorld = rayResult.m_hitNormalLocal;
  420. } else {
  421. m_hitNormalWorld = m_collisionObject->getWorldTransform().getBasis() * rayResult.m_hitNormalLocal;
  422. }
  423. m_hitPointWorld.setInterpolate3(m_rayFromWorld, m_rayToWorld, rayResult.m_hitFraction);
  424. jmeBulletUtil::addResult(env, resultlist, &m_hitNormalWorld, &m_hitPointWorld, rayResult.m_hitFraction, rayResult.m_collisionObject);
  425. return 1.f;
  426. }
  427. };
  428. btVector3 native_to = btVector3();
  429. jmeBulletUtil::convert(env, to, &native_to);
  430. btVector3 native_from = btVector3();
  431. jmeBulletUtil::convert(env, from, &native_from);
  432. AllRayResultCallback resultCallback(native_from, native_to);
  433. resultCallback.env = env;
  434. resultCallback.resultlist = resultlist;
  435. resultCallback.m_flags = flags;
  436. space->getDynamicsWorld()->rayTest(native_from, native_to, resultCallback);
  437. return;
  438. }
  439. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_sweepTest_1native
  440. (JNIEnv * env, jobject object, jlong shapeId, jobject from, jobject to, jlong spaceId, jobject resultlist, jfloat allowedCcdPenetration) {
  441. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*> (spaceId);
  442. if (space == NULL) {
  443. jclass newExc = env->FindClass("java/lang/NullPointerException");
  444. env->ThrowNew(newExc, "The physics space does not exist.");
  445. return;
  446. }
  447. btCollisionShape* shape = reinterpret_cast<btCollisionShape*> (shapeId);
  448. if (shape == NULL) {
  449. jclass newExc = env->FindClass("java/lang/NullPointerException");
  450. env->ThrowNew(newExc, "The shape does not exist.");
  451. return;
  452. }
  453. struct AllConvexResultCallback : public btCollisionWorld::ConvexResultCallback {
  454. AllConvexResultCallback(const btTransform& convexFromWorld, const btTransform & convexToWorld) : m_convexFromWorld(convexFromWorld), m_convexToWorld(convexToWorld) {
  455. }
  456. jobject resultlist;
  457. JNIEnv* env;
  458. btTransform m_convexFromWorld; //used to calculate hitPointWorld from hitFraction
  459. btTransform m_convexToWorld;
  460. btVector3 m_hitNormalWorld;
  461. btVector3 m_hitPointWorld;
  462. virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult& convexResult, bool normalInWorldSpace) {
  463. if (normalInWorldSpace) {
  464. m_hitNormalWorld = convexResult.m_hitNormalLocal;
  465. }
  466. else {
  467. m_hitNormalWorld = convexResult.m_hitCollisionObject->getWorldTransform().getBasis() * convexResult.m_hitNormalLocal;
  468. }
  469. m_hitPointWorld.setInterpolate3(m_convexFromWorld.getBasis() * m_convexFromWorld.getOrigin(), m_convexToWorld.getBasis() * m_convexToWorld.getOrigin(), convexResult.m_hitFraction);
  470. jmeBulletUtil::addSweepResult(env, resultlist, &m_hitNormalWorld, &m_hitPointWorld, convexResult.m_hitFraction, convexResult.m_hitCollisionObject);
  471. return 1.f;
  472. }
  473. };
  474. btTransform native_to = btTransform();
  475. jmeBulletUtil::convert(env, to, &native_to);
  476. btTransform native_from = btTransform();
  477. jmeBulletUtil::convert(env, from, &native_from);
  478. btScalar native_allowed_ccd_penetration = btScalar(allowedCcdPenetration);
  479. AllConvexResultCallback resultCallback(native_from, native_to);
  480. resultCallback.env = env;
  481. resultCallback.resultlist = resultlist;
  482. space->getDynamicsWorld()->convexSweepTest((btConvexShape *) shape, native_from, native_to, resultCallback, native_allowed_ccd_penetration);
  483. return;
  484. }
  485. JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_setSolverNumIterations
  486. (JNIEnv *env, jobject object, jlong spaceId, jint value) {
  487. jmePhysicsSpace* space = reinterpret_cast<jmePhysicsSpace*>(spaceId);
  488. if (space == NULL) {
  489. jclass newExc = env->FindClass("java/lang/NullPointerException");
  490. env->ThrowNew(newExc, "The physics space does not exist.");
  491. return;
  492. }
  493. space->getDynamicsWorld()->getSolverInfo().m_numIterations = value;
  494. }
  495. #ifdef __cplusplus
  496. }
  497. #endif