Hi,
I want to create a simple screensaver for my phone and JBox2D looks great for it! It would be just get some balls moving with the accelerometer.
However, after Testbed tests study, I tried to implement it with Android SDK. And then I'm getting some very strange behaviours, with circles keeping intersecting and bouncing around...
I tried a 200pixels/meter with 30px balls, without success...
I've created a "PhysicsWorld" class (end of the post), using real world units and managing a few things :
- creation of edges around the world
- creation of "ball"
- update method
It looks very similar to what I've done in a TestbedTest (end of the post), but it doesn't work this time on Android 🙁
Is there anybody who would be kind enough to help me and point me in the right direction please?
Thanks 😉
In the Android code, I recreate the world when the surface change :
Add ball if user touch screen :
Draw a ball :
Update/draw world using a thread managed with a handler :
I want to create a simple screensaver for my phone and JBox2D looks great for it! It would be just get some balls moving with the accelerometer.
However, after Testbed tests study, I tried to implement it with Android SDK. And then I'm getting some very strange behaviours, with circles keeping intersecting and bouncing around...
I tried a 200pixels/meter with 30px balls, without success...
I've created a "PhysicsWorld" class (end of the post), using real world units and managing a few things :
- creation of edges around the world
- creation of "ball"
- update method
It looks very similar to what I've done in a TestbedTest (end of the post), but it doesn't work this time on Android 🙁
Is there anybody who would be kind enough to help me and point me in the right direction please?
Thanks 😉
In the Android code, I recreate the world when the surface change :
Code:
mWorld.recreateWorld(width / SCALE_FACTOR, height / SCALE_FACTOR);
Add ball if user touch screen :
Code:
mWorld.addBall(worldPosition, BALL_RADIUS / SCALE_FACTOR);
Draw a ball :
Code:
c.drawCircle(screenPosition.x, screenPosition.y, BALL_RADIUS, mPaint);
Update/draw world using a thread managed with a handler :
Code:
private final Runnable mDrawCube = new Runnable() {
public void run() {
// Log.i("Wallpaper", "mDrawCube run");
mHandler.removeCallbacks(mDrawCube);
if (mVisible) {
mWorld.update();
mHandler.postDelayed(mDrawCube, PhysicsWorld.TIME_STEP);
}
drawFrame();
}
};
public void run() {
// Log.i("Wallpaper", "mDrawCube run");
mHandler.removeCallbacks(mDrawCube);
if (mVisible) {
mWorld.update();
mHandler.postDelayed(mDrawCube, PhysicsWorld.TIME_STEP);
}
drawFrame();
}
};
Code:
public class PhysicsWorld {
public static final int TARGET_FPS = 15;
public static final int TIME_STEP = (1000 / TARGET_FPS);
public static final int VEL_ITER = 3;
public static final int POS_ITER = 8;
private List<Body> bodies = new ArrayList<Body>();
private World world;
public PhysicsWorld() {
recreateWorld(16.0f, 12.0f);
}
public void recreateWorld(float xmax, float ymax) {
Log.i("World", "RecreateWorld " + xmax + " / " + ymax);
bodies.clear();
Vec2 gravity = new Vec2(0.0f, -10.0f);
world = new World(gravity, true);
BodyDef def = new BodyDef();
def.type = BodyType.STATIC;
Body groundBody = world.createBody(def);
createEdge(0.0f, 0.0f, xmax, 0.0f, groundBody);
createEdge(xmax, 0.0f, xmax, ymax, groundBody);
createEdge(xmax, ymax, 0.0f, ymax, groundBody);
createEdge(0.0f, ymax, 0.0f, 0.0f, groundBody);
}
private void createEdge(float x1, float y1, float x2, float y2,
Body groundBody) {
Vec2 v1 = new Vec2(x1, y1);
Vec2 v2 = new Vec2(x2, y2);
Log.i("World", "Add edge " + v1 + " / " + v2);
PolygonShape groundShapeDef = new PolygonShape();
groundShapeDef.setAsEdge(v1, v2);
groundBody.createFixture(groundShapeDef, 0.0f);
}
public void addBall(Vec2 position, float radius) {
Log.i("World", "Add ball " + position + " / " + radius);
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DYNAMIC;
bodyDef.position.set(position);
Body body = world.createBody(bodyDef);
CircleShape shape = new CircleShape();
shape.m_radius = radius;
body.createFixture(shape, 1.0f);
bodies.add(body);
}
public List<Body> getBalls() {
return bodies;
}
public void update() {
// Log.i("World", "World update");
world.step(TIME_STEP, VEL_ITER, POS_ITER);
}
}
public static final int TARGET_FPS = 15;
public static final int TIME_STEP = (1000 / TARGET_FPS);
public static final int VEL_ITER = 3;
public static final int POS_ITER = 8;
private List<Body> bodies = new ArrayList<Body>();
private World world;
public PhysicsWorld() {
recreateWorld(16.0f, 12.0f);
}
public void recreateWorld(float xmax, float ymax) {
Log.i("World", "RecreateWorld " + xmax + " / " + ymax);
bodies.clear();
Vec2 gravity = new Vec2(0.0f, -10.0f);
world = new World(gravity, true);
BodyDef def = new BodyDef();
def.type = BodyType.STATIC;
Body groundBody = world.createBody(def);
createEdge(0.0f, 0.0f, xmax, 0.0f, groundBody);
createEdge(xmax, 0.0f, xmax, ymax, groundBody);
createEdge(xmax, ymax, 0.0f, ymax, groundBody);
createEdge(0.0f, ymax, 0.0f, 0.0f, groundBody);
}
private void createEdge(float x1, float y1, float x2, float y2,
Body groundBody) {
Vec2 v1 = new Vec2(x1, y1);
Vec2 v2 = new Vec2(x2, y2);
Log.i("World", "Add edge " + v1 + " / " + v2);
PolygonShape groundShapeDef = new PolygonShape();
groundShapeDef.setAsEdge(v1, v2);
groundBody.createFixture(groundShapeDef, 0.0f);
}
public void addBall(Vec2 position, float radius) {
Log.i("World", "Add ball " + position + " / " + radius);
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DYNAMIC;
bodyDef.position.set(position);
Body body = world.createBody(bodyDef);
CircleShape shape = new CircleShape();
shape.m_radius = radius;
body.createFixture(shape, 1.0f);
bodies.add(body);
}
public List<Body> getBalls() {
return bodies;
}
public void update() {
// Log.i("World", "World update");
world.step(TIME_STEP, VEL_ITER, POS_ITER);
}
}
Code:
public class Spheres extends TestbedTest {
int x_count = 3;
int y_count = 3;
long startMillis = -1;
Body m_bodies[] = new Body[x_count * y_count];
/**
* @see org.jbox2d.testbed.framework.TestbedTest#initTest(boolean)
*/
@Override
public void initTest(boolean argDeserialized) {
{
BodyDef bd = new BodyDef();
Body ground = getWorld().createBody(bd);
getWorld().setGravity(new Vec2(0, 0));
PolygonShape shape = new PolygonShape();
shape.setAsEdge(new Vec2(-20.0f, -20.0f), new Vec2(-20.0f, 20.0f));
ground.createFixture(shape, 0.0f);
shape = new PolygonShape();
shape.setAsEdge(new Vec2(-20.0f, 20.0f), new Vec2(20.0f, 20.0f));
ground.createFixture(shape, 0.0f);
shape = new PolygonShape();
shape.setAsEdge(new Vec2(20.0f, 20.0f), new Vec2(20.0f, -20.0f));
ground.createFixture(shape, 0.0f);
shape = new PolygonShape();
shape.setAsEdge(new Vec2(20.0f, -20.0f), new Vec2(-20.0f, -20.0f));
ground.createFixture(shape, 0.0f);
}
{
CircleShape shape = new CircleShape();
shape.m_radius = 4.0f;
for (int i = 0; i < x_count; ++i) {
for (int j = 0; j < y_count; ++j) {
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
bd.position.set(-15.0f + i * (30.0f / x_count), -15.0f + j
* (30.0f / y_count));
m_bodies[i + j * x_count] = getWorld().createBody(bd);
m_bodies[i + j * x_count].createFixture(shape, 4.0f);
}
}
}
}
@Override
public void update() {
if (startMillis != -1) {
double angle = (System.currentTimeMillis() - startMillis) / (1000.0d);
float gx = (float) (-80.0f + 160.0f * Math.sin(angle));
float gy = (float) (-80.0f + 160.0f * Math.cos(angle));
getWorld().setGravity(new Vec2(gx, gy));
} else {
startMillis = System.currentTimeMillis();
}
super.update();
}
/**
* @see org.jbox2d.testbed.framework.TestbedTest#getTestName()
*/
@Override
public String getTestName() {
return "Balls";
}
}
int x_count = 3;
int y_count = 3;
long startMillis = -1;
Body m_bodies[] = new Body[x_count * y_count];
/**
* @see org.jbox2d.testbed.framework.TestbedTest#initTest(boolean)
*/
@Override
public void initTest(boolean argDeserialized) {
{
BodyDef bd = new BodyDef();
Body ground = getWorld().createBody(bd);
getWorld().setGravity(new Vec2(0, 0));
PolygonShape shape = new PolygonShape();
shape.setAsEdge(new Vec2(-20.0f, -20.0f), new Vec2(-20.0f, 20.0f));
ground.createFixture(shape, 0.0f);
shape = new PolygonShape();
shape.setAsEdge(new Vec2(-20.0f, 20.0f), new Vec2(20.0f, 20.0f));
ground.createFixture(shape, 0.0f);
shape = new PolygonShape();
shape.setAsEdge(new Vec2(20.0f, 20.0f), new Vec2(20.0f, -20.0f));
ground.createFixture(shape, 0.0f);
shape = new PolygonShape();
shape.setAsEdge(new Vec2(20.0f, -20.0f), new Vec2(-20.0f, -20.0f));
ground.createFixture(shape, 0.0f);
}
{
CircleShape shape = new CircleShape();
shape.m_radius = 4.0f;
for (int i = 0; i < x_count; ++i) {
for (int j = 0; j < y_count; ++j) {
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
bd.position.set(-15.0f + i * (30.0f / x_count), -15.0f + j
* (30.0f / y_count));
m_bodies[i + j * x_count] = getWorld().createBody(bd);
m_bodies[i + j * x_count].createFixture(shape, 4.0f);
}
}
}
}
@Override
public void update() {
if (startMillis != -1) {
double angle = (System.currentTimeMillis() - startMillis) / (1000.0d);
float gx = (float) (-80.0f + 160.0f * Math.sin(angle));
float gy = (float) (-80.0f + 160.0f * Math.cos(angle));
getWorld().setGravity(new Vec2(gx, gy));
} else {
startMillis = System.currentTimeMillis();
}
super.update();
}
/**
* @see org.jbox2d.testbed.framework.TestbedTest#getTestName()
*/
@Override
public String getTestName() {
return "Balls";
}
}