If I recall I got busy doing something else and was never able to get back to this. By all means play around with it and see if you can fix the problems. If you can submit a patch and I'd be happy to apply it to the repository.
I've been using the following modifications for the past few days. It seems to work pretty well, no errors. Keep in mind that I am probably not using any of the more exotic features of JME. Specifically, updateGeometricState() calls the update methods on the various controllers that can be added to Spatials, and some of those may access the GL thread.
I do still have a concern with this class, which is that the PhysicsThread keeps running even when its disabled. It seems to me that the thread should be put to sleep when its not enabled, as opposed to checking for the condition on each update call.
Index: PhysicsMultithreadedGameState.java
===================================================================
RCS file: /cvs/jmephysics/src/com/jmex/physics/util/states/PhysicsMultithreadedGameState.java,v
retrieving revision 1.3
diff -u -r1.3 PhysicsMultithreadedGameState.java
--- PhysicsMultithreadedGameState.java 22 Sep 2007 14:28:38 -0000 1.3
+++ PhysicsMultithreadedGameState.java 13 Mar 2008 01:05:04 -0000
@@ -37,6 +37,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;
+import com.jme.scene.Node;
+import com.jme.system.DisplaySystem;
import com.jme.util.Timer;
import com.jme.util.lwjgl.LWJGLTimer;
import com.jmex.game.state.BasicGameState;
@@ -51,10 +53,26 @@
private PhysicsSpace physics;
private PhysicsThread thread;
+ /**
+ * This node should be the parent for all physics objects.
+ * It will be updated in the physics thread.
+ */
+ private Node physicsRoot;
+
+ /**
+ * This node should be the parent for all physics objects.
+ * It will be updated in the physics thread.
+ * @return the root physics node.
+ */
+ public Node getPhysicsRoot() {
+ return physicsRoot;
+ }
+
public PhysicsMultithreadedGameState(String name) {
super(name);
thread = new PhysicsThread(30);
physics = thread.getPhysics();
+ physicsRoot = thread.getPhysicsRoot();
new Thread(thread).start();
}
@@ -69,9 +87,16 @@
}
}
+ @Override
+ public void render(float tpf) {
+ DisplaySystem.getDisplaySystem().getRenderer().draw(physicsRoot);
+ super.render(tpf);
+ }
+
public void setActive(boolean active) {
super.setActive(active);
- if (!active) {
+ if (!active)
+ {
thread.setEnabled(false);
}
}
@@ -97,6 +122,7 @@
private Timer timer;
private boolean limitUpdates;
private Lock updateLock;
+ private Node physicsRoot = new Node();
public PhysicsThread(int desiredUpdatesPerSecond) {
physics = PhysicsSpace.create();
@@ -163,8 +189,9 @@
unlock();
// Now lock up again so nothing can happen while we're updating
lock();
-
+ physicsRoot.updateGeometricState(tpf, true);
physics.update(tpf);
+
}
public void lock() {
@@ -178,4 +205,8 @@
public void shutdown() {
keepAlive = false;
}
+ public Node getPhysicsRoot()
+ {
+ return physicsRoot;
+ }
}
Thanks for the patch I'll get it applied. Irrisor, if you find yourself in jME-Physics before I do (I currently don't have it installed and running on my new machine) feel free to apply this for me.