physics: add stepping

This commit is contained in:
Lukas 2021-05-01 13:31:22 +02:00
parent c804ba9acb
commit 02db2d7ba9
2 changed files with 16 additions and 4 deletions

View File

@ -227,13 +227,24 @@ abstract class Entity(
val delta = Vec3(deltaPosition) val delta = Vec3(deltaPosition)
if (delta.y != 0.0f) { if (delta.y != 0.0f) {
delta.y = collisionsToCheck.computeOffset(aabb, deltaPosition.y, Axes.Y) delta.y = collisionsToCheck.computeOffset(aabb, deltaPosition.y, Axes.Y)
aabb.offsetAssign(0f, delta.y, 0f)
if (delta.y != deltaPosition.y) { if (delta.y != deltaPosition.y) {
onGround = false onGround = false
velocity.y = 0.0f velocity.y = 0.0f
if (deltaPosition.y < 0) { if (deltaPosition.y < 0) {
onGround = true onGround = true
} }
aabb.offsetAssign(0f, delta.y, 0f)
} else if (delta.y < 0) {
onGround = false
}
}
if ((deltaPosition.x != 0f || deltaPosition.z != 0f)) {
val testDelta = Vec3(delta)
testDelta.y = STEP_HEIGHT
val stepMovementY = collisionsToCheck.computeOffset(aabb + testDelta, -STEP_HEIGHT, Axes.Y)
if (stepMovementY < 0 && stepMovementY >= -STEP_HEIGHT) {
delta.y = STEP_HEIGHT + stepMovementY
aabb.offsetAssign(0f, delta.y, 0f)
} }
} }
val xPriority = delta.x > delta.z val xPriority = delta.x > delta.z
@ -264,7 +275,7 @@ abstract class Entity(
val newVelocity = Vec3(velocity) val newVelocity = Vec3(velocity)
val oldVelocity = Vec3(velocity) val oldVelocity = Vec3(velocity)
val deltaTime = deltaMillis.toFloat() / 1000.0f val deltaTime = deltaMillis.toFloat() / 1000.0f
if (!hasNoGravity && !isFlying) { if (! hasNoGravity && !isFlying) {
newVelocity.y -= ProtocolDefinition.GRAVITY * deltaTime newVelocity.y -= ProtocolDefinition.GRAVITY * deltaTime
} }
newVelocity *= 0.25f.pow(deltaTime) // apply newVelocity *= 0.25f.pow(deltaTime) // apply
@ -284,6 +295,7 @@ abstract class Entity(
get() = defaultAABB + position get() = defaultAABB + position
companion object { companion object {
private const val HITBOX_MARGIN = 1e-5 private const val HITBOX_MARGIN = 1e-5f
private const val STEP_HEIGHT = 0.6f
} }
} }

View File

@ -134,7 +134,7 @@ class AABB {
offsetAssign(Vec3(x, y, z)) offsetAssign(Vec3(x, y, z))
} }
fun offsetAssign(vec3: Vec3) { private fun offsetAssign(vec3: Vec3) {
min += vec3 min += vec3
max += vec3 max += vec3
} }