Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>public class PhysicsWorld extends View{ protected static final int GUIUPDATEIDENTIFIER = 0x231; public int targetFPS = 40; public float timeStep = 10.0f / targetFPS; public int iterations = 5; private Body[] bodies; private int count = 0; private AABB worldAABB; public World world; private PolygonDef groundShapeDef; public int World_W,World_H; private Paint paint; private float radius=10; public PhysicsWorld(Context context,int W,int H) { super(context); World_W=W; World_H=H; // Step 1: Create Physics World Boundaries worldAABB = new AABB(); Vec2 min = new Vec2(-50, -50); Vec2 max = new Vec2(World_W + 50, World_H + 50); worldAABB.lowerBound.set(min); worldAABB.upperBound.set(max); // Step 2: Create Physics World with Gravity Vec2 gravity = new Vec2((float) 0.0, (float) -10.0); boolean doSleep = true; world = new World(worldAABB, gravity, doSleep); // Step 3: //Create Ground Box : BodyDef bodyDef = new BodyDef(); bodyDef.position.set(new Vec2((float) 0.0, (float) -10.0)); Body groundBody = world.createBody(bodyDef); groundShapeDef = new PolygonDef(); groundShapeDef.setAsBox((float) World_W, (float) 10); groundBody.createShape(groundShapeDef); // up : bodyDef = new BodyDef(); bodyDef.position.set(new Vec2((float) 0.0, (float) (World_H+10.0) )); groundBody = world.createBody(bodyDef); groundShapeDef = new PolygonDef(); groundShapeDef.setAsBox((float) World_W, (float) 10); groundBody.createShape(groundShapeDef); // left : bodyDef = new BodyDef(); bodyDef.position.set(new Vec2((float) -10, (float) 0.0 )); groundBody = world.createBody(bodyDef); groundShapeDef = new PolygonDef(); groundShapeDef.setAsBox((float)10, (float) World_H); groundBody.createShape(groundShapeDef); // right : bodyDef = new BodyDef(); bodyDef.position.set(new Vec2((float) World_W+10, (float) 0.0 )); groundBody = world.createBody(bodyDef); groundShapeDef = new PolygonDef(); groundShapeDef.setAsBox((float)10, (float) World_H); groundBody.createShape(groundShapeDef); // // step 4: initialize bodies=new Body[50]; // paint=new Paint(); paint.setStyle(Style.FILL_AND_STROKE); paint.setColor(Color.CYAN); } public void addBall() { // Create Dynamic Body BodyDef bodyDef = new BodyDef(); Random rnd = new Random(); bodyDef.position.set((float) radius*2+rnd.nextInt( (int)(World_W-radius*4) ), (float)2*radius+ rnd.nextInt( (int)(World_H-radius*4) )); bodies[count] = world.createBody(bodyDef); // Create Shape with Properties CircleDef circle = new CircleDef(); circle.radius = (float) radius; circle.density = (float) 1.0; circle.friction = 0.1f; circle.restitution=0.5f; // Assign shape to Body bodies[count].createShape(circle); bodies[count].setMassFromShapes(); // Increase Counter count += 1; } public void addBox() { BodyDef bodyDef = new BodyDef(); Random rnd = new Random(); bodyDef.position.set((float)2*count+rnd.nextInt( (int)(World_W-4*count) ), (float)2*count+rnd.nextInt( (int)(World_H-count*4) )); bodies[count] = world.createBody(bodyDef); // Create Shape with Properties PolygonDef boxShapeDef = new PolygonDef(); boxShapeDef.setAsBox((float) 10.0, (float) 10.0); boxShapeDef.density = 1.0f; // A density of 0 will create a fixed Body that doesn't move boxShapeDef.friction = 0.1f; // How much friction boxShapeDef.restitution = 0.5f; // How bouncy is this Shape? // Assign shape to Body bodies[count].createShape(boxShapeDef); bodies[count].setMassFromShapes(); bodies[count].m_userData = "Box"; count += 1; } public void update() { world.step(timeStep/5, 1); postInvalidate(); } protected void onDraw(Canvas canvas) { for(int j = 0;j&lt;count;j++) { canvas.drawRect((float)(bodies[j].getPosition().x -10), (float)(bodies[j].getPosition().y - 10), (float)(bodies[j].getPosition().x+ 10), (float)(bodies[j].getPosition().y + 10), paint ); canvas.drawCircle(bodies[j].getPosition().x,World_H- bodies[j].getPosition().y, radius, paint); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload