Performance issues when adding a new object to the world

Hello to all

I'm new in the Box2D world, and that's exactly my problem 🙂

I have created my first game, but I have some performance issues that I couldn't fix on my own ... so I have to ask for some help.

The game link is this: http://gamesloop.com/2009/11/18/catch/

So ... as you can see .... the game hangs a little, for a few mSeconds (milliSeconds) when a new object is added to the world. A new ball is added randomly with this function:

Code:
public function createObject(rX:Number,rY:Number,mX:Number,mY:Number,type:String,randomNumber:Number) {

         // Add some objects
         bodyDef = new b2BodyDef();
         bodyDef.position.x=mX/30;
         bodyDef.position.y=mY/30;
         // CIRCLE
         if (type=="CIRCLE") {
            circleDef = new b2CircleDef();
            circleDef.radius=rX;
            circleDef.density=1.0;
            circleDef.friction=0.5;
            circleDef.restitution=0.5;
            bodyDef.userData = new PhysCircle();
            bodyDef.isBullet=true;

            // here is set the type of the ball with the randomType function
            var t:Number=randomType();
            bodyDef.userData.gotoAndStop(t);

            bodyDef.userData.name="Circle";
            bodyDef.userData.myNumber=nrEggs;
            bodyDef.userData.myValue=ballValue();
            
                                addChild(bodyDef.userData);
            body=m_world.CreateBody(bodyDef);
            body.CreateShape(circleDef);
         }
         body.SetMassFromShapes();
         addChild(bodyDef.userData);
         Update(null); // This is so the ball will not apper for a short time on the upper-left corner of the stage
      }


There are other functions involved in this block of code, but those bits of code are not important, and the function name will tell you what the function's role is.

I have tryed both versions of calling the createObject function. Based on Timer event and also with ENTER_FRAME event, but both of these present the same issue, and exactly the same !!!

My code is trimmed down to only creating the balls, I mean no images no other functions no nothing. I've been fighting with this for 3-4 days. Is this a Box2D issue or what ?

P.S. 1 beer to whoever helps me with this issue :p
P.S2 NOT JOKING 🙂
Are you sure its from when you add objects?
Do you run this before or after your timestep?

I notice you are spawning a new object immediately after destroying one. Are you caching this object to destroy after the timestep?
How much are you doing during collision callbacks?

In short, have you profiled it to find out where the hickup is?
VengantMjolnir wrote:
Are you sure its from when you add objects?

90% sure it's when I spawn a new object. You can actualy see this happening if you pay attention and I removed the rest of the code. It happens only when a new ball it's added to the stage
VengantMjolnir wrote:
Do you run this before or after your timestep?

It's created before the timestep but it doesn't matter if it's creaated before or after. Or am I wrong ?
VengantMjolnir wrote:
I notice you are spawning a new object immediately after destroying one.

Where ?
VengantMjolnir wrote:
Are you caching this object to destroy after the

Yes !!! The collisions are buffered and the balls involved in this are destroyed after the time step
VengantMjolnir wrote:
How much are you doing during collision callbacks?

There are only 2 if branches, and even with those commented it still happens.
VengantMjolnir wrote:
In short, have you profiled it to find out where the hickup is?

I'm not using Flex (i know flex has profiling), or atleas I don't know how to profile a flash app. I'd like to know how to do this without flex if possible.

Thanks for your quick response
ok, sounds like you aren't doing anything obvious to cause it.

I know this works though, and it isn't a box2d thing. I've created games where objects are spawned into the world quite often and it works fine.

As for profiling, there are many ways to set up a profiler yourself but one of the most robust I've seen is in the PBE(http://pushbuttonengine.com/.) Essentially what they all have in common is wrapping sections of your code in timers.

You could create a simple one with a small class. Call it Profiler and create a Begin and End static method. In begin use getTimer() to get the tick count, then on End do the same thing and minus off the begin amount. If you accumulate this, you can divide it by the call count to get an average. From there its just a matter of wrapping your code to find out what takes the longest, then narrowing it down from there.

It sounds like you have narrowed down a small test case though. If you don't want to post it for all to see, you can email me. I'll PM you the email.
You are creating objects at the top left, and I guess moving them later after creation. This is bad for performance, though I'm not sure to the extent you are seeing. In any case, please fix the problem to determine that it is not the source. You should set b2BodyDef.position. At least, you should ensure you move bodies before you add shapes to them.

This makes a difference to performance, as shapes are inserted into the broadphase when first created, and the broadphase basically chokes when lots of objects overlap in one place, EVEN IF they are moved elsewhere before b2World.Step is called.

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