Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Outer most class
- public class GameScreen {
- GameRenderer renderer;
- GameWorld world;
- // Called every frame. delta is the time since last frame.
- public void render(float delta) {
- float dt = world.update(delta);
- renderer.render(dt);
- }
- }
- public class GameRenderer {
- // Interpolation code (smooth states)
- static void interpolate(Vector2 newPosition, Vector2 oldPosition, float fraction, Vector2 vertex) {
- vertex.x = (1 - fraction) * oldPosition.x + fraction * newPosition.x;
- vertex.y = (1 - fraction) * oldPosition.y + fraction * newPosition.y;
- }
- // Main render method
- public void render(float delta) {
- batcher.begin();
- for (int i = 0; i < world.bodies.size(); i++) {
- GameCircle body = world.bodies.get(i);
- renderCircle(body, delta / GameWorld.timeDelta);
- }
- batcher.end();
- }
- private void renderCircle(GameCircle circle, float fraction) {
- tmpVertex3.x = circle.getX();
- tmpVertex3.y = circle.getY();
- GameRenderer.interpolate(tmpVertex3, circle._lastPosition, fraction, tmpVertex1);
- mapCoordinate(tmpVertex1.x, tmpVertex1.y, tmpVertex2);
- float radius = scale(circle.getRadius());
- sprite.setBounds(tmpVertex2.x - radius, tmpVertex2.y - radius, radius * 2, radius * 2);
- sprite.draw(batcher);
- }
- }
- // Physics updates
- public class GameWorld {
- // delta is time elapsed since last frame.
- // this is called every frame.
- // It returns the accumulator
- public float update(float delta) {
- physicsDelta += Math.min(delta, 0.25f);
- while (physicsDelta >= GameWorld.timeDelta) {
- rememberWorld();
- protagonist.body.applyForceToCenter(force, true);
- world.step(GameWorld.timeDelta, 8, 3);
- physicsDelta -= GameWorld.timeDelta;
- }
- return physicsDelta;
- }
- // Remembers the current state of the world
- public void rememberWorld() {
- for (int i = bodies.size() - 1; i >= 0; i--) {
- GameCircle body = bodies.get(i);
- body._lastPosition.x = body.getX();
- body._lastPosition.y = body.getY();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement