instead of dealing with delta, we'll just update multiple times, THIS TIME IN CORRECT WAY

(because it really works :p)
This commit is contained in:
minjaesong
2019-01-22 02:44:05 +09:00
parent 5260dc437c
commit 6d0616a7bd
12 changed files with 111 additions and 153 deletions

View File

@@ -207,6 +207,8 @@ public class AppLoader implements ApplicationListener {
Gdx.gl20.glViewport(0, 0, width, height); Gdx.gl20.glViewport(0, 0, width, height);
} }
public static final double UPDATE_RATE = 1.0 / 61.0; // TODO set it like 1/100, because apparent framerate is limited by update rate
private float loadTimer = 0f; private float loadTimer = 0f;
private final float showupTime = 100f / 1000f; private final float showupTime = 100f / 1000f;
@@ -235,7 +237,7 @@ public class AppLoader implements ApplicationListener {
updateFullscreenQuad(appConfig.width, appConfig.height); updateFullscreenQuad(appConfig.width, appConfig.height);
} }
private static double _kalman_xhat_k = 1.0 / 60.0; private static double _kalman_xhat_k = UPDATE_RATE;
private static double _kalman_return_value = _kalman_xhat_k; private static double _kalman_return_value = _kalman_xhat_k;
private static double _kalman_p_k = 1.0; private static double _kalman_p_k = 1.0;
private static final double _kalman_R = 0.2; // 0.2: empirical value private static final double _kalman_R = 0.2; // 0.2: empirical value
@@ -254,7 +256,7 @@ public class AppLoader implements ApplicationListener {
} }
public static void resetDeltaSmoothingHistory() { public static void resetDeltaSmoothingHistory() {
_kalman_xhat_k = 1.0 / 60.0; _kalman_xhat_k = UPDATE_RATE;
_kalman_p_k = 1.0; _kalman_p_k = 1.0;
} }
@@ -365,7 +367,7 @@ public class AppLoader implements ApplicationListener {
} }
// draw the screen // draw the screen
else { else {
screen.render(((float) getSmoothDelta())); screen.render((float) UPDATE_RATE);
} }
// nested FBOs are just not a thing in GL! // nested FBOs are just not a thing in GL!

View File

@@ -74,22 +74,12 @@ object Terrarum : Screen {
get() = HEIGHT.ushr(1) get() = HEIGHT.ushr(1)
/** /**
* To be used with physics simulator * To be used with physics simulator. This is a magic number.
*/ */
val PHYS_TIME_FRAME: Double = 26.0 + (2.0 / 3.0) val PHYS_TIME_FRAME: Double = 26.0 + (2.0 / 3.0)
val PHYS_CONST_MULT: Double = 60.0 / (26.0 + (2.0 / 3.0))
val PHYS_REF_FPS: Double = 60.0
// 26.0 + (2.0 / 3.0) // lower value == faster gravity response (IT WON'T HOTSWAP!!) // 26.0 + (2.0 / 3.0) // lower value == faster gravity response (IT WON'T HOTSWAP!!)
// protip: using METER, game unit and SI unit will have same number // protip: using METER, game unit and SI unit will have same number
/**
* To be used with render, to achieve smooth frame drawing
* TARGET_INTERNAL_FPS > PHYS_TIME_FRAME for smooth frame drawing
*/
val TARGET_INTERNAL_FPS: Double = 60.0
var previousScreen: Screen? = null // to be used with temporary states like StateMonitorCheck var previousScreen: Screen? = null // to be used with temporary states like StateMonitorCheck
@@ -483,13 +473,9 @@ object Terrarum : Screen {
get() = Gdx.input.x get() = Gdx.input.x
inline val mouseScreenY: Int inline val mouseScreenY: Int
get() = Gdx.input.y get() = Gdx.input.y
/** Bigger than 1.0 */ /** Delta converted as it it was a FPS */
inline val updateRate: Double inline val updateRate: Double
get() = 1.0 / AppLoader.getSmoothDelta() get() = 1.0 / AppLoader.getSmoothDelta()
/** Smaller than 1.0 */
val renderRate = 1.0 / TARGET_INTERNAL_FPS
val renderRateStr = TARGET_INTERNAL_FPS.toString()
/** /**
* Usage: * Usage:
* *

View File

@@ -199,23 +199,11 @@ class TitleScreen(val batch: SpriteBatch) : Screen {
private val introUncoverTime: Second = 0.3f private val introUncoverTime: Second = 0.3f
private var introUncoverDeltaCounter = 0f private var introUncoverDeltaCounter = 0f
private var updateDeltaCounter = 0.0 private var updateDeltaCounter = 0.0
protected val renderRate = Terrarum.renderRate
override fun render(delta: Float) { override fun render(delta: Float) {
// async update // TODO async update
updateDeltaCounter += delta
if (delta < 1f / 10f) { // discard async if measured FPS <= 10
var updateTries = 0
while (updateDeltaCounter >= renderRate && updateTries < 6) {
updateScreen(delta)
updateDeltaCounter -= renderRate
updateTries++
}
}
else {
updateScreen(delta)
}
updateScreen(delta)
// render? just do it anyway // render? just do it anyway
renderScreen() renderScreen()
} }

View File

@@ -95,7 +95,7 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
) )
/** /**
* TODO Pixels per 1/60 seconds. * Unit: Pixels per 1/60 (or AppLoader.UPDATE_RATE) seconds.
* *
* When the engine resolves this value, the framerate must be accounted for. E.g.: * When the engine resolves this value, the framerate must be accounted for. E.g.:
* 3.0 is resolved as 3.0 if FPS is 60, but the same value should be resolved as 6.0 if FPS is 30. * 3.0 is resolved as 3.0 if FPS is 60, but the same value should be resolved as 6.0 if FPS is 30.
@@ -109,15 +109,21 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
* Acceleration: used in code like: * Acceleration: used in code like:
* veloY += 3.0 * veloY += 3.0
* +3.0 is acceleration. You __accumulate__ acceleration to the velocity. * +3.0 is acceleration. You __accumulate__ acceleration to the velocity.
*
* V for Velocity!
*/ */
internal val externalForce = Vector2(0.0, 0.0) internal val externalV = Vector2(0.0, 0.0)
@Transient private val VELO_HARD_LIMIT = 100.0 @Transient private val VELO_HARD_LIMIT = 100.0
/** /**
* Unit: Pixels per 1/60 (or AppLoader.UPDATE_RATE) seconds.
*
* for "Controllable" actors * for "Controllable" actors
*
* V for Velocity!
*/ */
var controllerMoveDelta: Vector2? = if (this is Controllable) Vector2() else null var controllerV: Vector2? = if (this is Controllable) Vector2() else null
// not sure we need this... // not sure we need this...
//var jumpable = true // this is kind of like "semaphore" //var jumpable = true // this is kind of like "semaphore"
@@ -348,23 +354,17 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
* @param acc : Acceleration in Vector2 * @param acc : Acceleration in Vector2
*/ */
fun applyForce(acc: Vector2) { fun applyForce(acc: Vector2) {
externalForce += acc * speedMultByTile externalV += acc * speedMultByTile
} }
private val bounceDampenVelThreshold = 0.5 private val bounceDampenVelThreshold = 0.5
override fun update(fdelta: Float) { override fun update(delta: Float) {
if (isUpdate && !flagDespawn) { if (isUpdate && !flagDespawn) {
//val delta = Gdx.graphics.rawDeltaTime.toDouble()
val delta = AppLoader.getSmoothDelta()
//println("${Gdx.graphics.rawDeltaTime.toDouble()}\t${AppLoader.getSmoothDelta()}")
if (!assertPrinted) assertInit() if (!assertPrinted) assertInit()
if (sprite != null) sprite!!.update(fdelta) if (sprite != null) sprite!!.update(delta)
if (spriteGlow != null) spriteGlow!!.update(fdelta) if (spriteGlow != null) spriteGlow!!.update(delta)
// make NoClip work for player // make NoClip work for player
if (true) {//this == Terrarum.ingame!!.actorNowPlaying) { if (true) {//this == Terrarum.ingame!!.actorNowPlaying) {
@@ -381,7 +381,7 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
// Codes that modifies velocity (moveDelta and externalForce) // // Codes that modifies velocity (moveDelta and externalV) //
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
// --> Apply more forces <-- // // --> Apply more forces <-- //
@@ -396,8 +396,8 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
} }
// hard limit velocity // hard limit velocity
externalForce.x = externalForce.x.bipolarClamp(VELO_HARD_LIMIT) // displaceHitbox SHOULD use moveDelta externalV.x = externalV.x.bipolarClamp(VELO_HARD_LIMIT) // displaceHitbox SHOULD use moveDelta
externalForce.y = externalForce.y.bipolarClamp(VELO_HARD_LIMIT) externalV.y = externalV.y.bipolarClamp(VELO_HARD_LIMIT)
if (!isChronostasis) { if (!isChronostasis) {
/////////////////////////////////////////////////// ///////////////////////////////////////////////////
@@ -410,11 +410,11 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
* This body is NON-STATIC and the other body is STATIC * This body is NON-STATIC and the other body is STATIC
*/ */
if (!isNoCollideWorld) { if (!isNoCollideWorld) {
displaceHitbox(delta) displaceHitbox(delta.toDouble())
} }
else { else {
val vecSum = externalForce + (controllerMoveDelta ?: Vector2(0.0, 0.0)) val vecSum = externalV + (controllerV ?: Vector2(0.0, 0.0))
hitbox.translate(vecSum * (Terrarum.PHYS_REF_FPS * delta)) hitbox.translate(vecSum)
} }
////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////
@@ -428,7 +428,7 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
// TODO less friction for non-animating objects (make items glide far more on ice) // TODO less friction for non-animating objects (make items glide far more on ice)
// FIXME asymmetry on friction // FIXME asymmetry on friction
setHorizontalFriction(delta) // friction SHOULD use and alter externalForce setHorizontalFriction(delta) // friction SHOULD use and alter externalV
//if (isNoClip) { // TODO also hanging on the rope, etc. //if (isNoClip) { // TODO also hanging on the rope, etc.
setVerticalFriction(delta) setVerticalFriction(delta)
//} //}
@@ -470,33 +470,33 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
if (!(isWalled(hitbox, COLLIDING_LEFT) && walkX < 0) if (!(isWalled(hitbox, COLLIDING_LEFT) && walkX < 0)
|| !(isWalled(hitbox, COLLIDING_RIGHT) && walkX > 0) || !(isWalled(hitbox, COLLIDING_RIGHT) && walkX > 0)
) { ) {
moveDelta.x = externalForce.x + walkX moveDelta.x = externalV.x + walkX
} }
// decide whether to ignore walkY // decide whether to ignore walkY
if (!(isWalled(hitbox, COLLIDING_TOP) && walkY < 0) if (!(isWalled(hitbox, COLLIDING_TOP) && walkY < 0)
|| !(isWalled(hitbox, COLLIDING_BOTTOM) && walkY > 0) || !(isWalled(hitbox, COLLIDING_BOTTOM) && walkY > 0)
) { ) {
moveDelta.y = externalForce.y + walkY moveDelta.y = externalV.y + walkY
} }
} }
else { else {
if (!isWalled(hitbox, COLLIDING_LEFT) if (!isWalled(hitbox, COLLIDING_LEFT)
|| !isWalled(hitbox, COLLIDING_RIGHT) || !isWalled(hitbox, COLLIDING_RIGHT)
) { ) {
moveDelta.x = externalForce.x moveDelta.x = externalV.x
} }
// decide whether to ignore walkY // decide whether to ignore walkY
if (!isWalled(hitbox, COLLIDING_TOP) if (!isWalled(hitbox, COLLIDING_TOP)
|| !isWalled(hitbox, COLLIDING_BOTTOM) || !isWalled(hitbox, COLLIDING_BOTTOM)
) { ) {
moveDelta.y = externalForce.y moveDelta.y = externalV.y
} }
} }
}*/ }*/
fun getDrag(delta: Double, externalForce: Vector2): Vector2 { fun getDrag(delta: Float, externalForce: Vector2): Vector2 {
/** /**
* weight; gravitational force in action * weight; gravitational force in action
* W = mass * G (9.8 [m/s^2]) * W = mass * G (9.8 [m/s^2])
@@ -514,7 +514,7 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
val V: Vector2 = (W - D) / Terrarum.PHYS_TIME_FRAME * SI_TO_GAME_ACC val V: Vector2 = (W - D) / Terrarum.PHYS_TIME_FRAME * SI_TO_GAME_ACC
return V * (Terrarum.PHYS_REF_FPS * delta).sqrt() return V
// FIXME v * const, where const = 1.0 for FPS=60, sqrt(2.0) for FPS=30, etc. // FIXME v * const, where const = 1.0 for FPS=60, sqrt(2.0) for FPS=30, etc.
// this is "close enough" solution and not perfect. // this is "close enough" solution and not perfect.
@@ -535,11 +535,11 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
* *
* Apply only if not grounded; normal force is precessed separately. * Apply only if not grounded; normal force is precessed separately.
*/ */
private fun applyGravitation(delta: Double) { private fun applyGravitation(delta: Float) {
if (!isNoSubjectToGrav && !(gravitation.y > 0 && walledBottom || gravitation.y < 0 && walledTop)) { if (!isNoSubjectToGrav && !(gravitation.y > 0 && walledBottom || gravitation.y < 0 && walledTop)) {
//if (!isWalled(hitbox, COLLIDING_BOTTOM)) { //if (!isWalled(hitbox, COLLIDING_BOTTOM)) {
applyForce(getDrag(delta, externalForce)) applyForce(getDrag(delta, externalV))
//} //}
} }
} }
@@ -587,7 +587,7 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
// if not touching: // if not touching:
// do nothing // do nothing
// [Friction]: // [Friction]:
// deform vector "externalForce" // deform vector "externalV"
// if isControllable: // if isControllable:
// also alter walkX/Y // also alter walkX/Y
// translate ((nextHitbox)) hitbox by moveDelta (forces), this consumes force // translate ((nextHitbox)) hitbox by moveDelta (forces), this consumes force
@@ -637,7 +637,7 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
fun Double.modTileDelta() = this - this.modTile() fun Double.modTileDelta() = this - this.modTile()
val vectorSum = (externalForce + controllerMoveDelta) * (Terrarum.PHYS_REF_FPS * delta) val vectorSum = (externalV + controllerV)
val ccdSteps = minOf(16, (vectorSum.magnitudeSquared / TILE_SIZE.sqr()).floorInt() + 1) // adaptive val ccdSteps = minOf(16, (vectorSum.magnitudeSquared / TILE_SIZE.sqr()).floorInt() + 1) // adaptive
@@ -838,20 +838,20 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
// bounce X/Y // bounce X/Y
if (bounceX) { if (bounceX) {
externalForce.x *= elasticity externalV.x *= elasticity
controllerMoveDelta?.let { controllerMoveDelta!!.x *= elasticity } controllerV?.let { controllerV!!.x *= elasticity }
} }
if (bounceY) { if (bounceY) {
externalForce.y *= elasticity externalV.y *= elasticity
controllerMoveDelta?.let { controllerMoveDelta!!.y *= elasticity } controllerV?.let { controllerV!!.y *= elasticity }
} }
if (zeroX) { if (zeroX) {
externalForce.x = 0.0 externalV.x = 0.0
controllerMoveDelta?.let { controllerMoveDelta!!.x = 0.0 } controllerV?.let { controllerV!!.x = 0.0 }
} }
if (zeroY) { if (zeroY) {
externalForce.y = 0.0 externalV.y = 0.0
controllerMoveDelta?.let { controllerMoveDelta!!.y = 0.0 } controllerV?.let { controllerV!!.y = 0.0 }
} }
@@ -1012,11 +1012,11 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
(BlockCodex[tile].isSolid) || (BlockCodex[tile].isSolid) ||
// platforms, moving downward AND not "going down" // platforms, moving downward AND not "going down"
(this is ActorHumanoid && BlockCodex[tile].isPlatform && (this is ActorHumanoid && BlockCodex[tile].isPlatform &&
externalForce.y + (controllerMoveDelta?.y ?: 0.0) >= 0.0 && externalV.y + (controllerV?.y ?: 0.0) >= 0.0 &&
!this.isDownDown && this.axisY <= 0f) || !this.isDownDown && this.axisY <= 0f) ||
// platforms, moving downward // platforms, moving downward
(this !is ActorHumanoid && BlockCodex[tile].isPlatform && (this !is ActorHumanoid && BlockCodex[tile].isPlatform &&
externalForce.y + (controllerMoveDelta?.y ?: 0.0) >= 0.0) externalV.y + (controllerV?.y ?: 0.0) >= 0.0)
// TODO: as for the platform, only apply it when it's a feet tile // TODO: as for the platform, only apply it when it's a feet tile
@@ -1071,7 +1071,7 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
/** about stopping /** about stopping
* for about get moving, see updateMovementControl */ * for about get moving, see updateMovementControl */
private fun setHorizontalFriction(delta: Double) { private fun setHorizontalFriction(delta: Float) {
val friction = if (isNoClip) val friction = if (isNoClip)
BASE_FRICTION * BlockCodex[Block.STONE].friction.frictionToMult() BASE_FRICTION * BlockCodex[Block.STONE].friction.frictionToMult()
else { else {
@@ -1079,50 +1079,50 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
BASE_FRICTION * if (grounded) feetFriction else bodyFriction BASE_FRICTION * if (grounded) feetFriction else bodyFriction
} }
if (externalForce.x < 0) { if (externalV.x < 0) {
externalForce.x += friction externalV.x += friction
if (externalForce.x > 0) externalForce.x = 0.0 // compensate overshoot if (externalV.x > 0) externalV.x = 0.0 // compensate overshoot
} }
else if (externalForce.x > 0) { else if (externalV.x > 0) {
externalForce.x -= friction externalV.x -= friction
if (externalForce.x < 0) externalForce.x = 0.0 // compensate overshoot if (externalV.x < 0) externalV.x = 0.0 // compensate overshoot
} }
if (this is Controllable) { if (this is Controllable) {
if (controllerMoveDelta!!.x < 0) { if (controllerV!!.x < 0) {
controllerMoveDelta!!.x += friction controllerV!!.x += friction
if (controllerMoveDelta!!.x > 0) controllerMoveDelta!!.x = 0.0 if (controllerV!!.x > 0) controllerV!!.x = 0.0
} }
else if (controllerMoveDelta!!.x > 0) { else if (controllerV!!.x > 0) {
controllerMoveDelta!!.x -= friction controllerV!!.x -= friction
if (controllerMoveDelta!!.x < 0) controllerMoveDelta!!.x = 0.0 if (controllerV!!.x < 0) controllerV!!.x = 0.0
} }
} }
} }
private fun setVerticalFriction(delta: Double) { private fun setVerticalFriction(delta: Float) {
val friction = if (isNoClip) val friction = if (isNoClip)
BASE_FRICTION * BlockCodex[Block.STONE].friction.frictionToMult() BASE_FRICTION * BlockCodex[Block.STONE].friction.frictionToMult()
else else
BASE_FRICTION * bodyFriction BASE_FRICTION * bodyFriction
if (externalForce.y < 0) { if (externalV.y < 0) {
externalForce.y += friction externalV.y += friction
if (externalForce.y > 0) externalForce.y = 0.0 // compensate overshoot if (externalV.y > 0) externalV.y = 0.0 // compensate overshoot
} }
else if (externalForce.y > 0) { else if (externalV.y > 0) {
externalForce.y -= friction externalV.y -= friction
if (externalForce.y < 0) externalForce.y = 0.0 // compensate overshoot if (externalV.y < 0) externalV.y = 0.0 // compensate overshoot
} }
if (this is Controllable) { if (this is Controllable) {
if (controllerMoveDelta!!.y < 0) { if (controllerV!!.y < 0) {
controllerMoveDelta!!.y += friction controllerV!!.y += friction
if (controllerMoveDelta!!.y > 0) controllerMoveDelta!!.y = 0.0 if (controllerV!!.y > 0) controllerV!!.y = 0.0
} }
else if (controllerMoveDelta!!.y > 0) { else if (controllerV!!.y > 0) {
controllerMoveDelta!!.y -= friction controllerV!!.y -= friction
if (controllerMoveDelta!!.y < 0) controllerMoveDelta!!.y = 0.0 if (controllerV!!.y < 0) controllerV!!.y = 0.0
} }
} }
} }
@@ -1443,8 +1443,8 @@ open class ActorWBMovable(renderOrder: RenderOrder, val immobileBody: Boolean =
field = value field = value
if (value) { if (value) {
externalForce.zero() externalV.zero()
controllerMoveDelta?.zero() controllerV?.zero()
} }
} }

View File

@@ -46,7 +46,7 @@ import org.luaj.vm2.*
*/ */
fun composeActorObject(actor: ActorWBMovable): LuaTable { fun composeActorObject(actor: ActorWBMovable): LuaTable {
val t: LuaTable = LuaTable() val t: LuaTable = LuaTable()
val moveDelta = actor.externalForce + actor.controllerMoveDelta val moveDelta = actor.externalV + actor.controllerV
t["name"] = actor.actorValue.getAsString(AVKey.NAME).toLua() t["name"] = actor.actorValue.getAsString(AVKey.NAME).toLua()
t["startX"] = actor.hitbox.centeredX.toLua() t["startX"] = actor.hitbox.centeredX.toLua()

View File

@@ -120,9 +120,6 @@ class BuildingMaker(batch: SpriteBatch) : IngameInstance(batch) {
} }
} }
protected var updateDeltaCounter = 0.0
protected val updateRate = 1.0 / Terrarum.TARGET_INTERNAL_FPS
private val actorsRenderOverlay = ArrayList<ActorWithBody>() private val actorsRenderOverlay = ArrayList<ActorWithBody>()
init { init {
@@ -161,19 +158,8 @@ class BuildingMaker(batch: SpriteBatch) : IngameInstance(batch) {
// ASYNCHRONOUS UPDATE AND RENDER // // ASYNCHRONOUS UPDATE AND RENDER //
// async update // TODO async update
updateDeltaCounter += delta updateGame(delta)
if (delta < 1f / 10f) { // discard async if measured FPS <= 10
var updateTries = 0
while (updateDeltaCounter >= updateRate) {
updateGame(delta)
updateDeltaCounter -= updateRate
updateTries++
}
}
else {
updateGame(delta)
}
// render? just do it anyway // render? just do it anyway
renderGame() renderGame()

View File

@@ -82,7 +82,7 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
fun getCanonicalTitle() = AppLoader.GAME_NAME + fun getCanonicalTitle() = AppLoader.GAME_NAME +
" — F: ${Gdx.graphics.framesPerSecond}" + " — F: ${Gdx.graphics.framesPerSecond}" +
if (AppLoader.IS_DEVELOPMENT_BUILD) if (AppLoader.IS_DEVELOPMENT_BUILD)
"t${Terrarum.updateRateStr} / RT${Terrarum.renderRateStr})" + "F${Terrarum.updateRateStr})" +
" — M: J${Terrarum.memJavaHeap}M / N${Terrarum.memNativeHeap}M / X${Terrarum.memXmx}M" " — M: J${Terrarum.memJavaHeap}M / N${Terrarum.memNativeHeap}M / X${Terrarum.memXmx}M"
else else
"" ""
@@ -379,6 +379,7 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
LightmapRenderer.fireRecalculateEvent() LightmapRenderer.fireRecalculateEvent()
AppLoader.debugTimers["Ingame.updateCounter"] = 0
@@ -408,8 +409,6 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
itemOnGrip?.endSecondaryUse(delta) itemOnGrip?.endSecondaryUse(delta)
} }
protected val renderRate = Terrarum.renderRate
private var firstTimeRun = true private var firstTimeRun = true
/////////////// ///////////////
@@ -422,6 +421,7 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
} }
private var countdownToDeltaReset = 15 // number of frames private var countdownToDeltaReset = 15 // number of frames
private var updateAkku = 0.0
override fun render(delta: Float) { override fun render(delta: Float) {
// Q&D solution for LoadScreen and Ingame, where while LoadScreen is working, Ingame now no longer has GL Context // Q&D solution for LoadScreen and Ingame, where while LoadScreen is working, Ingame now no longer has GL Context
@@ -443,7 +443,7 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
} }
if (countdownToDeltaReset >= 0) {3 if (countdownToDeltaReset >= 0) {
if (countdownToDeltaReset == 0) { if (countdownToDeltaReset == 0) {
AppLoader.resetDeltaSmoothingHistory() AppLoader.resetDeltaSmoothingHistory()
} }
@@ -456,20 +456,15 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
/** UPDATE CODE GOES HERE */ /** UPDATE CODE GOES HERE */
updateAkku += AppLoader.getSmoothDelta()
var i = 0L
if (false && AppLoader.getConfigBoolean("multithread")) { // NO MULTITHREADING: camera don't like concurrent modification (jittery actor movements) while (updateAkku >= delta) {
if (firstTimeRun || updateThreadWrapper.state == Thread.State.TERMINATED) {
updateThreadWrapper = Thread(ingameUpdateThread, "Terrarum UpdateThread")
updateThreadWrapper.start()
if (firstTimeRun) firstTimeRun = false
}
// else, NOP;
}
else {
AppLoader.debugTimers["Ingame.update"] = measureNanoTime { updateGame(delta) } AppLoader.debugTimers["Ingame.update"] = measureNanoTime { updateGame(delta) }
updateAkku -= delta
i += 1
} }
AppLoader.debugTimers["Ingame.updateCounter"] = i
/** RENDER CODE GOES HERE */ /** RENDER CODE GOES HERE */
@@ -543,6 +538,8 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
private fun renderGame() { private fun renderGame() {
Gdx.graphics.setTitle(getCanonicalTitle()) Gdx.graphics.setTitle(getCanonicalTitle())
IngameRenderer.invoke( IngameRenderer.invoke(
world as GameWorldExtension, world as GameWorldExtension,
actorsRenderBehind, actorsRenderBehind,

View File

@@ -406,9 +406,9 @@ open class ActorHumanoid(
avAcceleration * applyVelo(walkCounterX) * (if (left) -1f else 1f) * absAxisVal avAcceleration * applyVelo(walkCounterX) * (if (left) -1f else 1f) * absAxisVal
if (absAxisVal != AXIS_KEYBOARD) if (absAxisVal != AXIS_KEYBOARD)
controllerMoveDelta?.x?.let { controllerMoveDelta!!.x = controllerMoveDelta!!.x.plus(readonly_totalX).bipolarClamp(avSpeedCap * absAxisVal) } controllerV?.x?.let { controllerV!!.x = controllerV!!.x.plus(readonly_totalX).bipolarClamp(avSpeedCap * absAxisVal) }
else else
controllerMoveDelta?.x?.let { controllerMoveDelta!!.x = controllerMoveDelta!!.x.plus(readonly_totalX).bipolarClamp(avSpeedCap) } controllerV?.x?.let { controllerV!!.x = controllerV!!.x.plus(readonly_totalX).bipolarClamp(avSpeedCap) }
if (walkCounterX < 1000000) { if (walkCounterX < 1000000) {
walkCounterX += 1 walkCounterX += 1
@@ -444,9 +444,9 @@ open class ActorHumanoid(
avAcceleration * applyVelo(walkCounterY) * (if (up) -1f else 1f) * absAxisVal avAcceleration * applyVelo(walkCounterY) * (if (up) -1f else 1f) * absAxisVal
if (absAxisVal != AXIS_KEYBOARD) if (absAxisVal != AXIS_KEYBOARD)
controllerMoveDelta?.y?.let { controllerMoveDelta!!.y = controllerMoveDelta!!.y.plus(readonly_totalY).bipolarClamp(avSpeedCap * absAxisVal) } controllerV?.y?.let { controllerV!!.y = controllerV!!.y.plus(readonly_totalY).bipolarClamp(avSpeedCap * absAxisVal) }
else else
controllerMoveDelta?.y?.let { controllerMoveDelta!!.y = controllerMoveDelta!!.y.plus(readonly_totalY).bipolarClamp(avSpeedCap) } controllerV?.y?.let { controllerV!!.y = controllerV!!.y.plus(readonly_totalY).bipolarClamp(avSpeedCap) }
if (walkCounterY < 1000000) { if (walkCounterY < 1000000) {
walkCounterY += 1 walkCounterY += 1
@@ -489,6 +489,7 @@ open class ActorHumanoid(
private var oldJUMPPOWERBUFF = -1.0 // init private var oldJUMPPOWERBUFF = -1.0 // init
private var oldScale = -1.0 private var oldScale = -1.0
private var oldDragCoefficient = -1.0 private var oldDragCoefficient = -1.0
// used by some AIs
var jumpAirTime: Double = -1.0 var jumpAirTime: Double = -1.0
get() { get() {
// compare all the affecting variables // compare all the affecting variables
@@ -519,7 +520,7 @@ open class ActorHumanoid(
val timedJumpCharge = jumpFunc(MAX_JUMP_LENGTH, jmpCtr) val timedJumpCharge = jumpFunc(MAX_JUMP_LENGTH, jmpCtr)
forceVec.y -= getJumpAcc(jumpPower, timedJumpCharge) forceVec.y -= getJumpAcc(jumpPower, timedJumpCharge)
forceVec.y += getDrag(1.0 / Terrarum.PHYS_REF_FPS, forceVec).y forceVec.y += getDrag(AppLoader.UPDATE_RATE.toFloat(), forceVec).y
simYPos += forceVec.y // ignoring all the fluid drag OTHER THAN THE AIR simYPos += forceVec.y // ignoring all the fluid drag OTHER THAN THE AIR
@@ -564,7 +565,7 @@ open class ActorHumanoid(
jumpAcc = getJumpAcc(jumpPower, timedJumpCharge) jumpAcc = getJumpAcc(jumpPower, timedJumpCharge)
controllerMoveDelta?.y?.let { controllerMoveDelta!!.y -= jumpAcc } // feed negative value to the vector controllerV?.y?.let { controllerV!!.y -= jumpAcc } // feed negative value to the vector
// do not think of resetting this to zero when counter hit the ceiling; that's HOW NOT // do not think of resetting this to zero when counter hit the ceiling; that's HOW NOT
// newtonian physics work, stupid myself :( // newtonian physics work, stupid myself :(
@@ -609,7 +610,7 @@ open class ActorHumanoid(
sprite?.update(delta) sprite?.update(delta)
spriteGlow?.update(delta) spriteGlow?.update(delta)
if (walledBottom && controllerMoveDelta?.x != 0.0) { if (walledBottom && controllerV?.x != 0.0) {
//switch row //switch row
sprite?.switchRow(SPRITE_ROW_WALK) sprite?.switchRow(SPRITE_ROW_WALK)
spriteGlow?.switchRow(SPRITE_ROW_WALK) spriteGlow?.switchRow(SPRITE_ROW_WALK)
@@ -617,8 +618,8 @@ open class ActorHumanoid(
// set anim frame delay // set anim frame delay
// 4f of the divider is a magic number, empirically decided // 4f of the divider is a magic number, empirically decided
if (this is HasAssembledSprite) { if (this is HasAssembledSprite) {
sprite?.delays?.set(SPRITE_ROW_WALK, scale.sqrt().toFloat() / (4f * (controllerMoveDelta?.x ?: 0.0001).abs().toFloat())) // FIXME empirical value sprite?.delays?.set(SPRITE_ROW_WALK, scale.sqrt().toFloat() / (4f * (controllerV?.x ?: 0.0001).abs().toFloat())) // FIXME empirical value
spriteGlow?.delays?.set(SPRITE_ROW_WALK, scale.sqrt().toFloat() / (4f * (controllerMoveDelta?.x ?: 0.0001).abs().toFloat())) // FIXME empirical value spriteGlow?.delays?.set(SPRITE_ROW_WALK, scale.sqrt().toFloat() / (4f * (controllerV?.x ?: 0.0001).abs().toFloat())) // FIXME empirical value
} }

View File

@@ -10,7 +10,6 @@ import net.torvald.terrarum.gameactors.AVKey
import net.torvald.terrarum.gameactors.ActorWBMovable import net.torvald.terrarum.gameactors.ActorWBMovable
import net.torvald.terrarum.gameactors.Controllable import net.torvald.terrarum.gameactors.Controllable
import net.torvald.terrarum.gameactors.Hitbox import net.torvald.terrarum.gameactors.Hitbox
import net.torvald.terrarum.gameworld.GameWorld
/** /**
* Created by minjaesong on 2018-01-17. * Created by minjaesong on 2018-01-17.
@@ -37,7 +36,7 @@ class PhysTestLuarLander : ActorWBMovable(RenderOrder.MIDTOP), Controllable {
super.update(delta) super.update(delta)
if (Gdx.input.isKeyPressed(Input.Keys.UP)) { if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
controllerMoveDelta!!.y = avSpeedCap controllerV!!.y = avSpeedCap
} }
} }

View File

@@ -9,7 +9,6 @@ import net.torvald.terrarum.blockproperties.BlockCodex
import net.torvald.terrarum.gameactors.ActorWBMovable import net.torvald.terrarum.gameactors.ActorWBMovable
import net.torvald.terrarum.gameactors.Hitbox import net.torvald.terrarum.gameactors.Hitbox
import net.torvald.terrarum.gameactors.Luminous import net.torvald.terrarum.gameactors.Luminous
import net.torvald.terrarum.gameworld.GameWorld
import org.dyn4j.geometry.Vector2 import org.dyn4j.geometry.Vector2
import java.util.* import java.util.*
@@ -54,7 +53,7 @@ open class ProjectileSimple(
posPre = Point2d(fromPoint.x, fromPoint.y) posPre = Point2d(fromPoint.x, fromPoint.y)
// lightbox sized 8x8 centered to the bullet // lightbox sized 8x8 centered to the bullet
lightBoxList.add(Hitbox(-4.0, -4.0, 8.0, 8.0)) lightBoxList.add(Hitbox(-4.0, -4.0, 8.0, 8.0))
//this.externalForce.set(velocity) //this.externalV.set(velocity)
damage = bulletDatabase[type][OFFSET_DAMAGE] as Int damage = bulletDatabase[type][OFFSET_DAMAGE] as Int
displayColour = bulletDatabase[type][OFFSET_COL] as Color displayColour = bulletDatabase[type][OFFSET_COL] as Color
@@ -64,7 +63,7 @@ open class ProjectileSimple(
setHitboxDimension(2, 2, 0, 0) // should be following sprite's properties if there IS one setHitboxDimension(2, 2, 0, 0) // should be following sprite's properties if there IS one
externalForce.set((fromPoint to toPoint).setMagnitude(speed.toDouble())) externalV.set((fromPoint to toPoint).setMagnitude(speed.toDouble()))

View File

@@ -150,8 +150,8 @@ object CollisionSolver {
// if they actually makes collision (e.g. player vs ball), solve it // if they actually makes collision (e.g. player vs ball), solve it
if (a makesCollisionWith b) { if (a makesCollisionWith b) {
val a_moveDelta = a.externalForce + a.controllerMoveDelta val a_moveDelta = a.externalV + a.controllerV
val b_moveDelta = b.externalForce + b.controllerMoveDelta val b_moveDelta = b.externalV + b.controllerV
val ux_1 = a_moveDelta.x val ux_1 = a_moveDelta.x
val ux_2 = b_moveDelta.x val ux_2 = b_moveDelta.x

View File

@@ -108,11 +108,11 @@ class BasicDebugInfoWindow : UICanvas() {
+ ccG + ccG
+ "${WorldCamera.y}") + "${WorldCamera.y}")
printLine(batch, 3, "veloX reported $ccG${player.externalForce.x}") printLine(batch, 3, "veloX reported $ccG${player.externalV.x}")
printLine(batch, 4, "veloY reported $ccG${player.externalForce.y}") printLine(batch, 4, "veloY reported $ccG${player.externalV.y}")
printLine(batch, 5, "p_WalkX $ccG${player.controllerMoveDelta?.x}") printLine(batch, 5, "p_WalkX $ccG${player.controllerV?.x}")
printLine(batch, 6, "p_WalkY $ccG${player.controllerMoveDelta?.y}") printLine(batch, 6, "p_WalkY $ccG${player.controllerV?.y}")
printLineColumn(batch, 2, 3, "veloX measured $ccG${xdelta}") printLineColumn(batch, 2, 3, "veloX measured $ccG${xdelta}")
printLineColumn(batch, 2, 4, "veloY measured $ccG${ydelta}") printLineColumn(batch, 2, 4, "veloY measured $ccG${ydelta}")