How do I combine a sprite with a Box2D body?

How do I set my sprites x/y coordinates to my body’s x/y position?
Also I scaled everything down by one hundred since Box2D measures in meters, so am I
also supposed to scale my image down by 100 or something?? This is weird. Code below.


  public class myGame implements ApplicationListener{
	OrthographicCamera camera;
	
	World myWorld;
	
	//Ground
	BodyDef floorDef;
	Body floor;
	FixtureDef floorFix;
	PolygonShape floorShape;
	//
	
	//Sprite
	Texture texture;
	Sprite sprite;
	
	BodyDef entityDef;
	Body entity;
	FixtureDef entityFix;
	PolygonShape entityShape;
	//
	
	SpriteBatch batch;
	Box2DDebugRenderer renderer;
	
	@Override
	public void create() {
		camera = new OrthographicCamera();
		camera.setToOrtho(false, Gdx.graphics.getWidth() / 100, Gdx.graphics.getHeight() /100);
		camera.update();
		
		renderer = new Box2DDebugRenderer();
		
		myWorld = new World(new Vector2(0, -10), true);
		
		batch = new SpriteBatch();
		
		//Ground 
		floorDef = new BodyDef();
		floorDef.type = BodyType.StaticBody;
		floor = myWorld.createBody(floorDef);
		floorShape = new PolygonShape();
		floorShape.setAsBox(camera.viewportWidth, 20f/100f);
		floorFix = new FixtureDef();
		floorFix.shape = floorShape;
		floor.createFixture(floorFix);
		//
		
		//Sprite
		texture = new Texture(Gdx.files.internal("data/myEntity.png"));
		sprite = new Sprite(texture);
		
		entityDef = new BodyDef();
		entityDef.type = BodyType.DynamicBody;
		entityDef.position.set(new Vector2(440 /100, 400 /100));
		entity = myWorld.createBody(entityDef);
		entityShape = new PolygonShape();
		entityShape.setAsBox(16f /100f, 16f / 100f);
		entityFix = new FixtureDef();
		entityFix.shape = entityShape;
		entity.createFixture(entityFix);
		
	}

	@Override
	public void resize(int width, int height) {
	
		
	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		
		renderer.render(myWorld, camera.combined);
		myWorld.step(1/60f, 6, 2);
	}

	@Override
	public void pause() {
	
		
	}

	@Override
	public void resume() {
		
		
	}

	@Override
	public void dispose() {
		
		
	}
	
	
	
	
	
}




Draw the sprite at the body.position.x or body.position.y

Also, do the same with the bodies rotation. Quite self-explanatory.

So I added that inside the render method but my image appears in the bottom left corner of the screen. Also he’s really tiny. The image jumps up/down but only a miniscule fraction of what my body does.


// sprite is my texture, and entity is my body...
   sprite.setPosition(entity.getPosition().x, entity.getPosition().y);
		batch.begin();
		sprite.draw(batch);
		batch.end();

Box2D uses meters not pixels. So, you would want to define what a meter is according to pixels in your game. And then convert the x andy from Box2D into pixels with that conversion if Im not mistaken.

Awesome! I multiplied the position by 100 and got it to work. But my image is still small and my image is out of sync with the body. For instance, the body jumps higher than my image (I added some restitution). How do I scale my image to the same size as the body shape?

Edit: It’s small because I made it 16 x 16px and since I’m not used to working with pixel art I thought it would look bigger. But I till can’t figure out how to scale it to the same size as the shape.

Was this problem solved? If not I believe your problem there is that you also have to do body.size or something along those line and convert that from meters to pixels as well. ;D Hope that works!