Last active
August 29, 2015 14:07
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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