Game Development Reference
In-Depth Information
Next, add the following import lines to the WorldController class:
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.packtpub.libgdx.canyonbunny.game.objects.Carrot;
Then, add the following lines of code to the same class:
private boolean goalReached;
public World b2world;
private void initPhysics () {
if (b2world != null) b2world.dispose();
b2world = new World(new Vector2(0, -9.81f), true);
// Rocks
Vector2 origin = new Vector2();
for (Rock rock : level.rocks) {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.KinematicBody;
bodyDef.position.set(rock.position);
Body body = b2world.createBody(bodyDef);
rock.body = body;
PolygonShape polygonShape = new PolygonShape();
origin.x = rock.bounds.width / 2.0f;
origin.y = rock.bounds.height / 2.0f;
polygonShape.setAsBox(rock.bounds.width / 2.0f,
rock.bounds.height / 2.0f, origin, 0);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = polygonShape;
body.createFixture(fixtureDef);
polygonShape.dispose();
}
}
 
Search WWH ::




Custom Search