Skip to content

Instantly share code, notes, and snippets.

@smoak
Created April 26, 2016 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smoak/f3999d99ed36c9973e2e3b4c34631a57 to your computer and use it in GitHub Desktop.
Save smoak/f3999d99ed36c9973e2e3b4c34631a57 to your computer and use it in GitHub Desktop.
Trying to understand box2d
package com.mygdx.game.screen;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class BaseScreen implements Screen, InputProcessor {
protected Viewport viewport;
protected Stage stage;
@Override
public boolean keyDown(int keycode) {
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
@Override
public void show() {
viewport = new FitViewport(1280, 720);
stage = new Stage(viewport);
Gdx.input.setInputProcessor(new InputMultiplexer(this, stage));
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}
package com.mygdx.game.screen;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import com.mygdx.game.MyGdxGame;
public class GameScreen extends BaseScreen {
Image ballImage;
World world;
Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
Body ballBody;
@Override
public void show() {
super.show();
Box2D.init();
ballImage = new BallImage(MyGdxGame.assets.get("PNG/Balls/Blue/ballBlue_02.png", Texture.class));
stage.addActor(ballImage);
world = new World(new Vector2(0, -10), true);
createBallBody();
createGround();
ballImage.setPosition(ballBody.getPosition().x, ballBody.getPosition().y);
}
private void createBallBody() {
// First we create a body definition
BodyDef bodyDef = new BodyDef();
// We set our body to dynamic, for something like ground which doesn't move we would set it to StaticBody
bodyDef.type = BodyDef.BodyType.DynamicBody;
// Set our body's starting position in the world
bodyDef.position.set(100, 500);
// Create our body in the world using our body definition
ballBody= world.createBody(bodyDef);
// Create a circle shape and set its radius to 6
CircleShape circle = new CircleShape();
circle.setRadius(/*0.25f */ ballImage.getWidth()/ 2);
// Create a fixture definition to apply our shape to
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circle;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.6f; // Make it bounce a little bit
// Create our fixture and attach it to the body
ballBody.createFixture(fixtureDef);
// Remember to dispose of any shapes after you're done with them!
// BodyDef and FixtureDef don't need disposing, but shapes do.
circle.dispose();
}
private void createGround() {
// Create our body definition
BodyDef groundBodyDef =new BodyDef();
// Set its world position
groundBodyDef.position.set(new Vector2(0, 0.1f));
// Create a body from the defintion and add it to the world
Body groundBody = world.createBody(groundBodyDef);
// Create a polygon shape
PolygonShape groundBox = new PolygonShape();
// Set the polygon shape as a box which is twice the size of our view port and 20 high
// (setAsBox takes half-width and half-height as arguments)
groundBox.setAsBox(stage.getCamera().viewportWidth, 0.1f);
// Create a fixture from our polygon shape and add it to our ground body
groundBody.createFixture(groundBox, 0.0f);
// Clean up after ourselves
groundBox.dispose();
}
@Override
public void render(float delta) {
super.render(delta);
debugRenderer.render(world, stage.getCamera().combined);
world.step(1/45f, 6, 2);
ballImage.setPosition(ballBody.getPosition().x, ballBody.getPosition().y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment