From a8644582cb484f6f06caf8f37281725ac25de73c Mon Sep 17 00:00:00 2001 From: austin Date: Sun, 3 Apr 2022 00:21:42 -0400 Subject: [PATCH] Time update --- assets/Data/creatures/human.json | 4 +- .../electrosphere/controls/CameraHandler.java | 2 +- .../controls/ControlHandler.java | 6 +- .../state/movement/GroundMovementTree.java | 10 ++-- .../game/client/ClientFunctions.java | 30 ++++++++++ .../game/collision/CollisionEngine.java | 1 - .../game/simulation/MicroSimulation.java | 9 +-- src/main/java/electrosphere/main/Main.java | 58 ++++++------------- .../renderer/RenderingEngine.java | 1 - 9 files changed, 63 insertions(+), 58 deletions(-) diff --git a/assets/Data/creatures/human.json b/assets/Data/creatures/human.json index 6d720a8a..4146eb43 100644 --- a/assets/Data/creatures/human.json +++ b/assets/Data/creatures/human.json @@ -63,8 +63,8 @@ "movementSystems" : [ { "type" : "GROUND", - "acceleration" : 1000.0, - "maxVelocity" : 2.5, + "acceleration" : 10.0, + "maxVelocity" : 0.025, "animationStartup" : { "name" : "Armature|Jog", "length" : 1, diff --git a/src/main/java/electrosphere/controls/CameraHandler.java b/src/main/java/electrosphere/controls/CameraHandler.java index e2aaba92..4aca9127 100644 --- a/src/main/java/electrosphere/controls/CameraHandler.java +++ b/src/main/java/electrosphere/controls/CameraHandler.java @@ -41,7 +41,7 @@ public class CameraHandler { } public void updateGlobalCamera(){ - cameraSpeed = 2.5f * Main.deltaTime; + cameraSpeed = 2.5f * Main.deltaFrames; if(Crosshair.getCrosshairActive()){ diff --git a/src/main/java/electrosphere/controls/ControlHandler.java b/src/main/java/electrosphere/controls/ControlHandler.java index 968f637c..a55e36a0 100644 --- a/src/main/java/electrosphere/controls/ControlHandler.java +++ b/src/main/java/electrosphere/controls/ControlHandler.java @@ -970,10 +970,10 @@ public class ControlHandler { if(!control.isState()){ //on press control.onPress(); - control.setPressFrame(Main.lastFrame); + control.setPressFrame(Main.deltaFrames); } else { //on repeat - if(Main.lastFrame - control.getPressFrame() > control.getRepeatTimeout()){ + if(Main.deltaFrames - control.getPressFrame() > control.getRepeatTimeout()){ control.onRepeat(); } } @@ -983,7 +983,7 @@ public class ControlHandler { //on release control.onRelease(); //on click - if(Main.lastFrame - control.getPressFrame() < control.getRepeatTimeout()){ + if(Main.deltaFrames - control.getPressFrame() < control.getRepeatTimeout()){ control.onClick(); } } else { diff --git a/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java b/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java index e32b27cc..3ce7c3f5 100644 --- a/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java +++ b/src/main/java/electrosphere/entity/state/movement/GroundMovementTree.java @@ -232,7 +232,7 @@ public class GroundMovementTree { } } //run startup code - velocity = velocity + acceleration * Main.deltaTime; + velocity = velocity + acceleration * Main.deltaFrames; //check if can transition state if(velocity >= maxNaturalVelocity){ velocity = maxNaturalVelocity; @@ -248,7 +248,7 @@ public class GroundMovementTree { // newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition); // } // //actually update - collidable.addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), new Vector3d(0,0,0), velocity * Main.deltaTime, "movement")); + collidable.addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), new Vector3d(0,0,0), velocity * Main.deltaFrames, "movement")); // position.set(newPosition); rotation.set(movementQuaternion); @@ -328,7 +328,7 @@ public class GroundMovementTree { // if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){ // newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition); // } - collidable.addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), new Vector3d(0,0,0), velocity * Main.deltaTime, "movement")); + collidable.addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), new Vector3d(0,0,0), velocity * Main.deltaFrames, "movement")); // position.set(newPosition); rotation.set(movementQuaternion); @@ -396,7 +396,7 @@ public class GroundMovementTree { } } //velocity stuff - velocity = velocity - acceleration * Main.deltaTime; + velocity = velocity - acceleration * Main.deltaFrames; //check if can transition state if(velocity <= 0){ velocity = 0; @@ -416,7 +416,7 @@ public class GroundMovementTree { // if(!Globals.collisionEngine.checkCanOccupyPosition(Globals.commonWorldData, parent, newPosition)){ // newPosition = Globals.collisionEngine.suggestMovementPosition(Globals.commonWorldData, parent, newPosition); // } - collidable.addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), new Vector3d(0,0,0), velocity * Main.deltaTime, "movement")); + collidable.addImpulse(new Impulse(new Vector3d(movementVector), new Vector3d(0,0,0), new Vector3d(0,0,0), velocity * Main.deltaFrames, "movement")); // position.set(newPosition); rotation.set(movementQuaternion); diff --git a/src/main/java/electrosphere/game/client/ClientFunctions.java b/src/main/java/electrosphere/game/client/ClientFunctions.java index 26f9bd9b..b6aa67fd 100644 --- a/src/main/java/electrosphere/game/client/ClientFunctions.java +++ b/src/main/java/electrosphere/game/client/ClientFunctions.java @@ -3,6 +3,8 @@ package electrosphere.game.client; import electrosphere.entity.EntityUtils; import electrosphere.entity.types.camera.CameraEntityUtils; import electrosphere.main.Globals; + +import org.joml.Vector3d; import org.joml.Vector3f; /** @@ -10,12 +12,26 @@ import org.joml.Vector3f; * @author amaterasu */ public class ClientFunctions { + + static Vector3d oldPlayerCharacterPosition = new Vector3d(); + static Vector3d newPlayerCharacterPosition = new Vector3d(); + + public static void runBeforeSimulationFunctions(){ + //cell tracking values + if(Globals.RUN_CLIENT){ + if(Globals.playerCharacter != null){ + oldPlayerCharacterPosition = new Vector3d(EntityUtils.getPosition(Globals.playerCharacter)); + } + } + } + public static void runClientFunctions(){ if(Globals.clientTerrainManager != null){ Globals.clientTerrainManager.handleMessages(); Globals.clientTerrainManager.ejectLoadedChunks(); } updateSkyboxPos(); + updateCellManager(); } static void updateSkyboxPos(){ @@ -29,4 +45,18 @@ public class ClientFunctions { EntityUtils.getPosition(Globals.skybox).set(EntityUtils.getPosition(Globals.playerCharacter)); } } + + static void updateCellManager(){ + /// + /// C L I E N T C E L L M A N A G E R + /// + if(Globals.drawCellManager != null){ + if(Globals.playerCharacter != null){ + newPlayerCharacterPosition = EntityUtils.getPosition(Globals.playerCharacter); + } + //Cell manager do your things + Globals.drawCellManager.calculateDeltas(oldPlayerCharacterPosition, newPlayerCharacterPosition); + Globals.drawCellManager.update(); + } + } } diff --git a/src/main/java/electrosphere/game/collision/CollisionEngine.java b/src/main/java/electrosphere/game/collision/CollisionEngine.java index 63c4beb9..c125aee9 100644 --- a/src/main/java/electrosphere/game/collision/CollisionEngine.java +++ b/src/main/java/electrosphere/game/collision/CollisionEngine.java @@ -29,7 +29,6 @@ import electrosphere.entity.state.collidable.Impulse; import electrosphere.entity.types.hitbox.HitboxData; import electrosphere.game.collision.collidable.Collidable; -import static electrosphere.main.Main.deltaTime; import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/electrosphere/game/simulation/MicroSimulation.java b/src/main/java/electrosphere/game/simulation/MicroSimulation.java index d22ce1e2..d5b9bd1b 100644 --- a/src/main/java/electrosphere/game/simulation/MicroSimulation.java +++ b/src/main/java/electrosphere/game/simulation/MicroSimulation.java @@ -19,10 +19,11 @@ import electrosphere.entity.state.movement.SprintTree; import electrosphere.entity.types.particle.ParticleUtils; import electrosphere.game.client.targeting.crosshair.Crosshair; import electrosphere.main.Globals; +import electrosphere.main.Main; import electrosphere.renderer.actor.Actor; -import static electrosphere.main.Main.deltaTime; +import org.joml.Vector3d; import org.joml.Vector3f; /** @@ -39,7 +40,7 @@ public class MicroSimulation { public void simulate(){ //simulate bullet physics engine step - Globals.collisionEngine.simulatePhysics(deltaTime); + Globals.collisionEngine.simulatePhysics(Main.deltaFrames); //update dynamic entity positions calculated by bullet // Globals.collisionEngine.updateDynamicObjectTransforms(); //list dynamic object positions @@ -52,7 +53,7 @@ public class MicroSimulation { Actor currentActor = EntityUtils.getActor(currentEntity); //increment animations if(currentActor.isPlayingAnimation()){ - currentActor.incrementAnimationTime(deltaTime); + currentActor.incrementAnimationTime(Main.deltaFrames / Main.targetFrameRate); } } //make items play idle animation @@ -72,7 +73,7 @@ public class MicroSimulation { //simulate creature gravity trees for(Entity currentGravity : Globals.entityManager.getGravityEntities()){ GravityTree gravityTree = (GravityTree)currentGravity.getData(EntityDataStrings.GRAVITY_TREE); - gravityTree.simulate(deltaTime); + gravityTree.simulate(Main.deltaFrames); } //attacker behavior tree for(Entity currentAttacker : Globals.entityManager.getAttackerEntities()){ diff --git a/src/main/java/electrosphere/main/Main.java b/src/main/java/electrosphere/main/Main.java index c401584b..1afd0367 100644 --- a/src/main/java/electrosphere/main/Main.java +++ b/src/main/java/electrosphere/main/Main.java @@ -66,9 +66,10 @@ public class Main { //Visualization Controls // //These are used in calculating the time between frames for visualization (camera) control and such - public static float deltaTime = 0.0f; - public static float lastFrame = 0.0f; + static double deltaTime = 0.0f; + static double lastFrame = 0.0f; public static long frameCount = 0; + public static float deltaFrames = 0; //View Controls public static float view_Range = 200000.0f; /* @@ -105,7 +106,9 @@ public class Main { public static Entity letterEntity; - static float targetFrameRate = 1.0f/144.0f; + //target amount of time per frame + public static float targetFrameRate = 60.0f; + static float targetFramePeriod = 1.0f/targetFrameRate; public static void main(String args[]){ @@ -225,8 +228,9 @@ public class Main { /* Frame calculation */ - float currentFrame = (float) glfwGetTime(); + double currentFrame = glfwGetTime(); deltaTime = currentFrame - lastFrame; + deltaFrames = targetFrameRate * (float)deltaTime; lastFrame = currentFrame; @@ -251,19 +255,6 @@ public class Main { /// /// I N P U T C O N T R O L S /// - // cameraSpeed = 2.5f * deltaTime; -// if (glfwGetKey(Globals.window, GLFW_KEY_ESCAPE) == GLFW_PRESS && Globals.RENDER_FLAG_RENDER_SCREEN_FRAMEBUFFER_CONTENT) { -// break; -// } - - //cell tracking values - Vector3d oldPlayerCharacterPosition = new Vector3d(); - if(Globals.playerCharacter != null){ - oldPlayerCharacterPosition = new Vector3d(EntityUtils.getPosition(Globals.playerCharacter)); - } - Vector3d newPlayerCharacterPosition = oldPlayerCharacterPosition; - - //Poll controls Globals.controlHandler.pollControls(); Globals.controlHandler.recaptureIfNecessary(); @@ -273,6 +264,7 @@ public class Main { /// /// C L I E N T S I M U L A T I O N S T U F F /// + ClientFunctions.runBeforeSimulationFunctions(); if(Globals.microSimulation != null && Globals.microSimulation.isReady()){ Globals.microSimulation.simulate(); } @@ -285,26 +277,6 @@ public class Main { Globals.macroSimulation.simulate(); } - // - // P L A Y E R W O R L D P O S I T I O N U P D A T E - // - // if(Globals.playerCharacter != null && Globals.commonWorldData != null){ - // EntityUtils.getRotation(Globals.playerCharacter).rotateX(0.01f); - // newPlayerCharacterPosition = EntityUtils.getPosition(Globals.playerCharacter); - // Globals.clientPlayerData.setWorldPosition(Globals.commonWorldData.convertRealToWorld(newPlayerCharacterPosition.x), Globals.commonWorldData.convertRealToWorld(newPlayerCharacterPosition.z)); - // } - - /// - /// C L I E N T C E L L M A N A G E R - /// - if(Globals.drawCellManager != null){ - - //Cell manager do your things - Globals.drawCellManager.calculateDeltas(oldPlayerCharacterPosition, newPlayerCharacterPosition); - Globals.drawCellManager.update(); - } - - Globals.renderingEngine.drawScreen(); @@ -314,15 +286,18 @@ public class Main { running = false; } - // System.out.println(deltaTime + " - " + targetFrameRate); - if(deltaTime < targetFrameRate){ - sleep((int)(1000.0 * (targetFrameRate - deltaTime))); + if(deltaTime < targetFramePeriod){ + sleep((int)(1000.0 * (targetFramePeriod - deltaTime))); } else { sleep(1); } frameCount++; - // sleep((int)(1000.0*Math.max(0.001, deltaTime-targetFrameRate))); + } + + // + // S H U T D O W N + // //Terminate the program. glfwTerminate(); //used to signal threads to stop @@ -333,6 +308,7 @@ public class Main { } //shut down audio engine Globals.audioEngine.shutdown(); + } static void sleep(int i) { diff --git a/src/main/java/electrosphere/renderer/RenderingEngine.java b/src/main/java/electrosphere/renderer/RenderingEngine.java index b2470cbe..0f684213 100644 --- a/src/main/java/electrosphere/renderer/RenderingEngine.java +++ b/src/main/java/electrosphere/renderer/RenderingEngine.java @@ -14,7 +14,6 @@ import electrosphere.game.server.pathfinding.navmesh.NavMesh; import electrosphere.game.server.pathfinding.navmesh.NavShape; import electrosphere.logger.LoggerInterface; import electrosphere.main.Globals; -import static electrosphere.main.Main.deltaTime; import static electrosphere.main.Main.view_Range; import static electrosphere.renderer.RenderUtils.createScreenTextureVAO;