Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
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;
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render(float arg0) {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
GL10 gl = Gdx.app.getGraphics().getGL10();
camera.apply(gl);
Matrix4 cameraCopy = camera.combined.cpy();
debugRenderer.render(world, cameraCopy.scl(BOX_TO_WORLD));
world.step(1/60f, 6, 2);
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void show() {
camera = new OrthographicCamera();
camera.setToOrtho(false);
world = new World(new Vector2(0, -10), true);
debugRenderer = new Box2DDebugRenderer();
groundDef = new BodyDef();
groundDef.position.set(new Vector2((Gdx.graphics.getWidth() / 2) * WORLD_TO_BOX, 16f * WORLD_TO_BOX));
groundBody = world.createBody(groundDef);
PolygonShape groundShape = new PolygonShape();
groundShape.setAsBox((Gdx.graphics.getWidth() / 2) * WORLD_TO_BOX, 16f * WORLD_TO_BOX);
groundBody.createFixture(groundShape, 0f);
groundShape.dispose();
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);
playerShape.dispose();
}
}
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.