From 5acb1b301cfb6c5d32ba648f1b7d4842e14a3fcb Mon Sep 17 00:00:00 2001 From: Jeasonfire Date: Wed, 13 May 2015 23:40:57 +0300 Subject: [PATCH] Made physics fps-independent and removed delta limit. --- .../gladiator/components/CPhysics.java | 2 +- .../gladiator/systems/PhysicsSystem.java | 87 ++++++++++--------- .../gladiator/desktop/DesktopLauncher.java | 7 +- 3 files changed, 52 insertions(+), 44 deletions(-) diff --git a/core/src/com/saltosion/gladiator/components/CPhysics.java b/core/src/com/saltosion/gladiator/components/CPhysics.java index 131cead..5a8a7c4 100644 --- a/core/src/com/saltosion/gladiator/components/CPhysics.java +++ b/core/src/com/saltosion/gladiator/components/CPhysics.java @@ -9,7 +9,7 @@ public class CPhysics extends Component { private final Vector2 position = new Vector2(); private final Vector2 velocity = new Vector2(); private final Vector2 size = new Vector2(); - private float movespeed = 5f, jumpForce = 0.5f, gravity = 1f; + private float movespeed = 7.5f, jumpForce = 35f, gravity = 100f; private CollisionListener collisionListener = null; private boolean movable = true; diff --git a/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java b/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java index 113c48c..18d2008 100644 --- a/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java +++ b/core/src/com/saltosion/gladiator/systems/PhysicsSystem.java @@ -12,7 +12,7 @@ import com.saltosion.gladiator.util.Direction; public class PhysicsSystem extends EntitySystem { - private static final float MAX_VEL = 0.5f; + private static final float MAX_VEL = 2f, COLLISION_PRECISION = 24f, UPDATES_PER_SECOND = 120f; private static final ComponentMapper pm = ComponentMapper.getFor(CPhysics.class); private static final ComponentMapper cm = ComponentMapper.getFor(CCombat.class); @@ -25,49 +25,52 @@ public class PhysicsSystem extends EntitySystem { @Override public void update(float deltaTime) { - deltaTime = Math.min(deltaTime, 0.033f); - for (int i = 0; i < entities.size(); i++) { - CPhysics obj = pm.get(entities.get(i)); - CCombat combat = cm.get(entities.get(i)); + float freq = 1f / UPDATES_PER_SECOND; + int times = (int) Math.ceil(deltaTime / freq); + deltaTime /= (float) times; + for (int t = 0; t < times; t++) { + for (int i = 0; i < entities.size(); i++) { + CPhysics obj = pm.get(entities.get(i)); + CCombat combat = cm.get(entities.get(i)); - // Apply movement - obj.getPosition().add(obj.getVelocity()); + // Apply movement + obj.getPosition().add(Math.max(Math.min(obj.getVelocity().x * deltaTime, MAX_VEL), -MAX_VEL), + Math.max(Math.min(obj.getVelocity().y * deltaTime, MAX_VEL), -MAX_VEL)); - // Movement - if (obj.isMovable()) { - float move = 0; - if (obj.movingLeft) { - move--; - } - if (obj.movingRight) { - move++; - } - obj.getVelocity().x = move * obj.getMovespeed() * deltaTime; - if (combat != null) { - if (combat.swingCdCounter > 0) { - obj.getVelocity().x /= 2; + // Movement + if (obj.isMovable()) { + float move = 0; + if (obj.movingLeft) { + move--; + } + if (obj.movingRight) { + move++; + } + obj.getVelocity().x = move * obj.getMovespeed(); + if (combat != null) { + if (combat.swingCdCounter > 0) { + obj.getVelocity().x /= 2; + } + } + if (obj.jumping && obj.isGrounded()) { + obj.setGrounded(false); + obj.getVelocity().y = obj.getJumpForce(); } } - if (obj.jumping && obj.isGrounded()) { - obj.setGrounded(false); - obj.getVelocity().y = obj.getJumpForce(); + + // Gravity + if (obj.isGravityApplied()) { + obj.getVelocity().y -= obj.getGravity() * deltaTime; } - } - // Gravity - if (obj.isGravityApplied()) { - obj.getVelocity().y -= obj.getGravity() * deltaTime; - } - - obj.getVelocity().y = Math.max(Math.min(obj.getVelocity().y, MAX_VEL), -MAX_VEL); - - // Collisions - if (obj.isProcessCollisions()) { - for (int j = 0; j < entities.size(); j++) { - if (i == j) { - continue; + // Collisions + if (obj.isProcessCollisions()) { + for (int j = 0; j < entities.size(); j++) { + if (i == j) { + continue; + } + collision(entities.get(i), entities.get(j)); } - collision(entities.get(i), entities.get(j)); } } } @@ -94,13 +97,14 @@ public class PhysicsSystem extends EntitySystem { boolean collidedAlready = false; - if (x00 <= x11 && Math.abs(x00 - x11) < (cp0.getSize().x + cp1.getSize().x) / 16) { + if (x00 <= x11 && Math.abs(x00 - x11) < (cp0.getSize().x + cp1.getSize().x) / COLLISION_PRECISION) { // cp0's left side is colliding with cp1's right side if (!cp0.isGhost() && !cp1.isGhost()) { if (cp0.getVelocity().x < 0) { // cp0 is going left, stop cp0.getVelocity().x = 0; } + cp0.getPosition().x += x11 - x00; } if (cp0.getCollisionListener() != null) { @@ -108,13 +112,14 @@ public class PhysicsSystem extends EntitySystem { } collidedAlready = true; } - if (x01 > x10 && Math.abs(x01 - x10) < (cp0.getSize().x + cp1.getSize().x) / 16) { + if (x01 > x10 && Math.abs(x01 - x10) < (cp0.getSize().x + cp1.getSize().x) / COLLISION_PRECISION) { // cp0's right side is colliding with cp1's left side if (!cp0.isGhost() && !cp1.isGhost()) { if (cp0.getVelocity().x > 0) { // cp0 is going right, stop cp0.getVelocity().x = 0; } + cp0.getPosition().x += x10 - x01; } if (cp0.getCollisionListener() != null) { @@ -122,7 +127,7 @@ public class PhysicsSystem extends EntitySystem { } collidedAlready = true; } - if (y00 <= y11 && Math.abs(y00 - y11) < (cp0.getSize().y + cp1.getSize().y) / 16) { + if (y00 <= y11 && Math.abs(y00 - y11) < (cp0.getSize().y + cp1.getSize().y) / COLLISION_PRECISION) { // cp0's bottom side is colliding with cp1's top side if (!cp0.isGhost() && !cp1.isGhost()) { if (cp0.getVelocity().y < 0) { @@ -138,7 +143,7 @@ public class PhysicsSystem extends EntitySystem { } collidedAlready = true; } - if (y01 > y10 && Math.abs(y01 - y10) < (cp0.getSize().y + cp1.getSize().y) / 16) { + if (y01 > y10 && Math.abs(y01 - y10) < (cp0.getSize().y + cp1.getSize().y) / COLLISION_PRECISION) { // cp0's top side is colliding with cp1's bottom side if (!cp0.isGhost() && !cp1.isGhost()) { if (cp0.getVelocity().y > 0) { diff --git a/desktop/src/com/saltosion/gladiator/desktop/DesktopLauncher.java b/desktop/src/com/saltosion/gladiator/desktop/DesktopLauncher.java index 1a8942c..f8f7e08 100644 --- a/desktop/src/com/saltosion/gladiator/desktop/DesktopLauncher.java +++ b/desktop/src/com/saltosion/gladiator/desktop/DesktopLauncher.java @@ -6,11 +6,14 @@ import com.saltosion.gladiator.GladiatorBrawler; import com.saltosion.gladiator.util.Name; public class DesktopLauncher { - public static void main (String[] arg) { + + public static void main(String[] args) { LwjglApplicationConfiguration config = new LwjglApplicationConfiguration(); config.title = Name.GAME_NAME; config.width = 1280; config.height = 720; - new LwjglApplication(new GladiatorBrawler(), config); + config.vSyncEnabled = true; // Change this to false for performance testing + config.foregroundFPS = 0; + LwjglApplication app = new LwjglApplication(new GladiatorBrawler(), config); } }