1
votes

How can we know if a box2d car is flying? This is how I do with sensors, which doesn't work perfect:

public void endContact(Contact contact) {
            // TODO Auto-generated method stub
            final Fixture x1 = contact.getFixtureA();
            final Fixture x2 = contact.getFixtureB();

            if (x1.getBody().getUserData() != null
                    && x2.getBody().getUserData() != null) {
                if ((x1.getBody().getUserData().equals("ground") && x2
                        .getBody().getUserData().equals("wheelSensor1"))
                        || (x1.getBody().getUserData().equals("wheelSensor1") && x2
                                .getBody().getUserData().equals("ground"))) {
                    r1 = true;
                }
                if ((x1.getBody().getUserData().equals("ground") && x2
                        .getBody().getUserData().equals("wheelSensor2"))
                        || (x1.getBody().getUserData().equals("wheelSensor2") && x2
                                .getBody().getUserData().equals("ground"))) {
                    r2 = true;
                }
                if (r1 && r2){
                    car.isFlying = true;
                }
            }
        }

        @Override
        public void beginContact(Contact contact) {
            // TODO Auto-generated method stub
            final Fixture x1 = contact.getFixtureA();
            final Fixture x2 = contact.getFixtureB();

            if (x1.getBody().getUserData() != null
                    && x2.getBody().getUserData() != null) {
                if (x1.getBody().getUserData().equals("ground")
                        && x2.getBody().getUserData().equals("wheelSensor1")) {
                    car.isFlying = false;
                    r1 = false;
                } else if (x1.getBody().getUserData().equals("wheelSensor1")
                        && x2.getBody().getUserData().equals("ground")) {
                    car.isFlying=false;
                    r1 = false;
                } else if (x1.getBody().getUserData().equals("ground")
                        && x2.getBody().getUserData().equals("wheelSensor2")) {
                    car.isFlying=false;
                    r2 = false;
                } else if (x1.getBody().getUserData().equals("wheelSensor2")
                        && x2.getBody().getUserData().equals("ground")) {
                    car.isFlying = false;
                    r2 = false;
                }
            }
        }

My sensor bodies are bigger than wheels to get better result.wheelSensorPicture But contact listener gives me endContact between sensors and ground many times even if sensors are buried in ground like in the picture. I've tried same process with just wheel bodies, the result was same. So what is my mistake here, or what is the better way? Thanks for any help.

1
What is the ground made of... is it multiple fixtures or just one? - iforce2d

1 Answers

0
votes

Try this:

bool isWheelTouching(b2Body* wheel)
{
    bool wheelOnEarth = false;
    for (b2ContactEdge* edge = wheel->GetContactList(); edge != null; edge = edge->next)
    {
        if (edge->contact->IsTouching())
        {
            wheelOnEarth = true;
            break;
        }
    }
    return wheelOnEarth;
}

Call it every time step for each wheel to determine, if car on the earth or not.