You're so close!
Creating redundant joints and filling up your arrays with redundant information will definitely slow you down. Here's how I'd do it:
Add() would push shape1 and shape2 onto two arrays to keep track of new collisions. These arrays would be cleared after being processed every step, so they never really grow. It's fine if we get multiple Adds between the same shape pairs during one step because we'll be removing redundancies shortly.
My update code (after Step()) would loop through these arrays and find new shape pairs to add joints between. This is where things get tricky. You need to maintain a list of shape pairs you already have joints between.
What you really want here is a hash map keyed off two pointers, sorted so that order doesn't matter, but Dictionary only supports one pointer as a key, and you can't get unique ids for objects (without mx?) so string concat isn't going to work (grumble grumble). The next simplest solution is what Boris recommended - a Dictionary for each Shape! In the below code, connectedShapes is a Dictionary for these Dictionaries for each shape. Instead of pre-filling it for each shape, I've elected to only create Dictionaries for shapes as we start seeing them.
I'm getting told it's bed time, so I didn't have time to check this code. Hope it helps!
Creating redundant joints and filling up your arrays with redundant information will definitely slow you down. Here's how I'd do it:
Add() would push shape1 and shape2 onto two arrays to keep track of new collisions. These arrays would be cleared after being processed every step, so they never really grow. It's fine if we get multiple Adds between the same shape pairs during one step because we'll be removing redundancies shortly.
My update code (after Step()) would loop through these arrays and find new shape pairs to add joints between. This is where things get tricky. You need to maintain a list of shape pairs you already have joints between.
What you really want here is a hash map keyed off two pointers, sorted so that order doesn't matter, but Dictionary only supports one pointer as a key, and you can't get unique ids for objects (without mx?) so string concat isn't going to work (grumble grumble). The next simplest solution is what Boris recommended - a Dictionary for each Shape! In the below code, connectedShapes is a Dictionary for these Dictionaries for each shape. Instead of pre-filling it for each shape, I've elected to only create Dictionaries for shapes as we start seeing them.
Code:
for ( var i:int = 0; i < contactListener.newShapeList1.length; i++ ) {
var shape1:b2Shape = contactListener.newShapeList1[i];
var shape2:b2Shape = contactListener.newShapeList2[i];
// first, make sure our shapes each have a dictionary already
if ( !connectedShapes[ shape1 ] ) {
connectedShapes[ shape1 ] = new Dictionary();
}
if ( !connectedShapes[ shape2 ] ) {
connectedShapes[ shape2 ] = new Dictionary();
}
// next, skip this pair if they've already been connected
if ( connectedShapes[ shape1 ][ shape2 ] ) { continue; }
if ( connectedShapes[ shape2 ][ shape1 ] ) { continue; }
// connect them (from both sides)
connectedShapes[ shape1 ][ shape2 ] = true;
connectedShapes[ shape2 ][ shape1 ] = true;
// finally, make the actual joint between the shapes!
createJoint( shape1.GetBody(), shape2.GetBody() ); // this is your function from above
}
// and clear our hoppers
contactListener.newShapeList1 = new Array();
contactListener.newShapeList2 = new Array();
var shape1:b2Shape = contactListener.newShapeList1[i];
var shape2:b2Shape = contactListener.newShapeList2[i];
// first, make sure our shapes each have a dictionary already
if ( !connectedShapes[ shape1 ] ) {
connectedShapes[ shape1 ] = new Dictionary();
}
if ( !connectedShapes[ shape2 ] ) {
connectedShapes[ shape2 ] = new Dictionary();
}
// next, skip this pair if they've already been connected
if ( connectedShapes[ shape1 ][ shape2 ] ) { continue; }
if ( connectedShapes[ shape2 ][ shape1 ] ) { continue; }
// connect them (from both sides)
connectedShapes[ shape1 ][ shape2 ] = true;
connectedShapes[ shape2 ][ shape1 ] = true;
// finally, make the actual joint between the shapes!
createJoint( shape1.GetBody(), shape2.GetBody() ); // this is your function from above
}
// and clear our hoppers
contactListener.newShapeList1 = new Array();
contactListener.newShapeList2 = new Array();
I'm getting told it's bed time, so I didn't have time to check this code. Hope it helps!