Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
LibGDX rendering routine using a TiledMap renderer, the Box2D debug renderer and sprites
@Override
public void update(float deltaTime) {
/* glClear etc... */
// mapCamera is an OrthogonalCamera
mapCamera.position.set(cameraMovable.position.x * mapComponent.pixelsPerMeter,
cameraMovable.position.y * mapComponent.pixelsPerMeter, 0);
mapCamera.zoom = 1.0f/cameraComponent.camera_zoom;
mapCamera.update();
// render the map - mapRenderer is an OrthogonalTiledMapRenderer
NavigationScreen.spriteBatch.setProjectionMatrix(mapCamera.combined);
mapRenderer.render();
// render physical bodies and triggers - debugRenderer is a Box2DDebugRenderer
if(displayDebugRenderer) {
debugProjectionMatrix = mapCamera.combined.cpy();
debugProjectionMatrix.scale(mapComponent.pixelsPerMeter, mapComponent.pixelsPerMeter, 1f);
debugRenderer.render(Managers.physics.world, debugProjectionMatrix);
}
// render sprites
NavigationScreen.spriteBatch.begin();
for(/* ... */) {
// spritePosition := position in world coordinates
sprite.setCenter(spritePosition.x * mapComponent.pixelsPerMeter,
spritePosition.y * mapComponent.pixelsPerMeter);
sprite.draw(NavigationScreen.spriteBatch);
}
NavigationScreen.spriteBatch.end();
}
@Override
public void resize(int width, int height) {
float aspectRatio = (float) width / (float) height;
// in my code this is called howManyTexturePixelsWeSeeInAScreenHeight
float camHeight = screenHeightInMeters*mapComponent.pixelsPerMeter;
cameraComponent.mapCamera = new OrthographicCamera(aspectRatio*camHeight, camHeight);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.