Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Box2d java contact point reference changes after collision

$
0
0

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)) {
        checkpointContact.add(contact);
    }
}

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)) {
            context.progressHandler.checkpointPassed(fa.getBody().getWorldCenter());
        }
        if (isFixtureCheckpoint(fb)) {
            context.progressHandler.checkpointPassed(fb.getBody().getWorldCenter());
        }
    }
}

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?


Viewing all articles
Browse latest Browse all 434

Trending Articles