I’m still vpretty new to Box2D and I’m following dermetfan’s video using my own code. According to his bitbucket, I’m supposed to render the image on the body like so.
batch.begin();
// renders all the Box2DSprites in the given Box2D World
Box2DSprite.draw(batch, world);
// renders just this Box2DSprite, but you have to give in the Body or Fixture to render on (this way, it doesn't have to be in the user data)
box2DSprite.draw(batch, body);
box2DSprite.draw(batch, fixture);
batch.end();
But using my code , I’m not sure how I would do that. Here is my source if your interested.
Main Gdx Class
public class MyGdxGame extends Game {
@SuppressWarnings("unused")
private TestScreenA GameScreen;
@Override
public void create () {
AssetLoaderTest.loadTile("testImgs/Colors tile.png");
GameScreen = new TestScreenA(this);
}
@Override
public void render () {
GameScreen.render(0);
}
}
Test Screen (only screen) Class
public class TestScreenA implements Screen{
@SuppressWarnings("unused")
private MyGdxGame game;
Random r;
//Camera
private OrthographicCamera camera;
//images etc.
private SpriteBatch batch;
Vector3 touch;
//Box2D
private World world;
private Box2DDebugRenderer b2dr;
private Body bd;
public TestScreenA(MyGdxGame game) {
this.game = game;
//Camera
camera = new OrthographicCamera();
//true starts the y axis at the bottom left and false starts it at the top left. It is said that top left is more useul.
camera.setToOrtho(false, 1000, 1000/16*9);
batch = new SpriteBatch();
//Box 2d
//CREATE WORLD
world = new World(new Vector2(0, 100f), true);
b2dr = new Box2DDebugRenderer();
BodyDef bdef2 = new BodyDef();
bdef2.position.set(165 , 100 );
bdef2.type = BodyType.DynamicBody;
bd = world.createBody(bdef2);
PolygonShape shape2 = new PolygonShape();
shape2.setAsBox(5, 5 );
FixtureDef fdef2 = new FixtureDef();
fdef2.shape = shape2;
fdef2.restitution = 1f;
bd.createFixture(fdef2);
// create platform
BodyDef bdef = new BodyDef();
bdef.position.set(160 , 120 );
bdef.type = BodyType.StaticBody;
Body body = world.createBody(bdef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(50, 5 );
FixtureDef fdef = new FixtureDef();
fdef.shape = shape;
body.createFixture(fdef);
//********************************************************************************//
body.setUserData(AssetLoaderTest.blueTile);
//platform 2
bdef.position.set(160 , 280 );
bdef.type = BodyType.StaticBody;
body = world.createBody(bdef);
shape.setAsBox(5 , 5);
fdef.shape = shape;
body.createFixture(fdef);
//platform 3
bdef.position.set(200 , 380 );
bdef.type = BodyType.StaticBody;
body = world.createBody(bdef);
shape.setAsBox(100 , 5);
fdef.shape = shape;
body.createFixture(fdef);
//platform 4
bdef.position.set(100 , 500);
bdef.type = BodyType.StaticBody;
body = world.createBody(bdef);
shape.setAsBox(80 , 5);
fdef.shape = shape;
body.createFixture(fdef);
//platform 4
bdef.position.set(0 , 0);
bdef.type = BodyType.StaticBody;
body = world.createBody(bdef);
shape.setAsBox(2 , 500);
fdef.shape = shape;
body.createFixture(fdef);
// create falling box
bdef.position.set(160 , 200 );
bdef.type = BodyType.DynamicBody;
body = world.createBody(bdef);
shape.setAsBox(5 , 5);
fdef.shape = shape;
fdef.restitution = 3;
fdef.density = 40;
body.createFixture(fdef);
//falling box 2
bdef.position.set(180 , 200 );
bdef.type = BodyType.DynamicBody;
body = world.createBody(bdef);
shape.setAsBox(5 , 5);
fdef.shape = shape;
fdef.restitution = 5;
fdef.density = 90;
body.createFixture(fdef);
}
public void render(float delta) {
Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
update(delta);
batch.begin();
//batch.draw(AssetLoaderTest.s, 0, 0);
//batch.draw(AssetLoaderTest.blueTile, 117, 10);
batch.end();
world.step(1.f/60.f, 6, 3);
batch.begin();
//Box2D
//@Param renders world and uses
b2dr.render(world, camera.combined);
batch.end();
}
public void update(float delta) {
}
public void resize(int width, int height) {
}
public void show() {
}
public void hide() {
}
public void pause() {
}
public void resume() {
}
public void dispose() {
}
}
Asset Loader Test Class
public class AssetLoaderTest {
//Tile Sheet
static Texture sSheet;
static Sprite blueTile;
//Animation
static Texture sheet;
static TextureRegion[] sheetFrames;
static TextureRegion currentFrame;
static Animation sheetAnim;
static void loadTile(String assetPath) {
sSheet = new Texture(assetPath);
blueTile = new Sprite(sSheet, 0, 0, 16, 16);
}
}
I hope this isn’t too much code for you to help.