Created
February 10, 2015 02:09
-
-
Save ichiko/b9608edf39764197c763 to your computer and use it in GitHub Desktop.
libGDX with Box2D PolygonShape Definition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package cc.clv.revjune.pts.screen; | |
import cc.clv.revjune.pts.PTSGame; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.Screen; | |
import com.badlogic.gdx.graphics.GL20; | |
import com.badlogic.gdx.graphics.OrthographicCamera; | |
import com.badlogic.gdx.graphics.Texture; | |
import com.badlogic.gdx.graphics.g2d.Sprite; | |
import com.badlogic.gdx.math.MathUtils; | |
import com.badlogic.gdx.math.Vector2; | |
import com.badlogic.gdx.physics.box2d.*; | |
import com.badlogic.gdx.utils.Array; | |
/** | |
* Created by ichiko on 2015/02/07. | |
*/ | |
public class PlayGroundScreen implements Screen { | |
private final PTSGame game; | |
private OrthographicCamera cam; | |
private World world; | |
private Box2DDebugRenderer debugRenderer; | |
private Array<Body> bodiesList; | |
public PlayGroundScreen(final PTSGame game) { | |
this.game = game; | |
float w = Gdx.graphics.getWidth(); | |
float h = Gdx.graphics.getHeight(); | |
cam = new OrthographicCamera(); | |
cam.setToOrtho(false, w, h); | |
Box2D.init(); | |
// no gravity | |
world = new World(new Vector2(0, 0), true); | |
debugRenderer = new Box2DDebugRenderer(); | |
bodiesList = new Array<Body>(); | |
addWorldObjects(); | |
world.getBodies(bodiesList); | |
} | |
private void addWorldObjects() { | |
Texture img = new Texture("square.png"); | |
Sprite sprite = new Sprite(img); | |
float w = sprite.getWidth(); // 100px | |
float h = sprite.getHeight(); // 100px | |
// ここから剛体の定義 | |
BodyDef bodyDef = new BodyDef(); | |
bodyDef.type = BodyDef.BodyType.DynamicBody; | |
bodyDef.position.set(300, 200); | |
Body body = world.createBody(bodyDef); | |
body.setUserData(sprite); | |
PolygonShape shape = new PolygonShape(); | |
// shape.setAsBox(w, h); | |
shape.setAsBox(w/2, h/2); | |
FixtureDef fixtureDef = new FixtureDef(); | |
fixtureDef.shape = shape; | |
fixtureDef.density = 1.0f; | |
fixtureDef.friction = 1.0f; | |
fixtureDef.restitution = 0.6f; // make it bounce little bit | |
body.createFixture(fixtureDef); | |
shape.dispose(); | |
} | |
@Override | |
public void show() { | |
} | |
@Override | |
public void render(float delta) { | |
Gdx.gl.glClearColor(0, 0, 1, 1); | |
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
game.batch.begin(); | |
for (Body body : bodiesList) { | |
if (body.getUserData() != null) { | |
Vector2 pos = body.getPosition(); | |
if (body.getUserData() instanceof Sprite) { | |
Sprite sprite = (Sprite) body.getUserData(); | |
sprite.setRotation(MathUtils.radiansToDegrees * body.getAngle()); | |
sprite.setPosition(pos.x - sprite.getWidth() / 2f, | |
pos.y - sprite.getHeight() / 2f); | |
sprite.draw(game.batch); | |
} | |
} | |
} | |
game.batch.end(); | |
debugRenderer.render(world, cam.combined); | |
world.step(1/60f, 6, 2); | |
} | |
@Override | |
public void resize(int width, int height) { | |
} | |
@Override | |
public void pause() { | |
} | |
@Override | |
public void resume() { | |
} | |
@Override | |
public void hide() { | |
} | |
@Override | |
public void dispose() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment