0
\$\begingroup\$

I don't have any idea about the box2d collision detection in android. I googled and got results that suggest to use the contact listener but I don't know how to use it in android java.

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

If you're using JBox2D, you can use

org.jbox2d.dynamics.World.setContactListener(new ContactListener() {
    ...
});
\$\endgroup\$
1
\$\begingroup\$

OMG! that was the best help I've found! Thankyou Skyuzo. This code works for JBox2D (I haven't tried Android however, but Android would be using JBox2D instead of the C++ Box2D)

Inside the ContactListener, this code must be placed handling the contacts: Note This code should only be initialized once.

world.setContactListener(new ContactListener() {

            @Override
        public void beginContact(Contact contact) {
            System.out.println("Contact detected");
        }

        @Override
        public void endContact(Contact contact) {
            System.out.println("Contact removed");
        }

        @Override
        public void postSolve(Contact arg0, ContactImpulse arg1) {
            // TODO Auto-generated method stub
        }

        @Override
        public void preSolve(Contact arg0, Manifold arg1) {
            // TODO Auto-generated method stub
        }
    });
\$\endgroup\$
2
  • \$\begingroup\$ Can't figure out if this is an answer or a thank you. \$\endgroup\$ Commented Feb 23, 2013 at 8:47
  • \$\begingroup\$ Looks like both. It's the same answer but it has the abstract methods implemented. \$\endgroup\$
    – House
    Commented Feb 23, 2013 at 17:22

You must log in to answer this question.

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