mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-16 19:05:02 -04:00
physics: add collisions for all directions
This commit is contained in:
parent
34acc9d846
commit
1222901a85
@ -23,7 +23,6 @@ import de.bixilon.minosoft.gui.rendering.Camera
|
|||||||
import de.bixilon.minosoft.gui.rendering.chunk.VoxelShape
|
import de.bixilon.minosoft.gui.rendering.chunk.VoxelShape
|
||||||
import de.bixilon.minosoft.gui.rendering.chunk.models.AABB
|
import de.bixilon.minosoft.gui.rendering.chunk.models.AABB
|
||||||
import de.bixilon.minosoft.protocol.network.Connection
|
import de.bixilon.minosoft.protocol.network.Connection
|
||||||
import de.bixilon.minosoft.util.logging.Log
|
|
||||||
import glm_.vec3.Vec3
|
import glm_.vec3.Vec3
|
||||||
import java.lang.reflect.InvocationTargetException
|
import java.lang.reflect.InvocationTargetException
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@ -47,7 +46,7 @@ abstract class Entity(
|
|||||||
protected var hasCollisions = true
|
protected var hasCollisions = true
|
||||||
|
|
||||||
fun forceMove(relativePosition: Vec3) {
|
fun forceMove(relativePosition: Vec3) {
|
||||||
position = Vec3(position.x + relativePosition.x, position.y + relativePosition.y, position.z + relativePosition.z)
|
position = position + relativePosition
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addEffect(effect: StatusEffectInstance) {
|
fun addEffect(effect: StatusEffectInstance) {
|
||||||
@ -181,14 +180,13 @@ abstract class Entity(
|
|||||||
|
|
||||||
fun move(deltaPosition: Vec3) {
|
fun move(deltaPosition: Vec3) {
|
||||||
if (!hasCollisions) {
|
if (!hasCollisions) {
|
||||||
position = Vec3(position + deltaPosition)
|
forceMove(deltaPosition)
|
||||||
Log.debug("New Position: $position")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
val aabb = aabb
|
val currentAABB = aabb
|
||||||
val collisionsToCheck = getCollisionsToCheck(deltaPosition, aabb)
|
val collisionsToCheck = getCollisionsToCheck(deltaPosition, currentAABB)
|
||||||
val realMovement = collide(deltaPosition, collisionsToCheck, aabb)
|
val realMovement = collide(deltaPosition, collisionsToCheck, currentAABB)
|
||||||
position = Vec3(position + realMovement)
|
forceMove(realMovement)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getCollisionsToCheck(deltaPosition: Vec3, originalAABB: AABB): VoxelShape {
|
private fun getCollisionsToCheck(deltaPosition: Vec3, originalAABB: AABB): VoxelShape {
|
||||||
@ -203,13 +201,22 @@ abstract class Entity(
|
|||||||
|
|
||||||
private fun collide(deltaPosition: Vec3, collisionsToCheck: VoxelShape, aabb: AABB): Vec3 {
|
private fun collide(deltaPosition: Vec3, collisionsToCheck: VoxelShape, aabb: AABB): Vec3 {
|
||||||
val delta = Vec3(deltaPosition)
|
val delta = Vec3(deltaPosition)
|
||||||
if (deltaPosition.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(Vec3(0f, delta.y, 0f))
|
aabb.offsetAssign(0f, delta.y, 0f)
|
||||||
}
|
}
|
||||||
if (deltaPosition.x != 0.0f) {
|
val xPriority = delta.x <= delta.z
|
||||||
|
if (delta.x != 0.0f && xPriority) {
|
||||||
delta.x = collisionsToCheck.computeOffset(aabb, deltaPosition.x, Axes.X)
|
delta.x = collisionsToCheck.computeOffset(aabb, deltaPosition.x, Axes.X)
|
||||||
aabb.offsetAssign(Vec3(delta.x, 0f, 0f))
|
aabb.offsetAssign(delta.x, 0f, 0f)
|
||||||
|
}
|
||||||
|
if (delta.z != 0.0f) {
|
||||||
|
delta.z = collisionsToCheck.computeOffset(aabb, deltaPosition.z, Axes.Z)
|
||||||
|
aabb.offsetAssign(0f, 0f, delta.z)
|
||||||
|
}
|
||||||
|
if (delta.x != 0.0f && !xPriority) {
|
||||||
|
delta.x = collisionsToCheck.computeOffset(aabb, deltaPosition.x, Axes.X)
|
||||||
|
aabb.offsetAssign(delta.x, 0f, 0f)
|
||||||
}
|
}
|
||||||
return delta
|
return delta
|
||||||
}
|
}
|
||||||
@ -217,15 +224,10 @@ abstract class Entity(
|
|||||||
private val aabb: AABB
|
private val aabb: AABB
|
||||||
get() = DEFAULT_PLAYER_AABB + position
|
get() = DEFAULT_PLAYER_AABB + position
|
||||||
|
|
||||||
fun hasCollisions(): Boolean {
|
|
||||||
return hasCollisions
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val DEFAULT_PLAYER_AABB = AABB(
|
private val DEFAULT_PLAYER_AABB = AABB(
|
||||||
Vec3(-Camera.PLAYER_WIDTH / 2, 0, -Camera.PLAYER_WIDTH / 2),
|
Vec3(-Camera.PLAYER_WIDTH / 2, 0, -Camera.PLAYER_WIDTH / 2),
|
||||||
Vec3(Camera.PLAYER_WIDTH / 2, 1.8, Camera.PLAYER_WIDTH / 2)
|
Vec3(Camera.PLAYER_WIDTH / 2, 1.8, Camera.PLAYER_WIDTH / 2)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -33,9 +33,9 @@ class AABB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun intersect(other: AABB): Boolean {
|
fun intersect(other: AABB): Boolean {
|
||||||
return (min.x <= other.max.x && max.x >= other.min.x) &&
|
return (min.x < other.max.x && max.x > other.min.x) &&
|
||||||
(min.y <= other.max.y && max.y >= other.min.y) &&
|
(min.y < other.max.y && max.y > other.min.y) &&
|
||||||
(min.z <= other.max.z && max.z >= other.min.z)
|
(min.z < other.max.z && max.z > other.min.z)
|
||||||
}
|
}
|
||||||
|
|
||||||
operator fun plus(vec3: Vec3): AABB {
|
operator fun plus(vec3: Vec3): AABB {
|
||||||
@ -126,6 +126,10 @@ class AABB {
|
|||||||
return offset
|
return offset
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun offsetAssign(x: Float, y: Float, z: Float) {
|
||||||
|
offsetAssign(Vec3(x, y, z))
|
||||||
|
}
|
||||||
|
|
||||||
fun offsetAssign(vec3: Vec3) {
|
fun offsetAssign(vec3: Vec3) {
|
||||||
min += vec3
|
min += vec3
|
||||||
max += vec3
|
max += vec3
|
||||||
|
Loading…
x
Reference in New Issue
Block a user