From f650bea7ad4e9b2960105dfe67b637335b5680af Mon Sep 17 00:00:00 2001 From: Gulum Date: Thu, 19 Oct 2017 01:28:40 +0200 Subject: [PATCH] Implemented render Prediction --- core/src/com/darkrp2d/DarkRP2D.java | 8 ++++---- core/src/com/darkrp2d/entitySystems/RenderSystem.java | 9 +++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/core/src/com/darkrp2d/DarkRP2D.java b/core/src/com/darkrp2d/DarkRP2D.java index f6e9bc5..c615bf7 100755 --- a/core/src/com/darkrp2d/DarkRP2D.java +++ b/core/src/com/darkrp2d/DarkRP2D.java @@ -67,7 +67,7 @@ public class DarkRP2D extends Game { @Override // GameLoop public void render () { - accumulator += Gdx.graphics.getDeltaTime(); //Seconds to milliseconds + accumulator += Gdx.graphics.getRawDeltaTime(); //Seconds to milliseconds //Process Input Command currentCommand = CommandStream.pollCommand(); @@ -84,7 +84,7 @@ public class DarkRP2D extends Game { skippedFrames++; } - trueRender(); + trueRender(accumulator / MS_PER_UPDATE); } public void update(float deltaTime) { @@ -92,7 +92,7 @@ public class DarkRP2D extends Game { engine.update(deltaTime); } - public void trueRender() { + public void trueRender(float delta) { Gdx.gl.glClearColor(0.f, 0.f, 0.f, 1.f); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); @@ -108,7 +108,7 @@ public class DarkRP2D extends Game { b2dbr.render(world, debugMatrix); //TODO: will be later moved to Gamescreen - engine.getSystem(RenderSystem.class).render(batch); + engine.getSystem(RenderSystem.class).render(delta, batch); //some testdrawing batch.begin(); diff --git a/core/src/com/darkrp2d/entitySystems/RenderSystem.java b/core/src/com/darkrp2d/entitySystems/RenderSystem.java index bbedad3..230c5ee 100644 --- a/core/src/com/darkrp2d/entitySystems/RenderSystem.java +++ b/core/src/com/darkrp2d/entitySystems/RenderSystem.java @@ -11,6 +11,7 @@ import com.badlogic.ashley.core.EntitySystem; import com.badlogic.ashley.core.Family; import com.badlogic.ashley.utils.ImmutableArray; import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.math.Vector2; import com.darkrp2d.entities.components.Mappers; import com.darkrp2d.entities.components.PhysicComponent; @@ -33,14 +34,18 @@ public class RenderSystem extends EntitySystem { entities = engine.getEntitiesFor(Family.all(TextureComponent.class, PhysicComponent.class).get()); } - public void render(SpriteBatch batch) { + public void render(float delta, SpriteBatch batch) { batch.begin(); for (int i = 0; i < entities.size(); ++i) { Entity entity = entities.get(i); PhysicComponent physics = Mappers.physicComponentMapper.get(entity); TextureComponent texture = Mappers.textureComponentMapper.get(entity); - batch.draw(texture.tex, Box2DUtils.worldToScreen(physics.body.getPosition().x) - texture.tex.getWidth() / 2, Box2DUtils.worldToScreen(physics.body.getPosition().y) - texture.tex.getHeight()/2); + Vector2 vel = physics.body.getLinearVelocity(); + vel.scl(1.f/60.f); + vel.scl(delta); + + batch.draw(texture.tex, Box2DUtils.worldToScreen(physics.body.getPosition().x + vel.x) - texture.tex.getWidth() / 2, Box2DUtils.worldToScreen(physics.body.getPosition().y + vel.y) - texture.tex.getHeight()/2); } batch.end(); }