From 061c598935a72626eeea9c9aa5d73dd2f7c4ce92 Mon Sep 17 00:00:00 2001 From: LukynkaCZE Date: Wed, 4 Jun 2025 17:15:55 +0200 Subject: [PATCH 1/7] what is velocity --- src/main/kotlin/Main.kt | 33 ++++++++++-- .../io/github/dockyardmc/entity/Entity.kt | 1 + .../entity/handlers/EntityMovementHandler.kt | 10 ++++ .../io/github/dockyardmc/location/Location.kt | 2 +- .../maths/velocity/VelocityPhysics.kt | 54 +++++++++++++++++++ 5 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/io/github/dockyardmc/entity/handlers/EntityMovementHandler.kt create mode 100644 src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index bd32a5429..4563856ea 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,14 +1,24 @@ import io.github.dockyardmc.DockyardServer import io.github.dockyardmc.commands.Commands +import io.github.dockyardmc.entity.EntityManager.despawnEntity +import io.github.dockyardmc.entity.EntityManager.spawnEntity +import io.github.dockyardmc.entity.ItemDropEntity import io.github.dockyardmc.events.Events import io.github.dockyardmc.events.PlayerJoinEvent import io.github.dockyardmc.events.PlayerRightClickWithItemEvent import io.github.dockyardmc.inventory.give +import io.github.dockyardmc.item.ItemStack +import io.github.dockyardmc.maths.randomFloat import io.github.dockyardmc.maths.vectors.Vector3d +import io.github.dockyardmc.maths.vectors.Vector3f +import io.github.dockyardmc.maths.velocity.VelocityPhysics import io.github.dockyardmc.player.systems.GameMode import io.github.dockyardmc.registry.Items -import io.github.dockyardmc.ui.TestScreen +import io.github.dockyardmc.registry.registries.ItemRegistry +import io.github.dockyardmc.scheduler.runLaterAsync +import io.github.dockyardmc.scheduler.runnables.ticks import io.github.dockyardmc.utils.DebugSidebar +import kotlin.time.Duration.Companion.seconds fun main() { val server = DockyardServer { @@ -30,11 +40,26 @@ fun main() { event.player.setVelocity(Vector3d(0, 20.5, 0)) } - Commands.add("/ui") { + Commands.add("/test") { execute { ctx -> val player = ctx.getPlayerOrThrow() - val screen = TestScreen() - screen.open(player) + player.world.scheduler.repeat(30, 1.ticks) { + repeat(3) { + val physics = VelocityPhysics(player.location, Vector3f(randomFloat(-0.50f, 0.50f), 0.5f, randomFloat(-0.50f, 0.50f)), true) + val entity = player.world.spawnEntity(ItemDropEntity(player.location, ItemStack(ItemRegistry.items.values.random()))) as ItemDropEntity + + physics.onTick.subscribe { location -> + entity.teleport(location) + } + + physics.start() + + runLaterAsync(5.seconds) { + physics.dispose() + player.world.despawnEntity(entity) + } + } + } } } diff --git a/src/main/kotlin/io/github/dockyardmc/entity/Entity.kt b/src/main/kotlin/io/github/dockyardmc/entity/Entity.kt index 9e3ebed70..65be9e335 100644 --- a/src/main/kotlin/io/github/dockyardmc/entity/Entity.kt +++ b/src/main/kotlin/io/github/dockyardmc/entity/Entity.kt @@ -70,6 +70,7 @@ abstract class Entity(open var location: Location, open var world: World) : Disp val hasNoGravity: Bindable = bindablePool.provideBindable(true) val isSilent: Bindable = bindablePool.provideBindable(false) val stuckArrows: Bindable = bindablePool.provideBindable(0) + var gravityTickCount = 0 val potionEffects: BindableMap = bindablePool.provideBindableMap() val isInvisible: Bindable = bindablePool.provideBindable(false) diff --git a/src/main/kotlin/io/github/dockyardmc/entity/handlers/EntityMovementHandler.kt b/src/main/kotlin/io/github/dockyardmc/entity/handlers/EntityMovementHandler.kt new file mode 100644 index 000000000..9c11adcab --- /dev/null +++ b/src/main/kotlin/io/github/dockyardmc/entity/handlers/EntityMovementHandler.kt @@ -0,0 +1,10 @@ +package io.github.dockyardmc.entity.handlers + +import io.github.dockyardmc.entity.Entity + +class EntityMovementHandler(override val entity: Entity) : TickableEntityHandler { + + override fun tick() { + entity.gravityTickCount = if (entity.isOnGround) 0 else entity.gravityTickCount + 1 + } +} \ No newline at end of file diff --git a/src/main/kotlin/io/github/dockyardmc/location/Location.kt b/src/main/kotlin/io/github/dockyardmc/location/Location.kt index 16e0a0ddd..f0806ab40 100644 --- a/src/main/kotlin/io/github/dockyardmc/location/Location.kt +++ b/src/main/kotlin/io/github/dockyardmc/location/Location.kt @@ -15,7 +15,7 @@ import io.github.dockyardmc.world.chunk.Chunk import io.netty.buffer.ByteBuf import kotlin.math.* -class Location( +data class Location( var x: Double, var y: Double, var z: Double, diff --git a/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt new file mode 100644 index 000000000..0e9c1db4c --- /dev/null +++ b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt @@ -0,0 +1,54 @@ +package io.github.dockyardmc.maths.velocity + +import cz.lukynka.bindables.BindableDispatcher +import io.github.dockyardmc.location.Location +import io.github.dockyardmc.maths.vectors.Vector3f +import io.github.dockyardmc.scheduler.runnables.ticks +import io.github.dockyardmc.utils.Disposable + +class VelocityPhysics(startLocation: Location, initialVelocity: Vector3f, val handlesCollision: Boolean = false) : Disposable { + + private val gravity: Vector3f = Vector3f(0f, -0.05f, 0f) + private val friction: Vector3f = Vector3f(0.98f) + + private var currentLocation: Location = startLocation + private var currentVelocity: Vector3f = initialVelocity + + val onTick: BindableDispatcher = BindableDispatcher() + private var running: Boolean = false + + private val schedulerTask = startLocation.world.scheduler.runRepeating(1.ticks) { + if (!running) return@runRepeating + currentVelocity = currentVelocity + gravity + currentVelocity = currentVelocity * friction + + currentLocation = Location( + currentLocation.x + currentVelocity.x, + currentLocation.y + currentVelocity.y, + currentLocation.z + currentVelocity.z, + currentLocation.yaw, + currentLocation.pitch, + currentLocation.world + ) + + if(!currentLocation.block.isAir()) { + dispose() + return@runRepeating + } + + onTick.dispatch(currentLocation) + } + + fun start() { + running = true + } + + fun stop() { + running = false + } + + override fun dispose() { + schedulerTask.cancel() + onTick.dispose() + } +} \ No newline at end of file From ceab30e7fa2d4f972e6fcce77035b24fa8de72fa Mon Sep 17 00:00:00 2001 From: LukynkaCZE Date: Wed, 4 Jun 2025 18:08:19 +0200 Subject: [PATCH 2/7] seperate air friction and ground friction --- src/main/kotlin/Main.kt | 2 +- .../maths/velocity/VelocityPhysics.kt | 38 ++++++++++++++++++- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 4563856ea..f9f5a2b37 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -45,7 +45,7 @@ fun main() { val player = ctx.getPlayerOrThrow() player.world.scheduler.repeat(30, 1.ticks) { repeat(3) { - val physics = VelocityPhysics(player.location, Vector3f(randomFloat(-0.50f, 0.50f), 0.5f, randomFloat(-0.50f, 0.50f)), true) + val physics = VelocityPhysics(player.location.add(0.0, 0.5, 0.0), Vector3f(randomFloat(-0.50f, 0.50f), 0.5f, randomFloat(-0.50f, 0.50f)), true) val entity = player.world.spawnEntity(ItemDropEntity(player.location, ItemStack(ItemRegistry.items.values.random()))) as ItemDropEntity physics.onTick.subscribe { location -> diff --git a/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt index 0e9c1db4c..2e4e20d3f 100644 --- a/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt +++ b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt @@ -5,11 +5,13 @@ import io.github.dockyardmc.location.Location import io.github.dockyardmc.maths.vectors.Vector3f import io.github.dockyardmc.scheduler.runnables.ticks import io.github.dockyardmc.utils.Disposable +import io.github.dockyardmc.world.World class VelocityPhysics(startLocation: Location, initialVelocity: Vector3f, val handlesCollision: Boolean = false) : Disposable { private val gravity: Vector3f = Vector3f(0f, -0.05f, 0f) - private val friction: Vector3f = Vector3f(0.98f) + private val airFriction: Vector3f = Vector3f(0.98f) + private val groundFriction: Vector3f = Vector3f(0.1f, 0.98f, 0.1f) private var currentLocation: Location = startLocation private var currentVelocity: Vector3f = initialVelocity @@ -19,9 +21,29 @@ class VelocityPhysics(startLocation: Location, initialVelocity: Vector3f, val ha private val schedulerTask = startLocation.world.scheduler.runRepeating(1.ticks) { if (!running) return@runRepeating + + var friction = airFriction + if(currentLocation.subtract(0.0, 0.05, 0.0).block.registryBlock.isSolid) { + friction = groundFriction + } + currentVelocity = currentVelocity + gravity currentVelocity = currentVelocity * friction + val newLocXOnly = newLocation(currentVelocity.x, 0f, 0f, currentLocation.yaw, currentLocation.pitch, currentLocation.world) + val newLocYOnly = newLocation(0f, currentVelocity.y, 0f, currentLocation.yaw, currentLocation.pitch, currentLocation.world) + val newLocZOnly = newLocation(0f, 0f, currentVelocity.z, currentLocation.yaw, currentLocation.pitch, currentLocation.world) + + if (newLocXOnly.block.registryBlock.isSolid) { + currentVelocity.x = 0f + } + if (newLocYOnly.block.registryBlock.isSolid) { + currentVelocity.y = 0f + } + if (newLocZOnly.block.registryBlock.isSolid) { + currentVelocity.z = 0f + } + currentLocation = Location( currentLocation.x + currentVelocity.x, currentLocation.y + currentVelocity.y, @@ -31,7 +53,7 @@ class VelocityPhysics(startLocation: Location, initialVelocity: Vector3f, val ha currentLocation.world ) - if(!currentLocation.block.isAir()) { + if (currentVelocity.isZero) { dispose() return@runRepeating } @@ -51,4 +73,16 @@ class VelocityPhysics(startLocation: Location, initialVelocity: Vector3f, val ha schedulerTask.cancel() onTick.dispose() } + + + private fun newLocation(newX: Float, newY: Float, newZ: Float, yaw: Float, pitch: Float, world: World): Location { + return Location( + currentLocation.x + newX, + currentLocation.y + newY, + currentLocation.z + newZ, + yaw, + pitch, + world + ) + } } \ No newline at end of file From 31f48df32a997bd07952aedc8f0b47827577a0ff Mon Sep 17 00:00:00 2001 From: LukynkaCZE Date: Wed, 4 Jun 2025 19:02:31 +0200 Subject: [PATCH 3/7] more collison stuff fix on diagonal axis --- src/main/kotlin/Main.kt | 2 +- .../maths/velocity/VelocityPhysics.kt | 24 ++++++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index f9f5a2b37..4ab2ad884 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -43,7 +43,7 @@ fun main() { Commands.add("/test") { execute { ctx -> val player = ctx.getPlayerOrThrow() - player.world.scheduler.repeat(30, 1.ticks) { + player.world.scheduler.repeat(5, 1.ticks) { repeat(3) { val physics = VelocityPhysics(player.location.add(0.0, 0.5, 0.0), Vector3f(randomFloat(-0.50f, 0.50f), 0.5f, randomFloat(-0.50f, 0.50f)), true) val entity = player.world.spawnEntity(ItemDropEntity(player.location, ItemStack(ItemRegistry.items.values.random()))) as ItemDropEntity diff --git a/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt index 2e4e20d3f..6079af62f 100644 --- a/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt +++ b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt @@ -44,14 +44,21 @@ class VelocityPhysics(startLocation: Location, initialVelocity: Vector3f, val ha currentVelocity.z = 0f } - currentLocation = Location( - currentLocation.x + currentVelocity.x, - currentLocation.y + currentVelocity.y, - currentLocation.z + currentVelocity.z, - currentLocation.yaw, - currentLocation.pitch, - currentLocation.world - ) + val newLocFull = newLocation(currentVelocity.x, currentVelocity.y, currentVelocity.z, currentLocation.yaw, currentLocation.pitch, currentLocation.world) + if (handlesCollision && newLocFull.block.registryBlock.isSolid) { + currentVelocity = Vector3f(0f, 0f, 0f) // Stop all movement + } + + if (!handlesCollision || !newLocFull.block.registryBlock.isSolid) { + currentLocation = Location( + currentLocation.x + currentVelocity.x, + currentLocation.y + currentVelocity.y, + currentLocation.z + currentVelocity.z, + currentLocation.yaw, + currentLocation.pitch, + currentLocation.world + ) + } if (currentVelocity.isZero) { dispose() @@ -74,7 +81,6 @@ class VelocityPhysics(startLocation: Location, initialVelocity: Vector3f, val ha onTick.dispose() } - private fun newLocation(newX: Float, newY: Float, newZ: Float, yaw: Float, pitch: Float, world: World): Location { return Location( currentLocation.x + newX, From 7cd6504d9a7d3e61131c7e2648a017dfba6cd774 Mon Sep 17 00:00:00 2001 From: LukynkaCZE Date: Wed, 4 Jun 2025 19:07:09 +0200 Subject: [PATCH 4/7] forgor return --- .../io/github/dockyardmc/maths/velocity/VelocityPhysics.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt index 6079af62f..6467f6ac2 100644 --- a/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt +++ b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt @@ -23,7 +23,7 @@ class VelocityPhysics(startLocation: Location, initialVelocity: Vector3f, val ha if (!running) return@runRepeating var friction = airFriction - if(currentLocation.subtract(0.0, 0.05, 0.0).block.registryBlock.isSolid) { + if(currentLocation.subtract(0.0, 0.03, 0.0).block.registryBlock.isSolid) { friction = groundFriction } @@ -47,6 +47,7 @@ class VelocityPhysics(startLocation: Location, initialVelocity: Vector3f, val ha val newLocFull = newLocation(currentVelocity.x, currentVelocity.y, currentVelocity.z, currentLocation.yaw, currentLocation.pitch, currentLocation.world) if (handlesCollision && newLocFull.block.registryBlock.isSolid) { currentVelocity = Vector3f(0f, 0f, 0f) // Stop all movement + return@runRepeating } if (!handlesCollision || !newLocFull.block.registryBlock.isSolid) { From 5b04da042710797fc4756c19c655b0f1a790ed13 Mon Sep 17 00:00:00 2001 From: LukynkaCZE Date: Wed, 4 Jun 2025 19:07:15 +0200 Subject: [PATCH 5/7] a --- .../io/github/dockyardmc/maths/velocity/VelocityPhysics.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt index 6467f6ac2..4d266e56d 100644 --- a/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt +++ b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt @@ -46,7 +46,7 @@ class VelocityPhysics(startLocation: Location, initialVelocity: Vector3f, val ha val newLocFull = newLocation(currentVelocity.x, currentVelocity.y, currentVelocity.z, currentLocation.yaw, currentLocation.pitch, currentLocation.world) if (handlesCollision && newLocFull.block.registryBlock.isSolid) { - currentVelocity = Vector3f(0f, 0f, 0f) // Stop all movement + currentVelocity = Vector3f(0f, 0f, 0f) return@runRepeating } From 8473fef1985797596f8d03fe445299d116290a90 Mon Sep 17 00:00:00 2001 From: LukynkaCZE Date: Wed, 4 Jun 2025 19:24:28 +0200 Subject: [PATCH 6/7] add `floorBouncinessFactor` --- .../dockyardmc/maths/velocity/VelocityPhysics.kt | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt index 4d266e56d..9882948b0 100644 --- a/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt +++ b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt @@ -7,7 +7,13 @@ import io.github.dockyardmc.scheduler.runnables.ticks import io.github.dockyardmc.utils.Disposable import io.github.dockyardmc.world.World -class VelocityPhysics(startLocation: Location, initialVelocity: Vector3f, val handlesCollision: Boolean = false) : Disposable { +class VelocityPhysics( + startLocation: Location, + initialVelocity: Vector3f, + val handlesCollision: Boolean = false, + val floorBouncinessFactor: Float = 0.4f, + + ) : Disposable { private val gravity: Vector3f = Vector3f(0f, -0.05f, 0f) private val airFriction: Vector3f = Vector3f(0.98f) @@ -23,7 +29,7 @@ class VelocityPhysics(startLocation: Location, initialVelocity: Vector3f, val ha if (!running) return@runRepeating var friction = airFriction - if(currentLocation.subtract(0.0, 0.03, 0.0).block.registryBlock.isSolid) { + if (currentLocation.subtract(0.0, 0.05, 0.0).block.registryBlock.isSolid) { friction = groundFriction } @@ -38,7 +44,7 @@ class VelocityPhysics(startLocation: Location, initialVelocity: Vector3f, val ha currentVelocity.x = 0f } if (newLocYOnly.block.registryBlock.isSolid) { - currentVelocity.y = 0f + currentVelocity.y = if (floorBouncinessFactor == -1f) 0f else (currentVelocity.y * -1) - 0.4f } if (newLocZOnly.block.registryBlock.isSolid) { currentVelocity.z = 0f From c68f660e291f19cf15933d425b46d441692bf5e2 Mon Sep 17 00:00:00 2001 From: LukynkaCZE Date: Wed, 4 Jun 2025 19:24:47 +0200 Subject: [PATCH 7/7] rename to `VelocitySimulation` --- src/main/kotlin/Main.kt | 4 ++-- .../velocity/{VelocityPhysics.kt => VelocitySimulation.kt} | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/main/kotlin/io/github/dockyardmc/maths/velocity/{VelocityPhysics.kt => VelocitySimulation.kt} (99%) diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 4ab2ad884..1041e444a 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -11,7 +11,7 @@ import io.github.dockyardmc.item.ItemStack import io.github.dockyardmc.maths.randomFloat import io.github.dockyardmc.maths.vectors.Vector3d import io.github.dockyardmc.maths.vectors.Vector3f -import io.github.dockyardmc.maths.velocity.VelocityPhysics +import io.github.dockyardmc.maths.velocity.VelocitySimulation import io.github.dockyardmc.player.systems.GameMode import io.github.dockyardmc.registry.Items import io.github.dockyardmc.registry.registries.ItemRegistry @@ -45,7 +45,7 @@ fun main() { val player = ctx.getPlayerOrThrow() player.world.scheduler.repeat(5, 1.ticks) { repeat(3) { - val physics = VelocityPhysics(player.location.add(0.0, 0.5, 0.0), Vector3f(randomFloat(-0.50f, 0.50f), 0.5f, randomFloat(-0.50f, 0.50f)), true) + val physics = VelocitySimulation(player.location.add(0.0, 0.5, 0.0), Vector3f(randomFloat(-0.50f, 0.50f), 0.5f, randomFloat(-0.50f, 0.50f)), true) val entity = player.world.spawnEntity(ItemDropEntity(player.location, ItemStack(ItemRegistry.items.values.random()))) as ItemDropEntity physics.onTick.subscribe { location -> diff --git a/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocitySimulation.kt similarity index 99% rename from src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt rename to src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocitySimulation.kt index 9882948b0..72893e725 100644 --- a/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocityPhysics.kt +++ b/src/main/kotlin/io/github/dockyardmc/maths/velocity/VelocitySimulation.kt @@ -7,7 +7,7 @@ import io.github.dockyardmc.scheduler.runnables.ticks import io.github.dockyardmc.utils.Disposable import io.github.dockyardmc.world.World -class VelocityPhysics( +class VelocitySimulation( startLocation: Location, initialVelocity: Vector3f, val handlesCollision: Boolean = false,