(LibGDx)Drawing a sprite on to a Box2D body

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.

Here is my real world method:


public void alignSpriteWithBody() {
        float facing = MathUtils.radiansToDegrees * (body.getAngle());
        float w = sprite.getWidth();
        float h = sprite.getHeight();
        Vector3 bodyPos = body.getPosition();
        float x = bodyPos.x - w / 2;
        float y = bodyPos.y - h / 2;
	sprite.setOrigin(w / 2, h / 2);
        sprite.setPosition(x, y);
        sprite.setRotation(facing);
    }

looking at it now it could be improved upon, but it does the job well enough

The Image still doesn’t appear. I mainly need to know what I’m doing wrong when i try to render the sprite on the body. I tried messing around with your code and in the gameloop, I set the x, y, width, and height each frame but the sprite just sits there.

I created an int for the x and y and gave both the bdef and the sprite that position. The sprite moves to the location but the body’s gravity moves and the sprite stays static. How do I constantly render the sprite in the body’s location?

I cannot find the lines where you actually draw some Sprites.

Like your alignSpriteWithBody seems fine, however where do you actually call it ?

drawing the sprite is done the most basic way possible so I didn’t include. Nothing to see there.