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

@ -49,20 +49,23 @@ class ItemEntityPhysics(entity: ItemEntity) : EntityPhysics<ItemEntity>(entity)
if (!entity.hasGravity) return
this.velocity = velocity + Vec3d(0.0, -GRAVITY, 0.0)
this.velocity = velocity + GRAVITY
}
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)
var friction = PhysicsConstants.AIR_RESISTANCEf
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
}
this.velocity = Vec3d(this.velocity.x * friction, 0.0, this.velocity.z * friction)
} else {
this.velocity = this.velocity * Vec3d(friction, PhysicsConstants.AIR_RESISTANCE, friction)
}
if (onGround) {
val velocity = this.velocity
@ -79,7 +82,8 @@ class ItemEntityPhysics(entity: ItemEntity) : EntityPhysics<ItemEntity>(entity)
}
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
get() = when (this) {