more accurate floating point comparison with epsilon

This commit is contained in:
minjaesong
2022-09-14 10:54:26 +09:00
parent f696672d0f
commit 17f85aa155
4 changed files with 15 additions and 10 deletions

View File

@@ -751,7 +751,7 @@ open class ActorHumanoid : ActorWithBody, Controllable, Pocketed, Factionable, L
if (this is HasAssembledSprite) {
try {
val baseDelay = animDesc!!.getAnimByFrameName("ANIM_RUN").delay
val moveSpeedMult = (controllerV?.x ?: 0.0).abs().coerceAtLeast(PHYS_EPSILON_VELO).toFloat() / 1.414f // FIXME empirical value
val moveSpeedMult = (controllerV?.x ?: 0.0).abs().coerceAtLeast(0.001).toFloat() / 1.414f // FIXME empirical value
val stride = scale.toFloat()
val maxMoveSpeed = scale.sqrt().toFloat() // ActorWithBody uses scale.sqrt() for determining walk acceleration
val scaleCompensation = stride / maxMoveSpeed

View File

@@ -12,9 +12,7 @@ import net.torvald.terrarum.gameactors.*
import net.torvald.terrarum.gameitems.mouseInInteractableRange
import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
import org.dyn4j.geometry.Vector2
import java.util.*
import kotlin.math.absoluteValue
/**
* @param width of hitbox, in tiles, when the door is opened. Default to 2. Closed door always have width of 1. (this limits how big and thick the door can be)
@@ -261,13 +259,16 @@ open class FixtureSwingingDoorBase : FixtureBase {
}
private fun ActorWithBody.movingTowardsRight(): Boolean {
return ((this.controllerV ?: Vector2()) + this.externalV).x >= PHYS_EPSILON_VELO
// return ((this.controllerV ?: Vector2()) + this.externalV).x >= PHYS_EPSILON_VELO
return (((this.controllerV?.x ?: 0.0) / this.externalV.x).let { if (it.isNaN()) 0.0 else it } - 1) >= PHYS_EPSILON_DIST
}
private fun ActorWithBody.movingTowardsLeft(): Boolean {
return ((this.controllerV ?: Vector2()) + this.externalV).x <= -PHYS_EPSILON_VELO
// return ((this.controllerV ?: Vector2()) + this.externalV).x <= -PHYS_EPSILON_VELO
return (((this.controllerV?.x ?: 0.0) / this.externalV.x).let { if (it.isNaN()) 0.0 else it } - 1) <= PHYS_EPSILON_DIST
}
private fun ActorWithBody.notMoving(): Boolean {
return ((this.controllerV ?: Vector2()) + this.externalV).x.absoluteValue < PHYS_EPSILON_VELO
// return ((this.controllerV ?: Vector2()) + this.externalV).x.absoluteValue < PHYS_EPSILON_VELO
return ActorWithBody.isCloseEnough(this.controllerV?.x ?: 0.0, this.externalV.x)
}
private var doorCloseQueueTimer = 0f