smoothDelta is now come from Gdx's LwjglGraphics instead of AppLoader

This commit is contained in:
minjaesong
2019-01-22 13:16:21 +09:00
parent a59a1d5c2f
commit 07373e13d2
13 changed files with 27 additions and 127 deletions

View File

@@ -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;
}
}

View File

@@ -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() {

View File

@@ -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. */

View File

@@ -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:
*

View File

@@ -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

View File

@@ -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

View File

@@ -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.

View File

@@ -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())
}
}
}

View File

@@ -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) {

View File

@@ -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()

View File

@@ -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

View File

@@ -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
}
}

View File

@@ -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())
}
}
}