Here's the code called in the main render loop inside the GameScreen class:
batch.begin(); for(DynamicSprite sprite : sprites) { sprite.update(this); sprite.draw(batch); } batch.end();
And here's the update() method inside DynamicSprite:
public void update(GameScreen screen) { Vector2 position = screen.getScreenPosition(getBody().getWorldCenter()); float radius = screen.scaleDistanceToScreen(getShape().getRadius()); setSize(radius * 2, radius * 2); setPositionCenter(position.x, position.y); setOrigin(getWidth() / 2, getHeight() / 2); setRotation((float)Math.toDegrees(physicsBody.getAngle())); }One thing you can see in the update() method is a lot of scaling (see lines 2&3). This is because box2D and libGDX uses two different scales. Box2D is in meters and the number of meters on the screen changes depending on the zoom of the camera. LibGDX uses the pixels on the screen. This means that the Body object's position is given and set using the former scale and the Sprite object uses the latter. Because of this, it is necessary to convert the measurements and positions. Here is the method scaleDistanceToScreen() that I wrote in the GameScreen class:
public float scaleDistanceToScreen(float distance) { return distance * screenHeight / camera.viewportHeight*(1/getZoom());}In this method, "distance" is the values we want to convert from meters to pixels. "screenHeight" is the total height of screen in pixels and camera.viewportHeight*(1/getZoom()) gives us the total height of the screen in meters. The method uses these numbers in a proportion to calculate the "distance" in pixels.
This code can be seen in context here (no promises it will still be there though if you're looking at this far in the future):
https://github.com/njwidmann/OrbitGame/tree/master/core/src/com/game/nick/orbit
|
No comments:
Post a Comment