Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a number of issues with your code....but this answer is probably way too late to be of any help anyways. </p> <p>Firstly, world.createBody() seems to have been deprecated. Now you have to explicitly call world.createDynamicBody() in the latest version of the Android Box2D jar that I have.</p> <p>Anyways...my version goes something like this...not perfect but at least its dynamic.</p> <pre><code>package com.box2D; import java.util.ArrayList; 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.graphics.Canvas; import android.graphics.Paint; import android.util.Log; public class PhysicsWorld { public float timeStep = 1; public int iterations = 1; private ArrayList&lt;Body&gt; bodies; private int count = 0; private AABB worldAABB; private World world; private BodyDef groundBodyDef; private PolygonDef boxShapeDef; //a paint private Paint paint; public void create() { //create the paint paint = new Paint(); paint.setColor(0xFFFFFF); // Step 1: Create Physics World Boundaries worldAABB = new AABB(); worldAABB.lowerBound.set(new Vec2((float) -100.0, (float) -100.0)); worldAABB.upperBound.set(new Vec2((float) 100.0, (float) 300.0)); // 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); bodies = new ArrayList&lt;Body&gt;(); // Step 3: Create Ground Box groundBodyDef = new BodyDef(); groundBodyDef.position.set(new Vec2((float) 0.0, (float) 300.0)); Body groundBody = world.createDynamicBody(groundBodyDef); boxShapeDef = new PolygonDef(); boxShapeDef.setAsBox((float) 50.0, (float) 10.0); groundBody.createShape(boxShapeDef); } public void addBall() { // Create Dynamic Body BodyDef bodyDef = new BodyDef(); bodyDef.position.set((float)16.0+(count * 10), (float)24.0); Body newBody = world.createDynamicBody(bodyDef); // Create Shape with Properties CircleDef circle = new CircleDef(); circle.radius = (float)5; circle.density = (float)1.0; // Assign shape to Body newBody.createShape(circle); newBody.setMassFromShapes(); newBody.m_userData = "Circle"; bodies.add(newBody); // Increase Counter count += 1; } public void addBox() { // Create Dynamic Body BodyDef bodyDef = new BodyDef(); bodyDef.position.set((float)36.0+(count * 10), (float)24.0); Body newBody = world.createDynamicBody(bodyDef); // Create Shape with Properties boxShapeDef = new PolygonDef(); boxShapeDef.setAsBox((float) 50.0, (float) 10.0); // Assign shape to Body newBody.createShape(boxShapeDef); newBody.setMassFromShapes(); newBody.m_userData = "Box"; bodies.add(newBody); // Increase Counter count += 1; } /* * * * Physics Update */ private Vec2 position; private float angle; public void Update(Canvas canvas) { // Update Physics World world.step(timeStep/5, 1); // Print info of latest body for (Body i : bodies) { position = i.getPosition(); angle = i.getAngle(); if(i.m_userData == "Circle") canvas.drawCircle(position.x, position.y, 5,paint ); if(i.m_userData == "Box") canvas.drawRect((float)(position.x -5), (float)(position.y - 5), (float)(position.x + 5), (float)(position.y + 5), paint ); Log.v("Physics Test", "Pos: (" + position.x + ", " + position.y + "), Angle: " + angle); } } } </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