mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-12 11:34:05 +09:00
smooth delta uses framerate-averaged instead of raw delta; stupid but works :\
This commit is contained in:
@@ -41,6 +41,9 @@ class CircularArray<T>(val size: Int) {
|
|||||||
//println("$this $unreliableAddCount")
|
//println("$this $unreliableAddCount")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getHeadElem(): T = buffer[head]
|
||||||
|
fun getTailElem(): T = buffer[tail]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Iterates the array with oldest element first.
|
* Iterates the array with oldest element first.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@@ -245,7 +244,7 @@ public class AppLoader implements ApplicationListener {
|
|||||||
private final int _KALMAN_FRAMES_TO_DISCARD = 3;
|
private final int _KALMAN_FRAMES_TO_DISCARD = 3;
|
||||||
private final double _KALMAN_UPDATE_THRE = 0.1;
|
private final double _KALMAN_UPDATE_THRE = 0.1;
|
||||||
|
|
||||||
private static final int _DELTA_ITER_AVR_SAMPLESIZE = 25;
|
private static final int _DELTA_ITER_AVR_SAMPLESIZE = 13;
|
||||||
private static CircularArray<Double> deltaHistory = new CircularArray<>(_DELTA_ITER_AVR_SAMPLESIZE);
|
private static CircularArray<Double> deltaHistory = new CircularArray<>(_DELTA_ITER_AVR_SAMPLESIZE);
|
||||||
private static double deltaAvr = 0.0;
|
private static double deltaAvr = 0.0;
|
||||||
|
|
||||||
@@ -255,14 +254,14 @@ public class AppLoader implements ApplicationListener {
|
|||||||
*/
|
*/
|
||||||
public static double getSmoothDelta() {
|
public static double getSmoothDelta() {
|
||||||
// kalman filter is calculated but not actually being used.
|
// kalman filter is calculated but not actually being used.
|
||||||
//return deltaAvr;
|
return deltaAvr;
|
||||||
|
|
||||||
|
|
||||||
// below is the kalman part
|
// below is the kalman part
|
||||||
if (_kalman_discard_requested)
|
/*if (_kalman_discard_requested)
|
||||||
return Gdx.graphics.getRawDeltaTime();
|
return Gdx.graphics.getRawDeltaTime();
|
||||||
|
|
||||||
return _kalman_xhat_k;
|
return _kalman_xhat_k;*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void resetDeltaSmoothingHistory() {
|
public static void resetDeltaSmoothingHistory() {
|
||||||
@@ -287,6 +286,9 @@ public class AppLoader implements ApplicationListener {
|
|||||||
// 2. everything is linear
|
// 2. everything is linear
|
||||||
// we may need to implement Extended Kalman Filter but wtf is Jacobian, I suck at maths.
|
// we may need to implement Extended Kalman Filter but wtf is Jacobian, I suck at maths.
|
||||||
|
|
||||||
|
//double observation = ((double) Gdx.graphics.getRawDeltaTime());
|
||||||
|
double observation = 1.0 / Gdx.graphics.getFramesPerSecond();
|
||||||
|
|
||||||
if (_kalman_discard_requested) {
|
if (_kalman_discard_requested) {
|
||||||
_kalman_discard_frame_counter += 1;
|
_kalman_discard_frame_counter += 1;
|
||||||
if (_kalman_discard_frame_counter >= _KALMAN_FRAMES_TO_DISCARD) {
|
if (_kalman_discard_frame_counter >= _KALMAN_FRAMES_TO_DISCARD) {
|
||||||
@@ -295,7 +297,7 @@ public class AppLoader implements ApplicationListener {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// measurement value
|
// measurement value
|
||||||
double _kalman_zed_k = Gdx.graphics.getRawDeltaTime();
|
double _kalman_zed_k = observation;
|
||||||
|
|
||||||
if (_kalman_zed_k <= _KALMAN_UPDATE_THRE) {
|
if (_kalman_zed_k <= _KALMAN_UPDATE_THRE) {
|
||||||
// time update
|
// time update
|
||||||
@@ -314,19 +316,10 @@ public class AppLoader implements ApplicationListener {
|
|||||||
|
|
||||||
|
|
||||||
// below is the averaging part
|
// below is the averaging part
|
||||||
if (Gdx.graphics.getRawDeltaTime() <= _KALMAN_UPDATE_THRE) {
|
if (observation <= _KALMAN_UPDATE_THRE) {
|
||||||
deltaHistory.add(((double) Gdx.graphics.getRawDeltaTime()));
|
deltaHistory.add(observation);
|
||||||
ArrayList<Double> middleVals = new ArrayList<>();
|
double deltaSum = deltaHistory.fold(0.0, (acc, val) -> { return acc += val; });
|
||||||
deltaHistory.forEach((it) -> {
|
deltaAvr = deltaSum / deltaHistory.getElemCount();
|
||||||
middleVals.add(it);
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
for (int k = deltaHistory.getElemCount() - 2; k >= 0; k--) {
|
|
||||||
for (int l = 0; l <= k; l++) {
|
|
||||||
middleVals.set(l, (middleVals.get(l) + middleVals.get(l + 1)) / 2.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
deltaAvr = middleVals.get(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -359,9 +359,9 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
|
|||||||
override fun update(delta: Float) {
|
override fun update(delta: Float) {
|
||||||
if (isUpdate && !flagDespawn) {
|
if (isUpdate && !flagDespawn) {
|
||||||
|
|
||||||
val ddelta = Gdx.graphics.rawDeltaTime.toDouble()
|
//val ddelta = Gdx.graphics.rawDeltaTime.toDouble()
|
||||||
//val ddelta = AppLoader.getSmoothDelta()
|
val ddelta = AppLoader.getSmoothDelta()
|
||||||
//println("${Gdx.graphics.rawDeltaTime.toDouble()}\t${AppLoader.getSmoothDelta()}")
|
println("${Gdx.graphics.rawDeltaTime.toDouble()}\t${AppLoader.getSmoothDelta()}")
|
||||||
|
|
||||||
|
|
||||||
if (!assertPrinted) assertInit()
|
if (!assertPrinted) assertInit()
|
||||||
|
|||||||
Reference in New Issue
Block a user