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

@@ -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>()
init {
@@ -161,19 +158,8 @@ class BuildingMaker(batch: SpriteBatch) : IngameInstance(batch) {
// ASYNCHRONOUS UPDATE AND RENDER //
// async update
updateDeltaCounter += 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)
}
// TODO async update
updateGame(delta)
// render? just do it anyway
renderGame()

View File

@@ -82,7 +82,7 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
fun getCanonicalTitle() = AppLoader.GAME_NAME +
" — F: ${Gdx.graphics.framesPerSecond}" +
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"
else
""
@@ -379,6 +379,7 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
LightmapRenderer.fireRecalculateEvent()
AppLoader.debugTimers["Ingame.updateCounter"] = 0
@@ -408,8 +409,6 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
itemOnGrip?.endSecondaryUse(delta)
}
protected val renderRate = Terrarum.renderRate
private var firstTimeRun = true
///////////////
@@ -422,6 +421,7 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
}
private var countdownToDeltaReset = 15 // number of frames
private var updateAkku = 0.0
override fun render(delta: Float) {
// 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) {
AppLoader.resetDeltaSmoothingHistory()
}
@@ -456,20 +456,15 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
/** UPDATE CODE GOES HERE */
updateAkku += AppLoader.getSmoothDelta()
if (false && AppLoader.getConfigBoolean("multithread")) { // NO MULTITHREADING: camera don't like concurrent modification (jittery actor movements)
if (firstTimeRun || updateThreadWrapper.state == Thread.State.TERMINATED) {
updateThreadWrapper = Thread(ingameUpdateThread, "Terrarum UpdateThread")
updateThreadWrapper.start()
if (firstTimeRun) firstTimeRun = false
}
// else, NOP;
}
else {
var i = 0L
while (updateAkku >= delta) {
AppLoader.debugTimers["Ingame.update"] = measureNanoTime { updateGame(delta) }
updateAkku -= delta
i += 1
}
AppLoader.debugTimers["Ingame.updateCounter"] = i
/** RENDER CODE GOES HERE */
@@ -543,6 +538,8 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
private fun renderGame() {
Gdx.graphics.setTitle(getCanonicalTitle())
IngameRenderer.invoke(
world as GameWorldExtension,
actorsRenderBehind,

View File

@@ -406,9 +406,9 @@ open class ActorHumanoid(
avAcceleration * applyVelo(walkCounterX) * (if (left) -1f else 1f) * absAxisVal
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
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) {
walkCounterX += 1
@@ -444,9 +444,9 @@ open class ActorHumanoid(
avAcceleration * applyVelo(walkCounterY) * (if (up) -1f else 1f) * absAxisVal
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
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) {
walkCounterY += 1
@@ -489,6 +489,7 @@ open class ActorHumanoid(
private var oldJUMPPOWERBUFF = -1.0 // init
private var oldScale = -1.0
private var oldDragCoefficient = -1.0
// used by some AIs
var jumpAirTime: Double = -1.0
get() {
// compare all the affecting variables
@@ -519,7 +520,7 @@ open class ActorHumanoid(
val timedJumpCharge = jumpFunc(MAX_JUMP_LENGTH, jmpCtr)
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
@@ -564,7 +565,7 @@ open class ActorHumanoid(
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
// newtonian physics work, stupid myself :(
@@ -609,7 +610,7 @@ open class ActorHumanoid(
sprite?.update(delta)
spriteGlow?.update(delta)
if (walledBottom && controllerMoveDelta?.x != 0.0) {
if (walledBottom && controllerV?.x != 0.0) {
//switch row
sprite?.switchRow(SPRITE_ROW_WALK)
spriteGlow?.switchRow(SPRITE_ROW_WALK)
@@ -617,8 +618,8 @@ open class ActorHumanoid(
// set anim frame delay
// 4f of the divider is a magic number, empirically decided
if (this is HasAssembledSprite) {
sprite?.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 * (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 * (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.Controllable
import net.torvald.terrarum.gameactors.Hitbox
import net.torvald.terrarum.gameworld.GameWorld
/**
* Created by minjaesong on 2018-01-17.
@@ -37,7 +36,7 @@ class PhysTestLuarLander : ActorWBMovable(RenderOrder.MIDTOP), Controllable {
super.update(delta)
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.Hitbox
import net.torvald.terrarum.gameactors.Luminous
import net.torvald.terrarum.gameworld.GameWorld
import org.dyn4j.geometry.Vector2
import java.util.*
@@ -54,7 +53,7 @@ open class ProjectileSimple(
posPre = Point2d(fromPoint.x, fromPoint.y)
// lightbox sized 8x8 centered to the bullet
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
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
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 (a makesCollisionWith b) {
val a_moveDelta = a.externalForce + a.controllerMoveDelta
val b_moveDelta = b.externalForce + b.controllerMoveDelta
val a_moveDelta = a.externalV + a.controllerV
val b_moveDelta = b.externalV + b.controllerV
val ux_1 = a_moveDelta.x
val ux_2 = b_moveDelta.x