Skip to content

Instantly share code, notes, and snippets.

@najmam
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save najmam/3f91d23c89250665c03a to your computer and use it in GitHub Desktop.
Save najmam/3f91d23c89250665c03a to your computer and use it in GitHub Desktop.
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