My game hangs/freezes when I create dynamic bullet objects with Box2D and I don't know why. I am making a game where the main character can shoot bullets by the user tapping on the screen. Each touch event spawns a new FireProjectileEvent that is handled properly by an event queue. So I know my problem is not trying to create a new body while the box2d world is locked. My bullets are then created and managed by an object pool class like this:

public Projectile getProjectile(){
    for(int i=0;i<mProjectiles.size();i++){
            return mProjectiles.get(i);

    return mSpriteFactory.createProjectile();


mSpriteFactory.createProjectile() leads to the physics component of the Projectile class creating its box2d body. I have narrowed the issue down to this method and it looks like this:

public void create(World world, float x, float y, Vec2 vertices[], boolean dynamic){

    BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyType.DYNAMIC;
        bodyDef.type = BodyType.STATIC;
    bodyDef.position.set(x, y);
    mBody = world.createBody(bodyDef);

    PolygonShape dynamicBox = new PolygonShape();
    dynamicBox.set(vertices, vertices.length);
    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = dynamicBox;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.0f;


If the dynamic parameter is set to true my game freezes before crashing, but if it is false, it will create a projectile exactly how I want it just doesn't function properly (because a projectile is not a static object). Why does my program fail when I try to create a dynamic object at runtime but not when I create a static one? I have other dynamic objects (like my main character) that work fine. Any help would be greatly appreciated.

This is a screenshot of a method profile I did: enter image description here Especially notable is number 8. I'm just still unsure what I'm doing wrong.

Other notes:
I am using JBox2D (Upgraded from to try to fix this problem)
When the application freezes, if I hit the back button, it appears to move my game backwards by one update tick. Very strange.

  • \$\begingroup\$ Any error messages you're getting might help. \$\endgroup\$ – Nic Foster May 3 '12 at 22:19
  • \$\begingroup\$ There are no errors. The application just "hangs" until android offers to force close it. The stack trace is written to a file on my phone that I can't access without root access. I did, however, narrow down the problem to my create() method. \$\endgroup\$ – Amplify91 May 3 '12 at 22:31
  • \$\begingroup\$ I've not yet worked with Android, but the first thing I'd try is attaching to the process and getting a breakpoint so you can get a callstack at the time it is frozen, or just pause the process and it should tell you which line it's currently executing. \$\endgroup\$ – Nic Foster May 3 '12 at 23:53
  • \$\begingroup\$ It seems to be hanging inside of box2d. Various methods inside of world.step() especially while solving time of impact. \$\endgroup\$ – Amplify91 May 4 '12 at 2:45
  • \$\begingroup\$ Do you have any real-time profiling tools? If so they might tell you which methods are taking up most of the time, which should help you pinpoint the offending function(s) at least. I'm guessing that world.step() is too broad to be helpful. \$\endgroup\$ – Nic Foster May 4 '12 at 2:55

It turned out that the vertices of my bullets were not making proper shapes due to a simple mistake.

They were something like: (0.0, 0.0),(0.0,0.5),(0.5,0.5),(0.0,0.5) which is obviously wrong but very hard to spot. Thank you for the help everyone, and I did end up using LibGDX; it's awesome.


Try to create new bodies so that they don't collide right away with others. Eg. its big problem if you create many bullets to same location at once.

Another note: Libgdx have java to native wrapper for box2d. Performance is so much better that maybe you could swap there.

  • \$\begingroup\$ I am only spawning one bullet at a time and it is not overlapping anything. Also, I am aware of Libgdx and have chosen not to use it. Thank you for your suggestions. \$\endgroup\$ – Amplify91 May 6 '12 at 15:41

I would suggest building a minimal test applet using the basics of the function above, nothing else. You may find the issue doesn't lie in this method directly, but rather in what it is tying into (the b2World or something else in your current setup).

I would also try the same under standard Java on the desktop rather than Android's Dalvik JVM, in the rather unlikely case that there is some discrepancy between them, since JBox2D was developed under/for the former.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.