My Bodies seem to be correctly created and affected by gravity, but they do not interact with anything. They are quite happy to just fall forever in whatever direction they are sent until they end up off screen. I can put other bodies in the way or crash them in to each other with MouseJoints, but unless I take direct control of one they just continue in their path indefinitely.
Most of my code is very similar to the HelloWorld from the manual, but as that is C++ there may be some unintentional differences there. This is running on Android.
Code:
private PhysicsThread thread;
SurfaceHolder surfaceHolder;
private World world;
private BodyDef groundBodyDef = new BodyDef();
private BodyDef bodyDef = new BodyDef();
private FixtureDef fixtureDef = new FixtureDef();
private PolygonShape groundBox = new PolygonShape();
private Body groundBody, body, circleA, circleB, circleC;
private CircleShape csA, csB, csC;
private BodyDef circleADef = new BodyDef(), circleBDef = new BodyDef(),
circleCDef = new BodyDef();
private MouseJointDef md;
private MouseJoint mj;
private int pitch;
private int roll;
public PhysicsView(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
thread = new PhysicsThread(getHolder(), this);
setFocusable(true);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Vec2 gravity = new Vec2((float) 0.0, (float) 10.0);
boolean doSleep = true;
world = new World(gravity, doSleep);
groundBodyDef = new BodyDef();
groundBodyDef.position.set(50.0f, 50.0f);
groundBody = world.createBody(groundBodyDef);
groundBox = new PolygonShape();
groundBox.setAsBox(50.0f, 50.0f);
groundBody.createFixture(groundBox, 0.0f);
// dynamic Body
bodyDef.type = BodyType.DYNAMIC;
bodyDef.position.set(0.0f, 4.0f);
body = world.createBody(bodyDef);
PolygonShape dynamicBox = new PolygonShape();
dynamicBox.setAsBox(1.0f, 1.0f);
// Circle A
circleADef.type = BodyType.DYNAMIC;
circleADef.position.set(100.0f, 100.0f);
circleA = world.createBody(circleADef);
csA = new CircleShape();
csA.m_p.set(2.0f, 3.0f);
csA.m_radius = 10.0f;
// Circle B
circleBDef.type = BodyType.DYNAMIC;
circleBDef.position.set(200.0f, 200.0f);
circleB = world.createBody(circleBDef);
csB = new CircleShape();
csB.m_p.set(2.0f, 3.0f);
csB.m_radius = 50.5f;
// Circle C – Static for testing
circleCDef.type = BodyType.STATIC;
circleCDef.position.set(200.0f, 400.0f);
circleC = world.createBody(circleCDef);
csC = new CircleShape();
csC.m_p.set(2.0f, 3.0f);
csC.m_radius = 50.5f;
fixtureDef.shape = dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body.createFixture(fixtureDef);
thread.setRunning(true);
thread.start();
}
The gravity and joints are set in other methods and seem to be functioning correctly. I'm sure it's a stupid problem but I've been unable to fix it in a couple of days work working solid. I've tried playing around with lots of different variables for groundBodyDef.position.set() and groundBody.setAsBox().
Ultimately I would like the objects should crash into the edges of the screen and each other.