Advertisement
Guest User

Untitled

a guest
May 13th, 2015
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.48 KB | None | 0 0
  1. Interpolation code (smooth states):
  2.  
  3.     static void interpolate(Vector2 newPosition, Vector2 oldPosition, float fraction, Vector2 vertex) {
  4.         vertex.x = (1 - fraction) * oldPosition.x + fraction * newPosition.x;
  5.         vertex.y = (1 - fraction) * oldPosition.y + fraction * newPosition.y;
  6.     }
  7.  
  8. Physics update code:
  9.  
  10.     // delta is time elapsed since last frame.
  11.     // this is called every frame.
  12.     // It returns the accumulator
  13.     public float update(float delta) {
  14.         physicsDelta += Math.min(delta, 0.25f);
  15.         while (physicsDelta >= GameWorld.timeDelta) {
  16.             rememberWorld();
  17.             protagonist.body.applyForceToCenter(force, true);
  18.             world.step(GameWorld.timeDelta, 8, 3);
  19.             physicsDelta -= GameWorld.timeDelta;
  20.         }
  21.         return physicsDelta;
  22.     }
  23.  
  24. Remember world code (reset smooth states):
  25.  
  26.     // Remembers the current state of the world
  27.     public void rememberWorld() {
  28.         for (int i = bodies.size() - 1; i >= 0; i--) {
  29.             GameCircle body = bodies.get(i);
  30.             body._lastPosition.x = body.getX();
  31.             body._lastPosition.y = body.getY();
  32.         }
  33.     }
  34.  
  35. Rendering code:
  36.  
  37.     private void renderCircle(GameCircle circle) {
  38.         tmpVertex3.x = circle.getX();
  39.         tmpVertex3.y = circle.getY();
  40.         GameRenderer.interpolate(tmpVertex3, circle._lastPosition, physicsDelta / FIXED_TIME_STEP, tmpVertex1);
  41.         mapCoordinate(tmpVertex1.x, tmpVertex1.y, tmpVertex2);
  42.         float radius = scale(circle.getRadius());
  43.         sprite.setBounds(tmpVertex2.x - radius, tmpVertex2.y - radius, radius * 2, radius * 2);
  44.         sprite.draw(batcher);
  45.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement