I am trying to hold the contacts made during a collision in an array list. After I add n amount of contacts, when I try to retrieve them later on, they point to the last contact made in the world.

How can I save the contact that was there initially?

I am using Box2d and Libgdx with Java.

public void beginContact(Contact contact) {
    Fixture fa = contact.getFixtureA();
    Fixture fb = contact.getFixtureB();

    if (isFixtureCheckpoint(fa) || isFixtureCheckpoint(fb)) {

and when I am trying to retrieve in the update method, any contact that i get back is from the last contact made in the world:

public void update(float delta) {
    Fixture fa;
    Fixture fb;

    if (checkpointContact.size > 0) {
        fa = checkpointContact.first().getFixtureA();
        fb = checkpointContact.first().getFixtureB();

        if (isAnyFixtureRearWheel(fa, fb)) {
            // <--- This returns true. Why?

        if (isFixtureCheckpoint(fa)) {
        if (isFixtureCheckpoint(fb)) {

The comparison functions are comparing to different unrelated classes:

/* Check if ANY fixture is a rear wheel */
private boolean isAnyFixtureRearWheel(Fixture fa, Fixture fb) {
    if (((fa.getUserData() instanceof VehicleRearWheel) || (fb.getUserData() instanceof VehicleRearWheel))) {
            return true;
    return false;

/* Check if fixture is contacting the flag */
private boolean isFixtureCheckpoint(Fixture f) {
    return f.getUserData() instanceof ProgressHandler;

EDIT: Last time I checked Java was considered a pass by value language?

  • \$\begingroup\$ It is impossible to create a new Contact because Box2d has the constructor set to protected. And I was adding them as is to an array list. \$\endgroup\$ Sep 13, 2015 at 2:29
  • \$\begingroup\$ This is what I ended up doing in the end :) Give that as an answer and I will accept it. Thank you. \$\endgroup\$ Sep 13, 2015 at 3:28

1 Answer 1


It seems like Box2D is not creating a new instance of Contact everytime, but instead it is changing the most recent contact object's variables. So when you reference the contact object, you get the "updated" one (same object, but with updated variables).

You could simply save whatever information you need from the contact object to an object of your own custom class and add that to your list instead.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .