From 07373e13d290c18d12f8fc041ca4333ddc28459f Mon Sep 17 00:00:00 2001 From: minjaesong Date: Tue, 22 Jan 2019 13:16:21 +0900 Subject: [PATCH] smoothDelta is now come from Gdx's LwjglGraphics instead of AppLoader --- .../gdx/backends/lwjgl/LwjglGraphics.java | 7 +- src/net/torvald/terrarum/AppLoader.java | 84 +------------------ .../terrarum/FuckingWorldRenderer.kt.unused | 6 +- src/net/torvald/terrarum/Terrarum.kt | 4 +- src/net/torvald/terrarum/TitleScreen.kt | 2 +- .../terrarum/blockproperties/BlockPropUtil.kt | 7 +- .../terrarum/gameactors/ActorWBMovable.kt | 2 +- .../gamecontroller/IngameController.kt | 8 +- .../torvald/terrarum/modulebasegame/Ingame.kt | 16 +--- .../gameactors/ActorInventory.kt | 2 +- .../modulebasegame/gameactors/ParticleBase.kt | 2 +- .../modulebasegame/gameactors/Pocketed.kt | 8 +- .../gameactors/ThreadActorUpdate.kt | 6 +- 13 files changed, 27 insertions(+), 127 deletions(-) diff --git a/src/com/badlogic/gdx/backends/lwjgl/LwjglGraphics.java b/src/com/badlogic/gdx/backends/lwjgl/LwjglGraphics.java index 58977a7b7..8a24c7003 100644 --- a/src/com/badlogic/gdx/backends/lwjgl/LwjglGraphics.java +++ b/src/com/badlogic/gdx/backends/lwjgl/LwjglGraphics.java @@ -66,7 +66,6 @@ public class LwjglGraphics implements Graphics { // deltaTime kalman filter related variables private float kalmanEstimate = 1.0f/60.0f; - private float kalmanReturnValue = kalmanEstimate; private float kalmanErrorCovariance = 1.0f; private final float kalmanErrorRate = 0.2f; // 0.2: empirical value private final float kalmanUpdateThreshold = 0.1f; @@ -119,7 +118,7 @@ public class LwjglGraphics implements Graphics { } public float getDeltaTime () { - return kalmanReturnValue; + return kalmanEstimate; } private void resetDeltaSmoothingHistory() { @@ -152,7 +151,7 @@ public class LwjglGraphics implements Graphics { // // It's not perfect but it works, much better than averaging. - if (getMagnitudeDifference(deltaTime, kalmanReturnValue) >= 2.0) { + if (getMagnitudeDifference(deltaTime, kalmanEstimate) >= 2.0) { resetDeltaSmoothingHistory(); } @@ -171,8 +170,6 @@ public class LwjglGraphics implements Graphics { kalmanEstimate = newEstimate; kalmanErrorCovariance = newError; - - kalmanReturnValue = newEstimate; } } diff --git a/src/net/torvald/terrarum/AppLoader.java b/src/net/torvald/terrarum/AppLoader.java index 5da1db2da..026cb3a70 100644 --- a/src/net/torvald/terrarum/AppLoader.java +++ b/src/net/torvald/terrarum/AppLoader.java @@ -237,86 +237,11 @@ public class AppLoader implements ApplicationListener { updateFullscreenQuad(appConfig.width, appConfig.height); } - private static double _kalman_xhat_k = UPDATE_RATE; - private static double _kalman_return_value = _kalman_xhat_k; - private static double _kalman_p_k = 1.0; - private static final double _kalman_R = 0.2; // 0.2: empirical value - private final double _KALMAN_UPDATE_THRE = 0.1; - - /** - * Because fuck you GDX. (No, really; take a look at LwjglGraphics.java, getDeltaTime() and rawDeltaTime() are exactly the same) - * @return Render delta that is smoothed out. - */ - public static double getSmoothDelta() { - // kalman filter is calculated but not actually being used. - - - // below is the kalman part - return _kalman_return_value; - } - - public static void resetDeltaSmoothingHistory() { - _kalman_xhat_k = UPDATE_RATE; - _kalman_p_k = 1.0; - } - /** * @link http://bilgin.esme.org/BitsAndBytes/KalmanFilterforDummies */ private void updateKalmanRenderDelta() { - - // TODO implement nonlinear kalman filter - - // The problem with this kalman filter is that it assumes most simplistic situation: - // 1. the actual delta (measured delta - noise) is constant (that is, not constantly increasing or something) - // 2. everything is linear - // We may need to implement Extended Kalman Filter but what is Jacobian, I suck at maths. - // - // Instead, this implementation will reset itself when difference in magnitude between - // old and new is greater than set value. - // - // It's not perfect but it works, much better than averaging. - - double observation = ((double) Gdx.graphics.getRawDeltaTime()); - - if (getMul(observation, _kalman_return_value) >= 2.0) { - resetDeltaSmoothingHistory(); - } - - // measurement value - double _kalman_zed_k = observation; - - if (_kalman_zed_k <= _KALMAN_UPDATE_THRE) { - // time update - double _kalman_xhatminus_k = _kalman_xhat_k; - double _kalman_pminus_k = _kalman_p_k; - - // measurement update - double _kalman_gain = _kalman_pminus_k / (_kalman_pminus_k + _kalman_R); - double _kalman_xhat_kNew = _kalman_xhatminus_k + _kalman_gain * (_kalman_zed_k - _kalman_xhatminus_k); - double _kalman_p_kNew = (1.0 - _kalman_gain) * _kalman_pminus_k; - - _kalman_xhat_k = _kalman_xhat_kNew; - _kalman_p_k = _kalman_p_kNew; - - _kalman_return_value = _kalman_xhat_kNew; - } - } - - - private final double getMul_epsilon = 0.00001; - // only for a > 0 && b > 0 - private double getMul(double a, double b) { - if (a < getMul_epsilon || b < getMul_epsilon) { - return a + b; - } - - if (a > b) { - return a / b; - } - else { - return b / a; - } + // moved to LwjglGraphics } @Override @@ -327,9 +252,6 @@ public class AppLoader implements ApplicationListener { postInit(); } - // update smooth delta AFTER postInit - updateKalmanRenderDelta(); - FrameBufferManager.begin(renderFBO); gdxClearAndSetBlend(.094f, .094f, .094f, 0f); setCameraPosition(0, 0); @@ -427,8 +349,6 @@ public class AppLoader implements ApplicationListener { updateFullscreenQuad(screenW, screenH); printdbg(this, "Resize event"); - - resetDeltaSmoothingHistory(); } @Override @@ -473,8 +393,6 @@ public class AppLoader implements ApplicationListener { } printdbg(this, "Screen transisiton complete: " + this.screen.getClass().getCanonicalName()); - - resetDeltaSmoothingHistory(); } private void postInit() { diff --git a/src/net/torvald/terrarum/FuckingWorldRenderer.kt.unused b/src/net/torvald/terrarum/FuckingWorldRenderer.kt.unused index f7dc3a94f..37f9e8be9 100644 --- a/src/net/torvald/terrarum/FuckingWorldRenderer.kt.unused +++ b/src/net/torvald/terrarum/FuckingWorldRenderer.kt.unused @@ -464,7 +464,7 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) { override fun run() { var updateTries = 0 while (ingame.updateDeltaCounter >= ingame.updateRate) { - ingame.updateGame(AppLoader.getSmoothDelta().toFloat()) + ingame.updateGame(AppLoader.UPDATE_RATE.toFloat()) ingame.updateDeltaCounter -= ingame.updateRate updateTries++ @@ -1090,7 +1090,7 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) { } playableActorDelegate = newActor - WorldSimulator(player, AppLoader.getSmoothDelta().toFloat()) + WorldSimulator(player, AppLoader.UPDATE_RATE.toFloat()) } private fun changePossession(refid: Int) { @@ -1107,7 +1107,7 @@ class Ingame(batch: SpriteBatch) : IngameInstance(batch) { // accept new delegate playableActorDelegate = PlayableActorDelegate(getActorByID(refid) as ActorHumanoid) playableActorDelegate!!.actor.collisionType = ActorWithPhysics.COLLISION_KINEMATIC - WorldSimulator(player, AppLoader.getSmoothDelta().toFloat()) + WorldSimulator(player, AppLoader.UPDATE_RATE.toFloat()) } /** Send message to notifier UI and toggle the UI as opened. */ diff --git a/src/net/torvald/terrarum/Terrarum.kt b/src/net/torvald/terrarum/Terrarum.kt index 86fe0c925..e1a429f93 100644 --- a/src/net/torvald/terrarum/Terrarum.kt +++ b/src/net/torvald/terrarum/Terrarum.kt @@ -400,7 +400,7 @@ object Terrarum : Screen { override fun render(delta: Float) { AppLoader.debugTimers["GDX.rawDelta"] = Gdx.graphics.rawDeltaTime.times(1000_000_000f).toLong() - AppLoader.debugTimers["GDX.smtDelta"] = AppLoader.getSmoothDelta().times(1000_000_000f).toLong() + AppLoader.debugTimers["GDX.smtDelta"] = Gdx.graphics.deltaTime.times(1000_000_000f).toLong() AppLoader.getINSTANCE().screen.render(delta) } @@ -475,7 +475,7 @@ object Terrarum : Screen { get() = Gdx.input.y /** Delta converted as it it was a FPS */ inline val updateRate: Double - get() = 1.0 / AppLoader.getSmoothDelta() + get() = 1.0 / Gdx.graphics.deltaTime /** * Usage: * diff --git a/src/net/torvald/terrarum/TitleScreen.kt b/src/net/torvald/terrarum/TitleScreen.kt index fc7de8f44..ce64112fc 100644 --- a/src/net/torvald/terrarum/TitleScreen.kt +++ b/src/net/torvald/terrarum/TitleScreen.kt @@ -203,7 +203,7 @@ class TitleScreen(val batch: SpriteBatch) : Screen { override fun render(delta: Float) { // async update and render - val dt = AppLoader.getSmoothDelta() + val dt = Gdx.graphics.deltaTime updateAkku += dt var i = 0L diff --git a/src/net/torvald/terrarum/blockproperties/BlockPropUtil.kt b/src/net/torvald/terrarum/blockproperties/BlockPropUtil.kt index 4a5846440..d410fa5da 100644 --- a/src/net/torvald/terrarum/blockproperties/BlockPropUtil.kt +++ b/src/net/torvald/terrarum/blockproperties/BlockPropUtil.kt @@ -4,7 +4,6 @@ import com.badlogic.gdx.Gdx import com.badlogic.gdx.graphics.Color import com.jme3.math.FastMath import net.torvald.random.HQRNG -import net.torvald.terrarum.AppLoader import net.torvald.terrarum.Second import net.torvald.terrarum.Terrarum import net.torvald.terrarum.modulebasegame.gameworld.WorldTime @@ -64,9 +63,9 @@ object BlockPropUtil { internal fun dynamicLumFuncTickClock() { // FPS-time compensation if (Gdx.graphics.framesPerSecond > 0) { - flickerFuncX += AppLoader.getSmoothDelta().toFloat() * 1000f - breathFuncX += AppLoader.getSmoothDelta().toFloat() * 1000f - pulsateFuncX += AppLoader.getSmoothDelta().toFloat() * 1000f + flickerFuncX += Gdx.graphics.deltaTime * 1000f + breathFuncX += Gdx.graphics.deltaTime * 1000f + pulsateFuncX += Gdx.graphics.deltaTime * 1000f } // flicker-related vars diff --git a/src/net/torvald/terrarum/gameactors/ActorWBMovable.kt b/src/net/torvald/terrarum/gameactors/ActorWBMovable.kt index 59f238870..3e93b0087 100644 --- a/src/net/torvald/terrarum/gameactors/ActorWBMovable.kt +++ b/src/net/torvald/terrarum/gameactors/ActorWBMovable.kt @@ -344,7 +344,7 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean = inline val feetPosTile: IntArray get() = intArrayOf(hIntTilewiseHitbox.centeredX.floorInt(), hIntTilewiseHitbox.endY.floorInt()) - override fun run() = update(AppLoader.getSmoothDelta().toFloat()) + override fun run() = update(AppLoader.UPDATE_RATE.toFloat()) /** * Add vector value to the velocity, in the time unit of single frame. diff --git a/src/net/torvald/terrarum/gamecontroller/IngameController.kt b/src/net/torvald/terrarum/gamecontroller/IngameController.kt index d6f69b919..80fcd2f6f 100644 --- a/src/net/torvald/terrarum/gamecontroller/IngameController.kt +++ b/src/net/torvald/terrarum/gamecontroller/IngameController.kt @@ -131,10 +131,10 @@ class IngameController(val ingame: Ingame) : InputAdapter() { if (ingame.uiContainer.map { if ((it.isOpening || it.isOpened) && it.mouseUp) 1 else 0 }.sum() == 0) { // no UI on the mouse, right? if (button == AppLoader.getConfigInt("mouseprimary")) { - ingame.worldPrimaryClickEnd(AppLoader.getSmoothDelta().toFloat()) + ingame.worldPrimaryClickEnd(AppLoader.UPDATE_RATE.toFloat()) } if (button == AppLoader.getConfigInt("mousesecondary")) { - ingame.worldSecondaryClickEnd(AppLoader.getSmoothDelta().toFloat()) + ingame.worldSecondaryClickEnd(AppLoader.UPDATE_RATE.toFloat()) } } } @@ -172,10 +172,10 @@ class IngameController(val ingame: Ingame) : InputAdapter() { if (ingame.uiContainer.map { if ((it.isOpening || it.isOpened) && it.mouseUp) 1 else 0 }.sum() == 0) { // no UI on the mouse, right? if (button == AppLoader.getConfigInt("mouseprimary")) { - ingame.worldPrimaryClickStart(AppLoader.getSmoothDelta().toFloat()) + ingame.worldPrimaryClickStart(AppLoader.UPDATE_RATE.toFloat()) } if (button == AppLoader.getConfigInt("mousesecondary")) { - ingame.worldSecondaryClickStart(AppLoader.getSmoothDelta().toFloat()) + ingame.worldSecondaryClickStart(AppLoader.UPDATE_RATE.toFloat()) } } } diff --git a/src/net/torvald/terrarum/modulebasegame/Ingame.kt b/src/net/torvald/terrarum/modulebasegame/Ingame.kt index 54545dd13..e23f3623a 100644 --- a/src/net/torvald/terrarum/modulebasegame/Ingame.kt +++ b/src/net/torvald/terrarum/modulebasegame/Ingame.kt @@ -425,7 +425,6 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) { } } - private var countdownToDeltaReset = 15 // number of frames private var updateAkku = 0.0 override fun render(delta: Float) { @@ -447,19 +446,10 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) { gameFullyLoaded = true } - - if (countdownToDeltaReset >= 0) { - if (countdownToDeltaReset == 0) { - AppLoader.resetDeltaSmoothingHistory() - } - countdownToDeltaReset -= 1 - } - - // ASYNCHRONOUS UPDATE AND RENDER // /** UPDATE CODE GOES HERE */ - val dt = AppLoader.getSmoothDelta() + val dt = Gdx.graphics.deltaTime updateAkku += dt var i = 0L @@ -477,10 +467,6 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) { AppLoader.debugTimers["Ingame.render-Light"] = (AppLoader.debugTimers["Ingame.render"] as Long) - ((AppLoader.debugTimers["Renderer.LightTotal"] as? Long) ?: 0) - - - AppLoader.debugTimers["Gdx.deltaRaw"] = Gdx.graphics.rawDeltaTime.times(1_000_000_000).toLong() - AppLoader.debugTimers["Gdx.deltaSmt"] = Gdx.graphics.deltaTime.times(1_000_000_000).toLong() } protected fun updateGame(delta: Float) { diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt index 18c756e22..8a51b4277 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/ActorInventory.kt @@ -211,7 +211,7 @@ class ActorInventory(val actor: Pocketed, var maxCapacity: Int, var capacityMode actor.avStrength / 1000.0 else 1.0 // TODO variable: scale, strength - val swingDmgToFrameDmg = AppLoader.getSmoothDelta().toFloat().toDouble() / actor.actorValue.getAsDouble(AVKey.ACTION_INTERVAL)!! + val swingDmgToFrameDmg = AppLoader.UPDATE_RATE.toFloat().toDouble() / actor.actorValue.getAsDouble(AVKey.ACTION_INTERVAL)!! // damage the item newItem.durability -= (baseDamagePerSwing * swingDmgToFrameDmg).toFloat() diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/ParticleBase.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/ParticleBase.kt index 4fba2c23d..16d3eac7c 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/ParticleBase.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/ParticleBase.kt @@ -23,7 +23,7 @@ open class ParticleBase(renderOrder: Actor.RenderOrder, val despawnUponCollision /** Will NOT actually delete from the CircularArray */ @Volatile var flagDespawn = false - override fun run() = update(AppLoader.getSmoothDelta().toFloat()) + override fun run() = update(AppLoader.UPDATE_RATE.toFloat()) var isNoSubjectToGrav = false var dragCoefficient = 3.0 diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/Pocketed.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/Pocketed.kt index f06f8b9cf..78c5c8fe5 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/Pocketed.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/Pocketed.kt @@ -28,7 +28,7 @@ interface Pocketed { } inventory.itemEquipped[item.equipPosition] = null - item.effectOnUnequip(AppLoader.getSmoothDelta().toFloat()) + item.effectOnUnequip(AppLoader.UPDATE_RATE.toFloat()) } // no need for equipSlot(Int) @@ -50,7 +50,7 @@ interface Pocketed { if (item.equipPosition >= 0) { inventory.itemEquipped[item.equipPosition] = item - item.effectWhenEquipped(AppLoader.getSmoothDelta().toFloat()) + item.effectWhenEquipped(AppLoader.UPDATE_RATE.toFloat()) } // else do nothing } @@ -69,13 +69,13 @@ interface Pocketed { fun consumePrimary(item: GameItem) { - if (item.startPrimaryUse(AppLoader.getSmoothDelta().toFloat())) { + if (item.startPrimaryUse(AppLoader.UPDATE_RATE.toFloat())) { inventory.consumeItem(this as Actor, item) // consume on successful } } fun consumeSecondary(item: GameItem) { - if (item.startSecondaryUse(AppLoader.getSmoothDelta().toFloat())) + if (item.startSecondaryUse(AppLoader.UPDATE_RATE.toFloat())) inventory.consumeItem(this as Actor, item) // consume on successful } } \ No newline at end of file diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/ThreadActorUpdate.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/ThreadActorUpdate.kt index 0529bd201..0e71d8a78 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/ThreadActorUpdate.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/ThreadActorUpdate.kt @@ -10,13 +10,13 @@ class ThreadActorUpdate(val startIndex: Int, val endIndex: Int) : Runnable { override fun run() { for (i in startIndex..endIndex) { val it = Terrarum.ingame!!.actorContainer[i] - it.update(AppLoader.getSmoothDelta().toFloat()) + it.update(AppLoader.UPDATE_RATE.toFloat()) if (it is Pocketed) { it.inventory.forEach { inventoryEntry -> - inventoryEntry.item.effectWhileInPocket(AppLoader.getSmoothDelta().toFloat()) + inventoryEntry.item.effectWhileInPocket(AppLoader.UPDATE_RATE.toFloat()) if (it.equipped(inventoryEntry.item)) { - inventoryEntry.item.effectWhenEquipped(AppLoader.getSmoothDelta().toFloat()) + inventoryEntry.item.effectWhenEquipped(AppLoader.UPDATE_RATE.toFloat()) } } }