calling b2World functions from ContactListener object

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.

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();

I'm getting told it's bed time, so I didn't have time to check this code. Hope it helps!
Thanks for your input Chriswa. It's sparked off a few ideas but I've found myself in a marginally worse position... and I'm not entirely sure why.

I've stored the colliding objects in two body objects, s1 and s2 within the bcontactlistener class. This was done because if my arrays are getting over written then I don't really see why I should be using arrays in the first place. Hopefully it'll reduce cost of resources. Upon collision I intend to compare them with my m_world.m_jointsList in my main class, through a for loop. The concept is simple: If the joints already exist- then don't create a joint. otherwise if the joint doesn't exist: create a joint. I've set up a couple nested conditional statements to help syphon out the unwanted joints.

here is my bcontactlistener definition:
Code:
public class bContactListener extends b2ContactListener
{
public var s1:b2Body;
public var s2:b2Body;
      
override public function Add(point:b2ContactPoint):void
{
if ( (point.shape1.GetBody().GetUserData().name == "bound") || (point.shape2.GetBody().GetUserData().name=="bound") ) {
            //if bound is a collision shape, don't do anything as it will result in sticky walls and floor
} else {
            
   s1=point.shape1.GetBody();
   s2=point.shape2.GetBody();
}}

here is my main.as code whcih takes place in my onEnterFrame event:

Code:
if(firstTime&&myListener.s1)
{
//create a joint for the first collision

djd = new b2DistanceJointDef();
djd.Initialize(myListener.s1, myListener.s2,
         new b2Vec2(myListener.s2.GetPosition().x,myListener.s1.GetPosition().y),
         new b2Vec2(myListener.s1.GetPosition().x,myListener.s2.GetPosition().y));

djd.collideConnected = true;
jo=m_world.CreateJoint(djd);

firstTime=false;
}
//Now compare s1 all joints in the m_world
   for (var jj:b2Joint=m_world.m_jointList;jj;jj=jj.m_next){
//checking to see whether the joints already exist or not.                     
      if(jj.GetBody1()==myListener.s1){
         if(jj.GetBody2()==myListener.s2) continue; //joint seems to already exist     
               }
      else if(jj.GetBody2()==myListener.s1){
         if(jj.GetBody1()==myListener.s2) continue;     //don't do anything because the 'inverse' of this joint already exists.
      }else {                       //past testing... joints don't exist so make a new joint                               
         djd.Initialize(myListener.s1, myListener.s2,
            new b2Vec2(myListener.s1.GetPosition().x,myListener.s1.GetPosition().y),
            new b2Vec2(myListener.s2.GetPosition().x,myListener.s2.GetPosition().y));
         djd.collideConnected = false;
         jo=m_world.CreateJoint(djd); //joint made!
      }
      trace("number of joints is ",m_world.m_jointCount);
}

I've been looking at this for about 2 hours now... and I can't see anythign wrong with my conditionals. However looking at my final trace statement- there seems to be alot wrong. my m_world.m_jointCount returns correctly fro the first 3 object collisions- thereafter it shoots immediately up to about 50 and then 80 before crashing really badly 🙁

Logically it should take more time to crash when compared to my previous versions of code, but the opposite is true. I'll let you knwo if there are any updates to the situation
It's that for loop - it's spitting out a new joint for every existing joint that doesn't match the one you're checking against, hence the exponential blowup. What you need to do is a loop through that list to see if you find a match, then if not, you create the joint after the loop is done.
ewjordan has it right on the money.

Also: it's possible to get two or more Add() calls with different shape pairs per step; that's why I suggested using arrays.
Just had a look again and yup... I think I'll be on my way to success. chriswa I'm sure you're technique is a lot more applicable than the route that I am taking, but I don't think I'm confident enough to pursue it. I will post the relevant code for this section soon as soon as I have it under wraps.
There are still problems with this code but I feel it is well on its way to success. I'll let you know what the problem is at the end, as I'm not entirely sure that it is a bug. I've rearranged my joint creation so that joints are only created AFTER the for loop has been traversed. The conditional statement inside the loop checks to see whether a joint exists.

This is an obvious point but cannot be overlooked: the joints list should only contain Unique joints, therefore if a collisionPoint's s1 and s2 objects already exist as a joint object, only ONE joint object within the jointsList needs to register as 'true' in the conditional statement (see below). At this point we can break the loop as we know that the joint already exists. Otherwise, if we have looped through the entire array and no joints with the same shapes as the collision objects shapes have been detected, make a new joint.

Code:
step();
if (jointOK == false) {
   for (var jj:b2Joint=m_world.m_jointList; jj; jj=jj.m_next) {
      //this conditional statement checks to see whether the collison object's s1 and s2 objects already exists            
      if ( ((jj.GetBody1() == myListener.s1) && (jj.GetBody2() == myListener.s2)) ||
         ((jj.GetBody1() == myListener.s2) && (jj.GetBody2() == myListener.s1)) ||
          ((jj.GetBody2() == myListener.s1) && (jj.GetBody1() == myListener.s2)) ||   
          ((jj.GetBody2() == myListener.s2) && (jj.GetBody1() == myListener.s1)) ){
break;     //if this returns true we can forget about the rest of the array (see previous comment)   

      }else{
//set thumbs up for joint making
         jointOK=true;
      

      }
   }
}
if (jointOK) {
   trace("number of joints is ", m_world.m_jointCount); //THIS PART MIGHT INDICATE BUGS
   makeJoint(s1,s2);//
   jointOK=false;

}      
   


ok. So what's the problem? the final trace statement returns a higher level of joints than expected. For example when a cluster of 8 objects are stuck together it returns 24 joints, although this number is variable. I have a feeling that's because, in my case, the cluster of objects might be bouncing off each other, hence creating more joints than is immediately obvious. If I'm correct then I suppose that's another problem to discuss. I'm personally not too fussed about this and if this is the case I think this'll be my final post on this topic. However if anyone thinks otherwise, please let me know and I'll do some more poking around.
24 < 28, which is the total number of potential pairings between 8 objects. Sounds like it's working perfectly! Congratulations. 😀
Nope... it's broken again! >:( .... When using revolute joints wit the same algorithm, the joint count continues to increase when the stacked bodies are piled on top of one another and their base moves... Not sure why this code works for distance joints but not revolute joints. Once again, I'll post code when/if I find a solution
if anyone's having problems with the creation of multiple joints being created where they shouldn't be, I suggest setting the jointdefinitions collideConnected attribute to false. That seems to have fixed the above problem

Original thread: https://box2d.org/forum/viewtopic.php?t=1738