diff trunk/chipmunkd/cpSpaceComponent.d @ 29:80058cee1a77

updated to chipmunk 5.3.4
author Extrawurst
date Thu, 16 Dec 2010 00:33:06 +0100
parents 4ceef5833c8c
children 34b36b5193af
line wrap: on
line diff
--- a/trunk/chipmunkd/cpSpaceComponent.d	Mon Dec 13 21:40:56 2010 +0100
+++ b/trunk/chipmunkd/cpSpaceComponent.d	Thu Dec 16 00:33:06 2010 +0100
@@ -35,6 +35,21 @@
 	}
 }
 
+void
+cpSpaceActivateBody(cpSpace *space, cpBody *_body)
+{
+	if(space.locked){
+		// cpSpaceActivateBody() is called again once the space is unlocked
+		cpArrayPush(space.rousedBodies, _body);
+	} else {
+		cpArrayPush(space.bodies, _body);
+		for(cpShape *shape=_body.shapesList; shape; shape=shape.next){
+			cpSpaceHashRemove(space.staticShapes, shape, shape.hashid);
+			cpSpaceHashInsert(space.activeShapes, shape, shape.hashid, shape.bb);
+		}
+	}
+}
+
 static void
 componentActivate(cpBody *root)
 {
@@ -42,22 +57,16 @@
 	
 	cpSpace *space = root.space;
 	assert(space, "Trying to activate a body that was never added to a space.");
-	assert(!space.locked, "Bodies can not be awakened during a query or a call to cpSpaceSte(). Put these calls into a post-step callback.");
-	
-	cpBody *_body = root;
-	cpBody *next;
-	do {
-		next = _body.node.next;
-		
-		cpFloat idleTime = (cpBodyIsStatic(_body) ? cast(cpFloat)INFINITY : 0.0f);
-		cpComponentNode node = {null, null, 0, idleTime};
-		_body.node = node;
-		if(!cpBodyIsRogue(_body)) cpArrayPush(space.bodies, _body);
-		
-		for(cpShape *shape=_body.shapesList; shape; shape=shape.next){
-			cpSpaceHashRemove(space.staticShapes, shape, shape.hashid);
-			cpSpaceHashInsert(space.activeShapes, shape, shape.hashid, shape.bb);
-		}
+
+	cpBody *_body = root, next;
+	do {
+		next = _body.node.next;
+		
+		cpComponentNode node = {null, null, 0, 0.0f};
+		_body.node = node;
+		
+		cpSpaceActivateBody(space, _body);
+
 	} while((_body = next) != root);
 	
 	cpArrayDeleteObj(space.sleepingComponents, root);
@@ -66,16 +75,15 @@
 void
 cpBodyActivate(cpBody *_body)
 {
-	// Reset the idle time even if it's not in a currently sleeping component
-	// Like a body resting on or jointed to a rogue body.
-	if(!cpBodyIsStatic(_body)) _body.node.idleTime = 0.0f;
-	componentActivate(componentNodeRoot(_body));
+	//NOTE: dmd compiler bug when useing -inline
+	auto foo = componentNodeRoot(_body);
+	componentActivate(foo);
 }
 
 static void
 mergeBodies(cpSpace *space, cpArray *components, cpArray *rogueBodies, cpBody *a, cpBody *b)
 {
-	// Don't merge with the static body
+	// Ignore connections to static bodies
 	if(cpBodyIsStatic(a) || cpBodyIsStatic(b)) return;
 	
 	cpBody *a_root = componentNodeRoot(a);
@@ -91,9 +99,9 @@
 		componentActivate(b_root);
 	} 
 	
-	// Add any rogue bodies (bodies not added to the space)
-	if(cpBodyIsRogue(a)) cpArrayPush(rogueBodies, a);
-	if(cpBodyIsRogue(b)) cpArrayPush(rogueBodies, b);
+	// Add any rogue bodies found to the list and reset the idle time of anything they touch.
+	if(cpBodyIsRogue(a)){ cpArrayPush(rogueBodies, a); b.node.idleTime = 0.0f; }
+	if(cpBodyIsRogue(b)){ cpArrayPush(rogueBodies, b); a.node.idleTime = 0.0f; }
 	
 	componentNodeMerge(a_root, b_root);
 }
@@ -142,10 +150,11 @@
 	cpArray *rogueBodies = cpArrayNew(16);
 	cpArray *arbiters = space.arbiters;
 	cpArray *constraints = space.constraints;
-	cpArray *components = cpArrayNew(bodies.num/8);
+	cpArray *components = cpArrayNew(space.sleepingComponents.num);
 	
 	cpFloat dv = space.idleSpeedThreshold;
 	cpFloat dvsq = (dv ? dv*dv : cpvdot(space.gravity, space.gravity)*dt*dt);
+	
 	// update idling
 	for(int i=0; i<bodies.num; i++){
 		cpBody *_body = cast(cpBody*)bodies.arr[i];
@@ -165,10 +174,8 @@
 	}
 	
 	// iterate bodies and add them to their components
-	for(int i=0; i<bodies.num; i++)
-		addToComponent(cast(cpBody*)bodies.arr[i], components);
-	for(int i=0; i<rogueBodies.num; i++)
-		addToComponent(cast(cpBody*)rogueBodies.arr[i], components);
+	for(int i=0; i<bodies.num; i++) addToComponent(cast(cpBody*)bodies.arr[i], components);
+	for(int i=0; i<rogueBodies.num; i++) addToComponent(cast(cpBody*)rogueBodies.arr[i], components);
 	
 	// iterate components, copy or deactivate
 	for(int i=0; i<components.num; i++){
@@ -180,9 +187,8 @@
 				next = _body.node.next;
 				
 				if(!cpBodyIsRogue(_body)) cpArrayPush(newBodies, _body);
-				_body.node.next = null;
-				_body.node.parent = null;
-				_body.node.rank = 0;
+				cpComponentNode node = {null, null, 0, _body.node.idleTime};
+				_body.node = node;
 			} while((_body = next) != root);
 		} else {
 			cpBody *_body = root;
@@ -224,9 +230,8 @@
 	if(cpBodyIsSleeping(_body)) return;
 	
 	for(cpShape *shape = _body.shapesList; shape; shape = shape.next){
+		cpShapeCacheBB(shape);
 		cpSpaceHashRemove(space.activeShapes, shape, shape.hashid);
-		
-		cpShapeCacheBB(shape);
 		cpSpaceHashInsert(space.staticShapes, shape, shape.hashid, shape.bb);
 	}
 	
@@ -248,19 +253,11 @@
 
 static void
 activateTouchingHelper(cpShape *shape, cpContactPointSet *points, cpArray **bodies){
-	if(*bodies == null) (*bodies) = cpArrayNew(0);
-	cpArrayPush(*bodies, shape._body);
+	cpBodyActivate(shape._body);
 }
 
 void
 cpSpaceActivateShapesTouchingShape(cpSpace *space, cpShape *shape){
 	cpArray *bodies = null;
 	cpSpaceShapeQuery(space, shape, cast(cpSpaceShapeQueryFunc)&activateTouchingHelper, &bodies);
-	
-	if(bodies){
-		for(int i=0; i<bodies.num; i++){
-			cpBody *_body = cast(cpBody *)bodies.arr[i];
-			cpBodyActivate(_body);
-		}
-	}
 }