Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- Physics update code:
- // 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;
- }
- Remember world code (reset smooth states):
- // 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();
- }
- }
- Rendering code:
- private void renderCircle(GameCircle circle) {
- tmpVertex3.x = circle.getX();
- tmpVertex3.y = circle.getY();
- GameRenderer.interpolate(tmpVertex3, circle._lastPosition, physicsDelta / FIXED_TIME_STEP, 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement