performance improvements to PhysicsSpace

experimental
Phr00t 11 years ago
parent 3b187e9342
commit d200e406d5
  1. 21
      jme3-bullet/src/main/java/com/jme3/bullet/PhysicsSpace.java
  2. 28
      jme3-jbullet/src/main/java/com/jme3/bullet/PhysicsSpace.java

@ -46,11 +46,14 @@ import com.jme3.math.Transform;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
import com.jme3.scene.Node; import com.jme3.scene.Node;
import com.jme3.scene.Spatial; import com.jme3.scene.Spatial;
import com.jme3.util.SafeArrayList;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Stack;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
@ -91,8 +94,8 @@ public class PhysicsSpace {
private Map<Long, PhysicsRigidBody> physicsBodies = new ConcurrentHashMap<Long, PhysicsRigidBody>(); private Map<Long, PhysicsRigidBody> physicsBodies = new ConcurrentHashMap<Long, PhysicsRigidBody>();
private Map<Long, PhysicsJoint> physicsJoints = new ConcurrentHashMap<Long, PhysicsJoint>(); private Map<Long, PhysicsJoint> physicsJoints = new ConcurrentHashMap<Long, PhysicsJoint>();
private Map<Long, PhysicsVehicle> physicsVehicles = new ConcurrentHashMap<Long, PhysicsVehicle>(); private Map<Long, PhysicsVehicle> physicsVehicles = new ConcurrentHashMap<Long, PhysicsVehicle>();
private List<PhysicsCollisionListener> collisionListeners = new LinkedList<PhysicsCollisionListener>(); private ArrayList<PhysicsCollisionListener> collisionListeners = new ArrayList<PhysicsCollisionListener>();
private List<PhysicsCollisionEvent> collisionEvents = new LinkedList<PhysicsCollisionEvent>(); private Stack<PhysicsCollisionEvent> collisionEvents = new Stack<PhysicsCollisionEvent>();
private Map<Integer, PhysicsCollisionGroupListener> collisionGroupListeners = new ConcurrentHashMap<Integer, PhysicsCollisionGroupListener>(); private Map<Integer, PhysicsCollisionGroupListener> collisionGroupListeners = new ConcurrentHashMap<Integer, PhysicsCollisionGroupListener>();
private ConcurrentLinkedQueue<PhysicsTickListener> tickListeners = new ConcurrentLinkedQueue<PhysicsTickListener>(); private ConcurrentLinkedQueue<PhysicsTickListener> tickListeners = new ConcurrentLinkedQueue<PhysicsTickListener>();
private PhysicsCollisionEventFactory eventFactory = new PhysicsCollisionEventFactory(); private PhysicsCollisionEventFactory eventFactory = new PhysicsCollisionEventFactory();
@ -331,7 +334,7 @@ public class PhysicsSpace {
// } // }
private void addCollisionEvent_native(PhysicsCollisionObject node, PhysicsCollisionObject node1, long manifoldPointObjectId) { private void addCollisionEvent_native(PhysicsCollisionObject node, PhysicsCollisionObject node1, long manifoldPointObjectId) {
// System.out.println("addCollisionEvent:"+node.getObjectId()+" "+ node1.getObjectId()); // System.out.println("addCollisionEvent:"+node.getObjectId()+" "+ node1.getObjectId());
collisionEvents.add(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, manifoldPointObjectId)); collisionEvents.push(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, manifoldPointObjectId));
} }
/** /**
@ -361,17 +364,15 @@ public class PhysicsSpace {
public void distributeEvents() { public void distributeEvents() {
//add collision callbacks //add collision callbacks
// synchronized (collisionEvents) { int clistsize = collisionListeners.size();
for (Iterator<PhysicsCollisionEvent> it = collisionEvents.iterator(); it.hasNext();) { while( collisionEvents.empty() == false ) {
PhysicsCollisionEvent physicsCollisionEvent = it.next(); PhysicsCollisionEvent physicsCollisionEvent = collisionEvents.pop();
for (PhysicsCollisionListener listener : collisionListeners) { for(int i=0;i<clistsize;i++) {
listener.collision(physicsCollisionEvent); collisionListeners.get(i).collision(physicsCollisionEvent);
} }
//recycle events //recycle events
eventFactory.recycle(physicsCollisionEvent); eventFactory.recycle(physicsCollisionEvent);
it.remove();
} }
// }
} }
public static <V> Future<V> enqueueOnThisThread(Callable<V> callable) { public static <V> Future<V> enqueueOnThisThread(Callable<V> callable) {

@ -66,11 +66,13 @@ import com.jme3.math.Transform;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
import com.jme3.scene.Node; import com.jme3.scene.Node;
import com.jme3.scene.Spatial; import com.jme3.scene.Spatial;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Stack;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentLinkedQueue;
@ -111,8 +113,8 @@ public class PhysicsSpace {
private Map<RaycastVehicle, PhysicsVehicle> physicsVehicles = new ConcurrentHashMap<RaycastVehicle, PhysicsVehicle>(); private Map<RaycastVehicle, PhysicsVehicle> physicsVehicles = new ConcurrentHashMap<RaycastVehicle, PhysicsVehicle>();
private Map<Integer, PhysicsCollisionGroupListener> collisionGroupListeners = new ConcurrentHashMap<Integer, PhysicsCollisionGroupListener>(); private Map<Integer, PhysicsCollisionGroupListener> collisionGroupListeners = new ConcurrentHashMap<Integer, PhysicsCollisionGroupListener>();
private ConcurrentLinkedQueue<PhysicsTickListener> tickListeners = new ConcurrentLinkedQueue<PhysicsTickListener>(); private ConcurrentLinkedQueue<PhysicsTickListener> tickListeners = new ConcurrentLinkedQueue<PhysicsTickListener>();
private List<PhysicsCollisionListener> collisionListeners = new LinkedList<PhysicsCollisionListener>(); private ArrayList<PhysicsCollisionListener> collisionListeners = new ArrayList<PhysicsCollisionListener>();
private List<PhysicsCollisionEvent> collisionEvents = new LinkedList<PhysicsCollisionEvent>(); private Stack<PhysicsCollisionEvent> collisionEvents = new Stack<PhysicsCollisionEvent>();
private PhysicsCollisionEventFactory eventFactory = new PhysicsCollisionEventFactory(); private PhysicsCollisionEventFactory eventFactory = new PhysicsCollisionEventFactory();
private Vector3f worldMin = new Vector3f(-10000f, -10000f, -10000f); private Vector3f worldMin = new Vector3f(-10000f, -10000f, -10000f);
private Vector3f worldMax = new Vector3f(10000f, 10000f, 10000f); private Vector3f worldMax = new Vector3f(10000f, 10000f, 10000f);
@ -294,7 +296,7 @@ public class PhysicsSpace {
CollisionObject rBody1 = (CollisionObject) body1; CollisionObject rBody1 = (CollisionObject) body1;
node = (PhysicsCollisionObject) rBody0.getUserPointer(); node = (PhysicsCollisionObject) rBody0.getUserPointer();
node1 = (PhysicsCollisionObject) rBody1.getUserPointer(); node1 = (PhysicsCollisionObject) rBody1.getUserPointer();
collisionEvents.add(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, cp)); collisionEvents.push(eventFactory.getEvent(PhysicsCollisionEvent.TYPE_PROCESSED, node, node1, cp));
} }
return true; return true;
} }
@ -332,19 +334,17 @@ public class PhysicsSpace {
public void distributeEvents() { public void distributeEvents() {
//add collision callbacks //add collision callbacks
// synchronized (collisionEvents) { int clistsize = collisionListeners.size();
for (Iterator<PhysicsCollisionEvent> it = collisionEvents.iterator(); it.hasNext();) { while( collisionEvents.empty() == false ) {
PhysicsCollisionEvent physicsCollisionEvent = it.next(); PhysicsCollisionEvent physicsCollisionEvent = collisionEvents.pop();
for (PhysicsCollisionListener listener : collisionListeners) { for(int i=0;i<clistsize;i++) {
listener.collision(physicsCollisionEvent); collisionListeners.get(i).collision(physicsCollisionEvent);
}
//recycle events
eventFactory.recycle(physicsCollisionEvent);
it.remove();
} }
// } //recycle events
eventFactory.recycle(physicsCollisionEvent);
}
} }
public static <V> Future<V> enqueueOnThisThread(Callable<V> callable) { public static <V> Future<V> enqueueOnThisThread(Callable<V> callable) {
AppTask<V> task = new AppTask<V>(callable); AppTask<V> task = new AppTask<V>(callable);
System.out.println("created apptask"); System.out.println("created apptask");

Loading…
Cancel
Save