QUESTION: In an application, where is the appropriate place to put the code to interact with the Body objects?

By 'interact' I mean

  1. Get position
  2. Determine actions
  3. Apply impulse

Would it be appropriate to use the step() method to process the interactions?

I'm guessing responding to collisions would be done in the listeners.

Disclosure: I'm very new to JBox2D, and haven't used Box2D at all.
I'm using the Testbed JAR in my application so that I can see the Bodies interact.
The Body objects are controlled by simple AI, not user input.
I skimmed through the box2d v2.3.0 user manual to get a basic understanding of the objects in the world.
I didn't find any hints on where to put the decision logic.


1 Answer 1


You're on the right track.

The step() method is the right place to put game decision logic.

JBox2D's tests directory contains great reference examples. Among them is ApplyForce.java, which is similar to your description. It applies forces to a body based on decision logic—inside step(). Their decisions are based on keyboard inputs, but you could easily replace them with AI.

Because JBox2D follows the API of the original C++ Box2D closely, the original Box2D's manual is helpful.

There's already a question on contact listeners in JBox2D with a good answer that explains how they work. There's also a CollisionProcessing.java-test, but note that the technicalities of the collision-listening happen in its superclass, TestbedTest.java.

  • \$\begingroup\$ Awesome answer. Great clarity and references. I'll up-vote when I can. \$\endgroup\$
    – null
    Commented Jul 7, 2014 at 11:26
  • \$\begingroup\$ @nsl Happy if I helped. :) You get +2 rep for edits on other people's stuff once reviewers approve your edit, by the way. \$\endgroup\$
    – Anko
    Commented Jul 7, 2014 at 11:58
  • \$\begingroup\$ you helped by removing some doubt I was on the correct path. Want to try your hand at ?gamedev.stackexchange.com/questions/79801 \$\endgroup\$
    – null
    Commented Jul 7, 2014 at 12:26

You must log in to answer this question.

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