optimize item entity physics performance

This commit is contained in:
Bixilon 2023-06-25 00:34:01 +02:00
parent 3d301929fe
commit 468e3ec735
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4

View File

@ -31,11 +31,11 @@ class ItemEntityPhysics(entity: ItemEntity) : EntityPhysics<ItemEntity>(entity)
private fun updateFluidVelocity(friction: Float) { private fun updateFluidVelocity(friction: Float) {
val velocity = this.velocity val velocity = this.velocity
this.velocity = Vec3d( this.velocity = Vec3d(
velocity.x * friction, velocity.x * friction,
velocity.y + if (this.velocity.y < 0.06f) 5.0E-4f else 0.0f, velocity.y + if (this.velocity.y < 0.06f) 5.0E-4f else 0.0f,
velocity.z * friction, velocity.z * friction,
) )
} }
private fun updateVelocity() { private fun updateVelocity() {
@ -49,20 +49,23 @@ class ItemEntityPhysics(entity: ItemEntity) : EntityPhysics<ItemEntity>(entity)
if (!entity.hasGravity) return if (!entity.hasGravity) return
this.velocity = velocity + Vec3d(0.0, -GRAVITY, 0.0) this.velocity = velocity + GRAVITY
} }
private fun move() { private fun move() {
if (onGround && this.velocity.xz.length2() <= 9.999999747378752E-6 && (entity.age + (entity.id ?: 0)) % 4 != 0) return // This is not 100% vanilla, but performance optimized
if (onGround && this.velocity.xz.length2() <= 9.999999747378752E-6 && entity.age % 4 != 0) return
move(this.velocity) move(this.velocity)
var friction = PhysicsConstants.AIR_RESISTANCEf var friction = PhysicsConstants.AIR_RESISTANCEf
if (onGround) { if (onGround) {
val frictionPosition = (position + Vec3d(0, -1, 0)).blockPosition.inChunkPosition val frictionPosition = (position + FRICTION_OFFSET).blockPosition.inChunkPosition
friction *= positionInfo.chunk?.get(frictionPosition)?.block?.nullCast<FrictionBlock>()?.friction ?: FrictionBlock.DEFAULT_FRICTION friction *= positionInfo.chunk?.get(frictionPosition)?.block?.nullCast<FrictionBlock>()?.friction ?: FrictionBlock.DEFAULT_FRICTION
this.velocity = Vec3d(this.velocity.x * friction, 0.0, this.velocity.z * friction)
} else {
this.velocity = this.velocity * Vec3d(friction, PhysicsConstants.AIR_RESISTANCE, friction)
} }
this.velocity = this.velocity * Vec3d(friction, PhysicsConstants.AIR_RESISTANCE, friction)
if (onGround) { if (onGround) {
val velocity = this.velocity val velocity = this.velocity
@ -79,7 +82,8 @@ class ItemEntityPhysics(entity: ItemEntity) : EntityPhysics<ItemEntity>(entity)
} }
companion object { companion object {
const val GRAVITY = 0.04 val GRAVITY = Vec3d(0.0, -0.04, 0.0)
val FRICTION_OFFSET = Vec3d(0, -1, 0)
val Fluid.friction: Float val Fluid.friction: Float
get() = when (this) { get() = when (this) {