Rendering Sprite on Newly Created Body

Hello,

I have been successful in rendering a spriteBatch to a simple square body that is created onCreate using the following code under render():

batch.setProjectionMatrix(camera.combined);
batch.begin();
world.getBodies(tmpBodies);
for (Body body : tmpBodies){
if(body.getUserData() != null && body.getUserData() instanceof Sprite) {
Sprite sprite = (Sprite) body.getUserData();
sprite.setPosition(body.getPosition().x - sprite.getWidth() / 2, body.getPosition().y - sprite.getHeight() / 2);
sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
sprite.draw(batch);
}
}
batch.end();

However, I have it so square bodies are created each time the screen is touched and don’t know how to attach a sprite to these newly created bodies.

Any ideas?

I’m not a LibGDX user, but since this is Box2D, this is where you create a Body and add it to the world I guess. I see that you are using user data, isn’t there a setUserData method? After creating the body also create the sprite, and attach it via the setUserData method.

So I am doing body.setUserData(Sprite)

but I guess it doesn’t draw it on the screen until it’s rendered? I’m not completely sure why it needs to be done but if I just do body.setUserData(Sprite) but don’t put the use the previous code in render() then it won’t draw my sprite onto my body.

So it seems like the render() is important but I don’t know how to run that code again with my newly created bodies so the sprite will draw to the body.

I feel like it should be as simple as body.setUserData(Sprite) but for some reason it’s not.

I’m sure it should be as simple as that. Can I see some of your code?

@Override
public void create () {
world = new World(new Vector2(0, -9.81f), true);
renderer = new Box2DDebugRenderer();
camera = new OrthographicCamera();
batch = new SpriteBatch();
Gdx.input.setInputProcessor(this);

	BodyDef bodyDef = new BodyDef();
	bodyDef.type = BodyDef.BodyType.DynamicBody;
	bodyDef.active = true;

	//square
	PolygonShape square = new PolygonShape();
	square.setAsBox(50,50);

	FixtureDef squareFixture = new FixtureDef();
	squareFixture.density = 0.5f / 25;
	squareFixture.friction = 0.5f;
	squareFixture.shape = square;
	squareFixture.restitution = 0.5f;

	box = world.createBody(bodyDef);

	Sprite boxSprite = new Sprite(new Texture(Gdx.files.internal("player.png")));
	boxSprite.setSize(100,100);
	boxSprite.setOrigin(boxSprite.getWidth() /2 , boxSprite.getHeight() / 2);
	box.setUserData(boxSprite);
	box.createFixture(squareFixture);

	square.dispose();

@Override
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

	batch.setProjectionMatrix(camera.combined);
	batch.begin();
	world.getBodies(tmpBodies);
	for (Body body : tmpBodies){
		if(body.getUserData() != null && body.getUserData() instanceof Sprite) {
			Sprite sprite = (Sprite) body.getUserData();
			sprite.setPosition(body.getPosition().x - sprite.getWidth() / 2, body.getPosition().y - sprite.getHeight() / 2);
			sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
			sprite.draw(batch);
		}
	}
	batch.end();


	world.step(1 / 60f, 8, 3);
	renderer.render(world, camera.combined);

}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
joint = null;
camera.unproject(tmp.set(screenX, screenY, 0));
world.QueryAABB(queryCallback, tmp.x, tmp.y, tmp.x, tmp.y);

	if(joint == null){
		randomSquareGenerator();
	}
    return true;
}

public void randomSquareGenerator(){
Random rand = new Random();
int n = rand.nextInt(5);

    Vector2 mouseLoc = new Vector2(Gdx.input.getX()/scale -  Gdx.graphics.getWidth()/(2 * scale), Gdx.graphics.getHeight()/(2 * scale) - Gdx.input.getY()/scale);

    BodyDef bodyDef = new BodyDef();
    bodyDef.position.set(mouseLoc);
    bodyDef.type = BodyDef.BodyType.DynamicBody;

    PolygonShape square = new PolygonShape();
    square.setAsBox(25*n+1, 25*n+1);

	box = world.createBody(bodyDef);

	FixtureDef squareFixture = new FixtureDef();
    squareFixture.density = 0.5f;
    squareFixture.friction = 0.5f;
    squareFixture.shape = square;
    squareFixture.restitution = 1.0f;

	boxSprite = new Sprite(new Texture(Gdx.files.internal("player.png")));

	boxSprite.setSize(25*n + 1, 25*n + 1);
	boxSprite.setPosition(box.getPosition().x - boxSprite.getWidth() / 2, box.getPosition().y - boxSprite.getHeight() / 2);
	boxSprite.setRotation(box.getAngle() * MathUtils.radiansToDegrees);


	box.setUserData(boxSprite);
	box.createFixture(squareFixture);

    square.dispose();
	boxSprite.getTexture().dispose();

}

Does anyone have any ideas? I’ve been looking all over the web & now my searches are returning my own question from here.