import java.util.ArrayList;
import java.util.List;
import org.jbox2d.collision.AABB;
import org.jbox2d.collision.CircleDef;
import org.jbox2d.collision.PolygonDef;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.World;
import android.util.Log;
/**
*
*
*/
public class PhysicsWorld
{
/** */
public static int TARGET_FPS = 40;
/** */
public static int TIME_STEP = (1000 / TARGET_FPS);
/** */
private int theIterations = 5;
/** */
private List<Body> theBodies = new ArrayList<Body>();
/** */
private World theWorld;
/**
*
*/
public void create()
{
// Step 1: Create Physics World Boundaries
AABB worldAabb = new AABB();
worldAabb.lowerBound.set(new Vec2((float) -100.0, (float) -100.0));
worldAabb.upperBound.set(new Vec2((float) 100.0, (float) 100.0));
// Step 2: Create Physics World with Gravity
Vec2 gravity = new Vec2((float) 0.0, (float) -10.0);
theWorld = new World(worldAabb, gravity, true);
// Step 3: Create Ground Box
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vec2((float) 0.0, (float) -10.0));
Body groundBody = theWorld.createBody(groundBodyDef);
PolygonDef groundShapeDef = new PolygonDef();
groundShapeDef.setAsBox((float) 50.0, (float) 10.0);
groundBody.createShape(groundShapeDef);
}
/**
*
*/
public void addBall()
{
// Create Dynamic Body
BodyDef bodyDef = new BodyDef();
bodyDef.position.set((float) 6.0+theBodies.size(), (float) 24.0);
Body newBody = theWorld.createBody(bodyDef);
// Create Shape with Properties
CircleDef circle = new CircleDef();
circle.radius = (float) 1.8;
circle.density = (float) 1.0;
// Assign shape to Body
newBody.createShape(circle);
newBody.setMassFromShapes();
theBodies.add(newBody);
}
/**
*
*/
public void update()
{
// Update Physics World
theWorld.step(TIME_STEP, theIterations);
// Print info of latest body
if (!theBodies.isEmpty())
{
Vec2 position = theBodies.get(theBodies.size()-1).getPosition();
float angle = theBodies.get(theBodies.size()-1).getAngle();
Log.v("Physics Test", "Pos: (" + position.x + ", " + position.y + "), Angle: " + angle);
}
}
}