com_jme3_bullet_objects_PhysicsCharacter.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. /**
  33. * Author: Normen Hansen
  34. */
  35. #include "com_jme3_bullet_objects_PhysicsCharacter.h"
  36. #include "jmeBulletUtil.h"
  37. #include "BulletCollision/CollisionDispatch/btGhostObject.h"
  38. #include "BulletDynamics/Character/btKinematicCharacterController.h"
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. /*
  43. * Class: com_jme3_bullet_objects_PhysicsCharacter
  44. * Method: createGhostObject
  45. * Signature: ()J
  46. */
  47. JNIEXPORT jlong JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_createGhostObject
  48. (JNIEnv * env, jobject object) {
  49. jmeClasses::initJavaClasses(env);
  50. btPairCachingGhostObject* ghost = new btPairCachingGhostObject();
  51. return reinterpret_cast<jlong>(ghost);
  52. }
  53. /*
  54. * Class: com_jme3_bullet_objects_PhysicsCharacter
  55. * Method: setCharacterFlags
  56. * Signature: (J)V
  57. */
  58. JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_setCharacterFlags
  59. (JNIEnv *env, jobject object, jlong ghostId) {
  60. btPairCachingGhostObject* ghost = reinterpret_cast<btPairCachingGhostObject*>(ghostId);
  61. if (ghost == NULL) {
  62. jclass newExc = env->FindClass("java/lang/NullPointerException");
  63. env->ThrowNew(newExc, "The native object does not exist.");
  64. return;
  65. }
  66. ghost->setCollisionFlags(/*ghost->getCollisionFlags() |*/ btCollisionObject::CF_CHARACTER_OBJECT);
  67. ghost->setCollisionFlags(ghost->getCollisionFlags() & ~btCollisionObject::CF_NO_CONTACT_RESPONSE);
  68. }
  69. /*
  70. * Class: com_jme3_bullet_objects_PhysicsCharacter
  71. * Method: createCharacterObject
  72. * Signature: (JJF)J
  73. */
  74. JNIEXPORT jlong JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_createCharacterObject
  75. (JNIEnv *env, jobject object, jlong objectId, jlong shapeId, jfloat stepHeight) {
  76. btPairCachingGhostObject* ghost = reinterpret_cast<btPairCachingGhostObject*>(objectId);
  77. if (ghost == NULL) {
  78. jclass newExc = env->FindClass("java/lang/NullPointerException");
  79. env->ThrowNew(newExc, "The native object does not exist.");
  80. return 0;
  81. }
  82. //TODO: check convexshape!
  83. btConvexShape* shape = reinterpret_cast<btConvexShape*>(shapeId);
  84. btKinematicCharacterController* character = new btKinematicCharacterController(ghost, shape, stepHeight);
  85. return reinterpret_cast<jlong>(character);
  86. }
  87. /*
  88. * Class: com_jme3_bullet_objects_PhysicsCharacter
  89. * Method: warp
  90. * Signature: (JLcom/jme3/math/Vector3f;)V
  91. */
  92. JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_warp
  93. (JNIEnv *env, jobject object, jlong objectId, jobject vector) {
  94. btKinematicCharacterController* character = reinterpret_cast<btKinematicCharacterController*>(objectId);
  95. if (character == NULL) {
  96. jclass newExc = env->FindClass("java/lang/NullPointerException");
  97. env->ThrowNew(newExc, "The native object does not exist.");
  98. return;
  99. }
  100. btVector3 vec = btVector3();
  101. jmeBulletUtil::convert(env, vector, &vec);
  102. character->warp(vec);
  103. }
  104. /*
  105. * Class: com_jme3_bullet_objects_PhysicsCharacter
  106. * Method: setWalkDirection
  107. * Signature: (JLcom/jme3/math/Vector3f;)V
  108. */
  109. JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_setWalkDirection
  110. (JNIEnv *env, jobject object, jlong objectId, jobject vector) {
  111. btKinematicCharacterController* character = reinterpret_cast<btKinematicCharacterController*>(objectId);
  112. if (character == NULL) {
  113. jclass newExc = env->FindClass("java/lang/NullPointerException");
  114. env->ThrowNew(newExc, "The native object does not exist.");
  115. return;
  116. }
  117. btVector3 vec = btVector3();
  118. jmeBulletUtil::convert(env, vector, &vec);
  119. character->setWalkDirection(vec);
  120. }
  121. /*
  122. * Class: com_jme3_bullet_objects_PhysicsCharacter
  123. * Method: setUpAxis
  124. * Signature: (JI)V
  125. */
  126. JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_setUpAxis
  127. (JNIEnv *env, jobject object, jlong objectId, jint value) {
  128. btKinematicCharacterController* character = reinterpret_cast<btKinematicCharacterController*>(objectId);
  129. if (character == NULL) {
  130. jclass newExc = env->FindClass("java/lang/NullPointerException");
  131. env->ThrowNew(newExc, "The native object does not exist.");
  132. return;
  133. }
  134. character->setUpAxis(value);
  135. }
  136. /*
  137. * Class: com_jme3_bullet_objects_PhysicsCharacter
  138. * Method: setFallSpeed
  139. * Signature: (JF)V
  140. */
  141. JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_setFallSpeed
  142. (JNIEnv *env, jobject object, jlong objectId, jfloat value) {
  143. btKinematicCharacterController* character = reinterpret_cast<btKinematicCharacterController*>(objectId);
  144. if (character == NULL) {
  145. jclass newExc = env->FindClass("java/lang/NullPointerException");
  146. env->ThrowNew(newExc, "The native object does not exist.");
  147. return;
  148. }
  149. character->setFallSpeed(value);
  150. }
  151. /*
  152. * Class: com_jme3_bullet_objects_PhysicsCharacter
  153. * Method: setJumpSpeed
  154. * Signature: (JF)V
  155. */
  156. JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_setJumpSpeed
  157. (JNIEnv *env, jobject object, jlong objectId, jfloat value) {
  158. btKinematicCharacterController* character = reinterpret_cast<btKinematicCharacterController*>(objectId);
  159. if (character == NULL) {
  160. jclass newExc = env->FindClass("java/lang/NullPointerException");
  161. env->ThrowNew(newExc, "The native object does not exist.");
  162. return;
  163. }
  164. character->setJumpSpeed(value);
  165. }
  166. /*
  167. * Class: com_jme3_bullet_objects_PhysicsCharacter
  168. * Method: setGravity
  169. * Signature: (JF)V
  170. */
  171. JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_setGravity
  172. (JNIEnv *env, jobject object, jlong objectId, jfloat value) {
  173. btKinematicCharacterController* character = reinterpret_cast<btKinematicCharacterController*>(objectId);
  174. if (character == NULL) {
  175. jclass newExc = env->FindClass("java/lang/NullPointerException");
  176. env->ThrowNew(newExc, "The native object does not exist.");
  177. return;
  178. }
  179. character->setGravity(value);
  180. }
  181. /*
  182. * Class: com_jme3_bullet_objects_PhysicsCharacter
  183. * Method: getGravity
  184. * Signature: (J)F
  185. */
  186. JNIEXPORT jfloat JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_getGravity
  187. (JNIEnv *env, jobject object, jlong objectId) {
  188. btKinematicCharacterController* character = reinterpret_cast<btKinematicCharacterController*>(objectId);
  189. if (character == NULL) {
  190. jclass newExc = env->FindClass("java/lang/NullPointerException");
  191. env->ThrowNew(newExc, "The native object does not exist.");
  192. return 0;
  193. }
  194. return character->getGravity();
  195. }
  196. /*
  197. * Class: com_jme3_bullet_objects_PhysicsCharacter
  198. * Method: setMaxSlope
  199. * Signature: (JF)V
  200. */
  201. JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_setMaxSlope
  202. (JNIEnv *env, jobject object, jlong objectId, jfloat value) {
  203. btKinematicCharacterController* character = reinterpret_cast<btKinematicCharacterController*>(objectId);
  204. if (character == NULL) {
  205. jclass newExc = env->FindClass("java/lang/NullPointerException");
  206. env->ThrowNew(newExc, "The native object does not exist.");
  207. return;
  208. }
  209. character->setMaxSlope(value);
  210. }
  211. /*
  212. * Class: com_jme3_bullet_objects_PhysicsCharacter
  213. * Method: getMaxSlope
  214. * Signature: (J)F
  215. */
  216. JNIEXPORT jfloat JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_getMaxSlope
  217. (JNIEnv *env, jobject object, jlong objectId) {
  218. btKinematicCharacterController* character = reinterpret_cast<btKinematicCharacterController*>(objectId);
  219. if (character == NULL) {
  220. jclass newExc = env->FindClass("java/lang/NullPointerException");
  221. env->ThrowNew(newExc, "The native object does not exist.");
  222. return 0;
  223. }
  224. return character->getMaxSlope();
  225. }
  226. /*
  227. * Class: com_jme3_bullet_objects_PhysicsCharacter
  228. * Method: onGround
  229. * Signature: (J)Z
  230. */
  231. JNIEXPORT jboolean JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_onGround
  232. (JNIEnv *env, jobject object, jlong objectId) {
  233. btKinematicCharacterController* character = reinterpret_cast<btKinematicCharacterController*>(objectId);
  234. if (character == NULL) {
  235. jclass newExc = env->FindClass("java/lang/NullPointerException");
  236. env->ThrowNew(newExc, "The native object does not exist.");
  237. return false;
  238. }
  239. return character->onGround();
  240. }
  241. /*
  242. * Class: com_jme3_bullet_objects_PhysicsCharacter
  243. * Method: jump
  244. * Signature: (J)V
  245. */
  246. JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_jump
  247. (JNIEnv *env, jobject object, jlong objectId) {
  248. btKinematicCharacterController* character = reinterpret_cast<btKinematicCharacterController*>(objectId);
  249. if (character == NULL) {
  250. jclass newExc = env->FindClass("java/lang/NullPointerException");
  251. env->ThrowNew(newExc, "The native object does not exist.");
  252. return;
  253. }
  254. character->jump();
  255. }
  256. /*
  257. * Class: com_jme3_bullet_objects_PhysicsCharacter
  258. * Method: getPhysicsLocation
  259. * Signature: (JLcom/jme3/math/Vector3f;)V
  260. */
  261. JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_getPhysicsLocation
  262. (JNIEnv *env, jobject object, jlong objectId, jobject value) {
  263. btGhostObject* ghost = reinterpret_cast<btGhostObject*>(objectId);
  264. if (ghost == NULL) {
  265. jclass newExc = env->FindClass("java/lang/NullPointerException");
  266. env->ThrowNew(newExc, "The native object does not exist.");
  267. return;
  268. }
  269. jmeBulletUtil::convert(env, &ghost->getWorldTransform().getOrigin(), value);
  270. }
  271. /*
  272. * Class: com_jme3_bullet_objects_PhysicsCharacter
  273. * Method: setCcdSweptSphereRadius
  274. * Signature: (JF)V
  275. */
  276. JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_setCcdSweptSphereRadius
  277. (JNIEnv *env, jobject object, jlong objectId, jfloat value) {
  278. btGhostObject* ghost = reinterpret_cast<btGhostObject*>(objectId);
  279. if (ghost == NULL) {
  280. jclass newExc = env->FindClass("java/lang/NullPointerException");
  281. env->ThrowNew(newExc, "The native object does not exist.");
  282. return;
  283. }
  284. ghost->setCcdSweptSphereRadius(value);
  285. }
  286. /*
  287. * Class: com_jme3_bullet_objects_PhysicsCharacter
  288. * Method: setCcdMotionThreshold
  289. * Signature: (JF)V
  290. */
  291. JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_setCcdMotionThreshold
  292. (JNIEnv *env, jobject object, jlong objectId, jfloat value) {
  293. btGhostObject* ghost = reinterpret_cast<btGhostObject*>(objectId);
  294. if (ghost == NULL) {
  295. jclass newExc = env->FindClass("java/lang/NullPointerException");
  296. env->ThrowNew(newExc, "The native object does not exist.");
  297. return;
  298. }
  299. ghost->setCcdMotionThreshold(value);
  300. }
  301. /*
  302. * Class: com_jme3_bullet_objects_PhysicsCharacter
  303. * Method: getCcdSweptSphereRadius
  304. * Signature: (J)F
  305. */
  306. JNIEXPORT jfloat JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_getCcdSweptSphereRadius
  307. (JNIEnv *env, jobject object, jlong objectId) {
  308. btGhostObject* ghost = reinterpret_cast<btGhostObject*>(objectId);
  309. if (ghost == NULL) {
  310. jclass newExc = env->FindClass("java/lang/NullPointerException");
  311. env->ThrowNew(newExc, "The native object does not exist.");
  312. return 0;
  313. }
  314. return ghost->getCcdSweptSphereRadius();
  315. }
  316. /*
  317. * Class: com_jme3_bullet_objects_PhysicsCharacter
  318. * Method: getCcdMotionThreshold
  319. * Signature: (J)F
  320. */
  321. JNIEXPORT jfloat JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_getCcdMotionThreshold
  322. (JNIEnv *env, jobject object, jlong objectId) {
  323. btGhostObject* ghost = reinterpret_cast<btGhostObject*>(objectId);
  324. if (ghost == NULL) {
  325. jclass newExc = env->FindClass("java/lang/NullPointerException");
  326. env->ThrowNew(newExc, "The native object does not exist.");
  327. return 0;
  328. }
  329. return ghost->getCcdMotionThreshold();
  330. }
  331. /*
  332. * Class: com_jme3_bullet_objects_PhysicsCharacter
  333. * Method: getCcdSquareMotionThreshold
  334. * Signature: (J)F
  335. */
  336. JNIEXPORT jfloat JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_getCcdSquareMotionThreshold
  337. (JNIEnv *env, jobject object, jlong objectId) {
  338. btGhostObject* ghost = reinterpret_cast<btGhostObject*>(objectId);
  339. if (ghost == NULL) {
  340. jclass newExc = env->FindClass("java/lang/NullPointerException");
  341. env->ThrowNew(newExc, "The native object does not exist.");
  342. return 0;
  343. }
  344. return ghost->getCcdSquareMotionThreshold();
  345. }
  346. /*
  347. * Class: com_jme3_bullet_objects_PhysicsCharacter
  348. * Method: finalizeNativeCharacter
  349. * Signature: (J)V
  350. */
  351. JNIEXPORT void JNICALL Java_com_jme3_bullet_objects_PhysicsCharacter_finalizeNativeCharacter
  352. (JNIEnv *env, jobject object, jlong objectId) {
  353. btKinematicCharacterController* character = reinterpret_cast<btKinematicCharacterController*>(objectId);
  354. if (character == NULL) {
  355. jclass newExc = env->FindClass("java/lang/NullPointerException");
  356. env->ThrowNew(newExc, "The native object does not exist.");
  357. return;
  358. }
  359. delete(character);
  360. }
  361. #ifdef __cplusplus
  362. }
  363. #endif