Question

I have my image (for the sake of explaining, lets call it Image.png) and I'm trying to get it to scale to properly fit in my Bodies I have drawn. The bodies and everything work with the debug but I'm not a fan of it.. I'm using a SpriteBatch to draw the background image onto the screen(it would be nice to scale that too). How would I scaleImage.png to the same size/position as a Dynamic body that has been rendered?

EDIT: While drawing the images, I cant get them to match up with the debug render Bodies..

Creating Body:

                BodyDef bodydef2 = new BodyDef();  
                bodydef2.type = BodyType.DynamicBody;  
                bodydef2.position.set(camera.viewportWidth + 30.0f, (int)(Math.random()*camera.viewportHeight)%(LastPlatformY+5)+20);  
                //System.out.println(bodydef2.position.y + " " + LastPlatformY);
                Body block = world.createBody(bodydef2);  
                PolygonShape Shape2 = new PolygonShape();  
                Shape2.setAsBox(750*WORLD_TO_BOX, 200*WORLD_TO_BOX);  
                FixtureDef fixtureDef2 = new FixtureDef();  
                fixtureDef2.shape = Shape2;  
                fixtureDef2.density = 1000.0f; 
                fixtureDef2.friction = 0.0f;  
                fixtureDef2.restitution = 0; 
                block.setLinearVelocity(new Vector2((float) (Platform_Velocity*-1), 0));
                block.createFixture(fixtureDef2); 
                //Lock 'block' to X-Axis, Relative to floor.
                PrismaticJointDef jointDef = new PrismaticJointDef();
                jointDef.collideConnected = true;
                jointDef.initialize(block, groundBody, block.getWorldCenter(), new Vector2(1, 0)); 
                world.createJoint(jointDef);
                Platforms.add(block);
                Platforms_Created++;
                LastPlatformY = (int)bodydef2.position.y;

Drawing Image:

sp.draw(platforms, (float)(Platforms.get(i).getPosition().x), (float)(Platforms.get(i).getPosition().y), 75,20);

EDIT #2: turns out that if your camera is smaller than the size of your screen you have to do some compensation to account for that variation in positions.. Problem solved, thanks!

Was it helpful?

Solution

Just save the size of your body in two variables while creating your body and then call the appropriate draw method. To get the position of your object call body.getPosition().

For example

Texture t = assetManager.get("your texture", Texture.class);

BodyDef bodyDef = new BodyDef();
body.type = BodyType.DynamicBody

float width = 64 * WORLD_TO_BOX;
float height = 64 * WORLD_TO_BOX;

Shape shape = new PolygonShape();
float[] points = new float[]{0,0,width,0,width,height,0, height};
shape.set(points);
FixtureDef def = new FixtureDef();
def.shape = shape;
...
body.createFixture(def);
shape.dispose();

And later when you want to draw

public void render(Spritebatch batch) {
    batch.draw(t, body.getPosition().x * BOX_TO_WORLD, body.getPosition().y * BOX_TO_WORLD, width * BOX_TO_WORLD, height * BOX_TO_WORLD);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top