From cd1d14500571da1d771b9748d6eda912edeb40bd Mon Sep 17 00:00:00 2001 From: Kirill Vainer Date: Mon, 27 Apr 2015 19:03:43 -0400 Subject: [PATCH] PhysicsSpace: add method to set solver iterations Thanks to Seppes (see thread: http://hub.jmonkeyengine.org/t/how-to-access-native-bullets-constraintsolver-numiterations) --- .../cpp/com_jme3_bullet_PhysicsSpace.cpp | 12 +++++++++ .../native/cpp/com_jme3_bullet_PhysicsSpace.h | 8 ++++++ .../java/com/jme3/bullet/PhysicsSpace.java | 26 ++++++++++++++++++- .../java/com/jme3/bullet/PhysicsSpace.java | 15 +++++++++++ 4 files changed, 60 insertions(+), 1 deletion(-) diff --git a/jme3-bullet-native/src/native/cpp/com_jme3_bullet_PhysicsSpace.cpp b/jme3-bullet-native/src/native/cpp/com_jme3_bullet_PhysicsSpace.cpp index f9be6a6ad..3ecf7fd6b 100644 --- a/jme3-bullet-native/src/native/cpp/com_jme3_bullet_PhysicsSpace.cpp +++ b/jme3-bullet-native/src/native/cpp/com_jme3_bullet_PhysicsSpace.cpp @@ -528,6 +528,18 @@ extern "C" { space->getDynamicsWorld()->convexSweepTest((btConvexShape *) shape, native_from, native_to, resultCallback, native_allowed_ccd_penetration); return; } + + JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_setSolverNumIterations + (JNIEnv *env, jobject object, jlong spaceId, jint value) { + jmePhysicsSpace* space = reinterpret_cast(spaceId); + if (space == NULL) { + jclass newExc = env->FindClass("java/lang/NullPointerException"); + env->ThrowNew(newExc, "The physics space does not exist."); + return; + } + + space->getDynamicsWorld()->getSolverInfo().m_numIterations = value; + } #ifdef __cplusplus } diff --git a/jme3-bullet-native/src/native/cpp/com_jme3_bullet_PhysicsSpace.h b/jme3-bullet-native/src/native/cpp/com_jme3_bullet_PhysicsSpace.h index b499ff04c..0380c17b0 100644 --- a/jme3-bullet-native/src/native/cpp/com_jme3_bullet_PhysicsSpace.h +++ b/jme3-bullet-native/src/native/cpp/com_jme3_bullet_PhysicsSpace.h @@ -174,6 +174,14 @@ JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_finalizeNative JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_sweepTest_1native (JNIEnv *, jobject, jlong, jobject, jobject, jlong, jobject, jfloat); +/* + * Class: com_jme3_bullet_PhysicsSpace + * Method: setSolverNumIterations + * Signature: (JI)V + */ +JNIEXPORT void JNICALL Java_com_jme3_bullet_PhysicsSpace_setSolverNumIterations +(JNIEnv *, jobject, jlong, jint); + #ifdef __cplusplus } #endif diff --git a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java index 4485edcd7..edea485ef 100644 --- a/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -102,6 +102,7 @@ public class PhysicsSpace { private Vector3f worldMax = new Vector3f(10000f, 10000f, 10000f); private float accuracy = 1f / 60f; private int maxSubSteps = 4, rayTestFlags = 1 << 2; + private int solverNumIterations = 10; static { // System.loadLibrary("bulletjme"); @@ -871,7 +872,7 @@ public class PhysicsSpace { } */ - + /** * destroys the current PhysicsSpace so that a new one can be created */ @@ -958,6 +959,29 @@ public class PhysicsSpace { this.worldMax.set(worldMax); } + /** + * Set the number of iterations used by the contact solver. + * + * The default is 10. Use 4 for low quality, 20 for high quality. + * + * @param numIterations The number of iterations used by the contact & constraint solver. + */ + public void setSolverNumIterations(int numIterations) { + this.solverNumIterations = numIterations; + setSolverNumIterations(physicsSpaceId, numIterations); + } + + /** + * Get the number of iterations used by the contact solver. + * + * @return The number of iterations used by the contact & constraint solver. + */ + public int getSolverNumIterations() { + return solverNumIterations; + } + + private static native void setSolverNumIterations(long physicsSpaceId, int numIterations); + public static native void initNativePhysics(); /** diff --git a/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java b/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java index 3c8a3b225..016440bf0 100644 --- a/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java +++ b/jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java @@ -875,7 +875,22 @@ public class PhysicsSpace { public void setWorldMax(Vector3f worldMax) { this.worldMax.set(worldMax); } + + /** + * Set the number of iterations used by the contact solver. + * + * The default is 10. Use 4 for low quality, 20 for high quality. + * + * @param numIterations The number of iterations used by the contact & constraint solver. + */ + public void setSolverNumIterations(int numIterations) { + dynamicsWorld.getSolverInfo().numIterations = numIterations; + } + public int getSolverNumIterations() { + return dynamicsWorld.getSolverInfo().numIterations; + } + /** * interface with Broadphase types */