Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.math.Matrix4;
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.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
public class GameScreen implements Screen {
private static final float WORLD_TO_BOX = 0.01f;
private static final float BOX_TO_WORLD = 100f;
private OrthographicCamera camera;
private BodyDef groundDef;
private Body groundBody;
private BodyDef playerDef;
private Body playerBody;
private World world;
private Box2DDebugRenderer debugRenderer;
public void dispose() {
// TODO Auto-generated method stub
public void hide() {
// TODO Auto-generated method stub
public void pause() {
// TODO Auto-generated method stub
public void render(float arg0) {;
GL10 gl =;
Matrix4 cameraCopy = camera.combined.cpy();
debugRenderer.render(world, cameraCopy.scl(BOX_TO_WORLD));
world.step(1/60f, 6, 2);
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
public void resume() {
// TODO Auto-generated method stub
public void show() {
camera = new OrthographicCamera();
world = new World(new Vector2(0, -10), true);
debugRenderer = new Box2DDebugRenderer();
groundDef = new BodyDef();
groundDef.position.set(new Vector2(( / 2) * WORLD_TO_BOX, 16f * WORLD_TO_BOX));
groundBody = world.createBody(groundDef);
PolygonShape groundShape = new PolygonShape();
groundShape.setAsBox(( / 2) * WORLD_TO_BOX, 16f * WORLD_TO_BOX);
groundBody.createFixture(groundShape, 0f);
playerDef = new BodyDef();
playerDef.type = BodyType.DynamicBody;
playerDef.position.set(new Vector2(100 * WORLD_TO_BOX, 400 * WORLD_TO_BOX));
playerBody = world.createBody(playerDef);
PolygonShape playerShape = new PolygonShape();
playerShape.setAsBox(50 * WORLD_TO_BOX, 50 * WORLD_TO_BOX);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = playerShape;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.6f;
Fixture fixture = playerBody.createFixture(fixtureDef);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.