Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7489027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:11:25+00:00 2026-05-29T15:11:25+00:00

A strange bug I can’t track down. I’ve implemented a ContactListener class for handling

  • 0

A strange bug I can’t track down. I’ve implemented a ContactListener class for handling collisions in my Android game. In the beginContact(Contact arg0) method I can see that the two bodies in arg0 is there, and pushed onto a stack. Right after calling world.step() I run my own handleCollisions() method, where I pop off the Contact objects and do some game logic. However, occasionally when I pop off a Contact, one or both of its bodies are null.

The Contact goes in the stack with its bodies there, but it comes out with null bodies. I don’t know why this is occurring, and more importantly, I can’t find when this is occurring. To my knowledge, none of my code elsewhere is removing the bodies, but then again there could be side effects I’m unaware of. It doesn’t help that this doesn’t always happen. Typically it occurs when there’s several collisions occurring.

Anyone have any ideas on what might remove the bodies? Or, does anyone know of a way to track the bodies to determine when they become null?

Below is some code which may or may not be helpful:

public class ConcreteContactListener implements ContactListener
{
    private Stack<Contact> contacts;

    public ConcreteContactListener()
    {
        contacts = new Stack<Contact>();
    }

    @Override
    public void beginContact(Contact arg0)
    {
        contacts.push(arg0);
        System.out.println("push: " + arg0.m_fixtureA.m_body);
    }


public int handleCollisions(ArrayList<Ball> balls, World world, ArrayList<Ball> smears, SoundEffects sfx, Combos combos)
{
    int score = 0;

    while (!contacts.isEmpty())
    {
        Contact contact = contacts.pop();
        System.out.println("Contact: " + contact.m_fixtureA.m_body);
        int a = -1;
        int b = -1;

        for (int i = 0; i < balls.size(); i++)
        {
            System.out.println("Ball: " + balls.get(i).getBody());
            if (contact.m_fixtureA.m_body.equals(balls.get(i).getBody()))
                 a = i;
            else if (contact.m_fixtureB.m_body.equals(balls.get(i).getBody()))
                b = i;
        }

        ...

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:11:26+00:00Added an answer on May 29, 2026 at 3:11 pm

    Contacts are pooled and re-used, so I would not recommend using this approach. Instead I would buffer only the information you need (which is probably the two bodies). The jbox2d testbed handles it this way:

    First we have a contact point:

    public class ContactPoint {
        public Fixture fixtureA;
        public Fixture fixtureB;
        public final Vec2 normal = new Vec2();
        public final Vec2 position = new Vec2();
        public PointState state;
    }
    

    And then we listen like so:

    public void beginContact(Contact contact) {
    }
    
    public void endContact(Contact contact) {
    }
    
    public void postSolve(Contact contact, ContactImpulse impulse) {
    }
    
    private final PointState[] state1 = new PointState[Settings.maxManifoldPoints];
    private final PointState[] state2 = new PointState[Settings.maxManifoldPoints];
    private final WorldManifold worldManifold = new WorldManifold();
    
    public void preSolve(Contact contact, Manifold oldManifold) {
        Manifold manifold = contact.getManifold();
    
        if (manifold.pointCount == 0) {
            return;
        }
    
        Fixture fixtureA = contact.getFixtureA();
        Fixture fixtureB = contact.getFixtureB();
    
        Collision.getPointStates(state1, state2, oldManifold, manifold);
    
        contact.getWorldManifold(worldManifold);
    
        for (int i = 0; i < manifold.pointCount
                && pointCount < MAX_CONTACT_POINTS; i++) {
            ContactPoint cp = points[pointCount];
            cp.fixtureA = fixtureA;
            cp.fixtureB = fixtureB;
            cp.position.set(worldManifold.points[i]);
            cp.normal.set(worldManifold.normal);
            cp.state = state2[i];
            ++pointCount;
        }
    }
    

    This would probably be a bit overkill for your purposes, as it executes this logic for every single contact. Instead you can use the beginContact() and endContact() methods and buffer something a little more optimized with your game, like just storing the colliding bodies or something.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to track down what appears to be a very strange bug.
I've run into a really strange bug, that I'm hoping someone can explain. I
I have a rather strange bug which i can't make sense of that is
I'm writing a simple nD-vector class, but am encountering a strange bug. I've stripped
I have a strange bug in a multithreaded app: public class MyClass { private
Here's some strange behavior I hope someone can confirm is a known bug, or
I have a very strange bug in a shipping iPad/iPhone app that I can't
I am a beginner with Android, and I have a strange bug with the
I think I've just found a really strange bug... But it can just be
Yesterday I tracked down a strange bug which caused a website display only a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.