Skip to content

Instantly share code, notes, and snippets.

@kotcrab
Created January 10, 2014 19:29
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 kotcrab/8360895 to your computer and use it in GitHub Desktop.
Save kotcrab/8360895 to your computer and use it in GitHub Desktop.
Box2dTester 2
package pl.kotcrab.libgdxtests;
import pl.kotcrab.core.Touch;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
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 Box2dTester implements ApplicationListener
{
private OrthographicCamera camera;
static final float WORLD_TO_BOX = 0.01f;
static final float BOX_TO_WORLD = 100f;
private World world;
private Box2DDebugRenderer debugRenderer;
@Override
public void create()
{
camera = new OrthographicCamera(800, 480);
camera.position.x = 400;
camera.position.y = 240;
world = new World(new Vector2(0, -10), true);
debugRenderer = new Box2DDebugRenderer();
Touch.setCamera(camera);
// teren
BodyDef groundBodyDef = new BodyDef();
groundBodyDef.position.set(new Vector2(400 * WORLD_TO_BOX, 10 * WORLD_TO_BOX));
Body groundBody = world.createBody(groundBodyDef);
PolygonShape groundBox = new PolygonShape();
groundBox.setAsBox(800 * WORLD_TO_BOX / 2, 10 * WORLD_TO_BOX / 2);
groundBody.createFixture(groundBox, 0);
groundBox.dispose();
}
private void createCirlce(float x, float y)
{
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(new Vector2(x * WORLD_TO_BOX, y * WORLD_TO_BOX));
Body body = world.createBody(bodyDef);
CircleShape circle = new CircleShape();
circle.setRadius(20f * WORLD_TO_BOX);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circle;
fixtureDef.density = 0.5f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.6f;
body.createFixture(fixtureDef);
circle.dispose();
}
@Override
public void render()
{
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
if(Gdx.input.justTouched())
{
if(Gdx.input.isButtonPressed(Buttons.LEFT))
createCirlce(Touch.calcX(Gdx.input.getX()), Touch.calcY(Gdx.input.getY()));
}
world.step(1 / 60f, 6, 2);
debugRenderer.render(world, camera.combined.scl(BOX_TO_WORLD));
}
@Override
public void dispose()
{
world.dispose();
}
//@formatter:off
@Override public void pause(){}
@Override public void resume(){}
@Override public void resize(int width, int height){}
//@formatter:on
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment