Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix move collisions using local velocity #266

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ execute unless entity B5-0-0-0-1 run summon minecraft:marker -30000000 0 1600 {U
scoreboard objectives add bs.in dummy [{"text":"BS ","color":"dark_gray"},{"text":"Input","color":"aqua"}]
scoreboard objectives add bs.data dummy [{"text":"BS ","color":"dark_gray"},{"text":"Data","color":"aqua"}]
scoreboard objectives add bs.const dummy [{"text":"BS ","color":"dark_gray"},{"text":"Constants","color":"aqua"}]
scoreboard objectives add bs.ctx dummy [{"text":"BS ","color":"dark_gray"},{"text":"Context","color":"aqua"}]
scoreboard objectives add bs.vel.x dummy [{"text":"BS ","color":"dark_gray"},{"text":"Velocity X","color":"aqua"}]
scoreboard objectives add bs.vel.y dummy [{"text":"BS ","color":"dark_gray"},{"text":"Velocity Y","color":"aqua"}]
scoreboard objectives add bs.vel.z dummy [{"text":"BS ","color":"dark_gray"},{"text":"Velocity Z","color":"aqua"}]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ forceload remove -30000000 1600
scoreboard objectives remove bs.in
scoreboard objectives remove bs.data
scoreboard objectives remove bs.const
scoreboard objectives remove bs.ctx
scoreboard objectives remove bs.vel.x
scoreboard objectives remove bs.vel.y
scoreboard objectives remove bs.vel.z
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ execute if score #move.e bs.data matches 1 if data storage bs:data move{entities
execute if score #move.vx bs.data matches 0.. at B5-0-0-0-1 align xyz run function bs.move:collision/recurse/x_pos
execute if score #move.vx bs.data matches ..-1 at B5-0-0-0-1 align xyz run function bs.move:collision/recurse/x_neg
tag @e[tag=bs.move.omit] remove bs.move.omit
execute if score #move.ctime bs.data matches 0..999 run function bs.move:collision/resolution/resolve with storage bs:data move
$execute if score #move.ctime bs.data matches 0..999 run function bs.move:collision/resolution/resolve {type:$(type)}
execute in minecraft:overworld run tp B5-0-0-0-1 -30000000 0 1600
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
# compute a bounding box that encompasses all passengers
function #bs.hitbox:get_entity

execute store result score #move._h bs.data run data get storage bs:out hitbox.height 1000
execute store result score #move._w bs.data run data get storage bs:out hitbox.width 500
execute store result score #move._s bs.data run data get storage bs:out hitbox.scale 1000
execute store result score #move._y bs.data run data get entity @s Pos[1] 1000
execute store result score #h bs.ctx run data get storage bs:out hitbox.height 1000
execute store result score #w bs.ctx run data get storage bs:out hitbox.width 500
execute store result score #s bs.ctx run data get storage bs:out hitbox.scale 1000
execute store result score #y bs.ctx run data get entity @s Pos[1] 1000

scoreboard players operation #move._h bs.data *= #move._s bs.data
scoreboard players operation #move._w bs.data *= #move._s bs.data
scoreboard players operation #move._y bs.data -= #move.y bs.data
scoreboard players operation #move._y bs.data *= 1000 bs.const
scoreboard players operation #move._h bs.data += #move._y bs.data
execute store result score #move.sh bs.data run scoreboard players operation #move.h bs.data > #move._h bs.data
execute store result score #move.sw bs.data run scoreboard players operation #move.w bs.data > #move._w bs.data
scoreboard players operation #h bs.ctx *= #s bs.ctx
scoreboard players operation #w bs.ctx *= #s bs.ctx
scoreboard players operation #y bs.ctx -= #move.y bs.data
scoreboard players operation #y bs.ctx *= 1000 bs.const
scoreboard players operation #h bs.ctx += #y bs.ctx
execute store result score #move.sh bs.data run scoreboard players operation #move.h bs.data > #h bs.ctx
execute store result score #move.sw bs.data run scoreboard players operation #move.w bs.data > #w bs.ctx

execute on passengers run function bs.move:collision/hitbox
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# ------------------------------------------------------------------------------------------------------------
# Copyright (c) 2024 Gunivers
#
# This file is part of the Bookshelf project (https://github.com/Gunivers/Bookshelf).
#
# This source code is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Conditions:
# - You may use this file in compliance with the MPL v2.0
# - Any modifications must be documented and disclosed under the same license
#
# For more details, refer to the MPL v2.0.
# ------------------------------------------------------------------------------------------------------------

# resolve collision using the on_collision callback
$function $(on_collision)
execute store result storage bs:ctx x double .001 run scoreboard players get $move.vel_remaining.x bs.data
execute store result storage bs:ctx y double .001 run scoreboard players get $move.vel_remaining.y bs.data
execute store result storage bs:ctx z double .001 run scoreboard players get $move.vel_remaining.z bs.data
execute unless data storage bs:ctx {x:0d,y:0d,z:0d} at @s run function bs.move:teleport/canonical/run with storage bs:ctx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ------------------------------------------------------------------------------------------------------------
# Copyright (c) 2024 Gunivers
#
# This file is part of the Bookshelf project (https://github.com/Gunivers/Bookshelf).
#
# This source code is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Conditions:
# - You may use this file in compliance with the MPL v2.0
# - Any modifications must be documented and disclosed under the same license
#
# For more details, refer to the MPL v2.0.
# ------------------------------------------------------------------------------------------------------------

# resolve collision using the on_collision callback (ensure the velocity vector is in canonical space for simpler transformation)
execute rotated as @s run function #bs.move:local_to_canonical
$function $(on_collision)
execute store result storage bs:ctx x double .001 run scoreboard players get $move.vel_remaining.x bs.data
execute store result storage bs:ctx y double .001 run scoreboard players get $move.vel_remaining.y bs.data
execute store result storage bs:ctx z double .001 run scoreboard players get $move.vel_remaining.z bs.data
execute unless data storage bs:ctx {x:0d,y:0d,z:0d} at @s run function bs.move:teleport/canonical/run with storage bs:ctx
execute rotated as @s run function #bs.move:canonical_to_local
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,4 @@ scoreboard players operation $move.vel_remaining.x bs.data += #move.vx bs.data
scoreboard players operation $move.vel_remaining.y bs.data += #move.vy bs.data
scoreboard players operation $move.vel_remaining.z bs.data += #move.vz bs.data

# resolve collision using the on_collision callback (ensure the velocity vector is in canonical space for simpler transformation)
execute if score #move.is_local bs.data matches 1 rotated as @s run function #bs.move:local_to_canonical
$function $(on_collision)
execute store result storage bs:ctx x double .001 run scoreboard players get $move.vel_remaining.x bs.data
execute store result storage bs:ctx y double .001 run scoreboard players get $move.vel_remaining.y bs.data
execute store result storage bs:ctx z double .001 run scoreboard players get $move.vel_remaining.z bs.data
execute unless data storage bs:ctx {x:0d,y:0d,z:0d} at @s run function bs.move:teleport/canonical/run with storage bs:ctx
execute if score #move.is_local bs.data matches 1 rotated as @s run function #bs.move:canonical_to_local
$function bs.move:collision/resolution/callback/$(type) with storage bs:data move
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,33 @@
execute as B5-0-0-0-1 run function bs.move:convert/canonical_to_local/sincos

# X' = Z * sin(h) + X * cos(h)
scoreboard players operation #move.canonical_to_local.x bs.data = @s bs.vel.z
scoreboard players operation #move.canonical_to_local.x bs.data *= #move.canonical_to_local.sin.h bs.data
scoreboard players operation #move.canonical_to_local bs.data = @s bs.vel.x
scoreboard players operation #move.canonical_to_local bs.data *= #move.canonical_to_local.cos.h bs.data
scoreboard players operation #move.canonical_to_local.x bs.data += #move.canonical_to_local bs.data
scoreboard players operation #move.canonical_to_local.x bs.data /= 1000 bs.const
scoreboard players operation #x bs.ctx = @s bs.vel.z
scoreboard players operation #x bs.ctx *= #j bs.ctx
scoreboard players operation #t bs.ctx = @s bs.vel.x
scoreboard players operation #t bs.ctx *= #i bs.ctx
scoreboard players operation #x bs.ctx += #t bs.ctx
scoreboard players operation #x bs.ctx /= 1000 bs.const

# Y' = (z * cos(h) - x * sin(h)) * sin(v) + y * cos(v)
scoreboard players operation #move.canonical_to_local.y bs.data = @s bs.vel.z
scoreboard players operation #move.canonical_to_local.y bs.data *= #move.canonical_to_local.cos.h bs.data
scoreboard players operation #move.canonical_to_local bs.data = @s bs.vel.x
scoreboard players operation #move.canonical_to_local bs.data *= #move.canonical_to_local.sin.h bs.data
scoreboard players operation #move.canonical_to_local.y bs.data -= #move.canonical_to_local bs.data
execute store result score #move.canonical_to_local.z bs.data run scoreboard players operation #move.canonical_to_local.y bs.data /= 1000 bs.const
scoreboard players operation #move.canonical_to_local.y bs.data *= #move.canonical_to_local.sin.v bs.data
scoreboard players operation #move.canonical_to_local bs.data = @s bs.vel.y
scoreboard players operation #move.canonical_to_local bs.data *= #move.canonical_to_local.cos.v bs.data
scoreboard players operation #move.canonical_to_local.y bs.data += #move.canonical_to_local bs.data
scoreboard players operation #move.canonical_to_local.y bs.data /= 1000 bs.const
scoreboard players operation #y bs.ctx = @s bs.vel.z
scoreboard players operation #y bs.ctx *= #i bs.ctx
scoreboard players operation #t bs.ctx = @s bs.vel.x
scoreboard players operation #t bs.ctx *= #j bs.ctx
scoreboard players operation #y bs.ctx -= #t bs.ctx
execute store result score #z bs.ctx run scoreboard players operation #y bs.ctx /= 1000 bs.const
scoreboard players operation #y bs.ctx *= #l bs.ctx
scoreboard players operation #t bs.ctx = @s bs.vel.y
scoreboard players operation #t bs.ctx *= #k bs.ctx
scoreboard players operation #y bs.ctx += #t bs.ctx
scoreboard players operation #y bs.ctx /= 1000 bs.const

# Z' = (z * cos(h) - x * sin(h)) * cos(v) - y * sin(v)
scoreboard players operation #move.canonical_to_local.z bs.data *= #move.canonical_to_local.cos.v bs.data
scoreboard players operation #move.canonical_to_local bs.data = @s bs.vel.y
scoreboard players operation #move.canonical_to_local bs.data *= #move.canonical_to_local.sin.v bs.data
scoreboard players operation #move.canonical_to_local.z bs.data -= #move.canonical_to_local bs.data
scoreboard players operation #move.canonical_to_local.z bs.data /= 1000 bs.const
scoreboard players operation #z bs.ctx *= #k bs.ctx
scoreboard players operation #t bs.ctx = @s bs.vel.y
scoreboard players operation #t bs.ctx *= #l bs.ctx
scoreboard players operation #z bs.ctx -= #t bs.ctx
scoreboard players operation #z bs.ctx /= 1000 bs.const

scoreboard players operation @s bs.vel.x = #move.canonical_to_local.x bs.data
scoreboard players operation @s bs.vel.y = #move.canonical_to_local.y bs.data
scoreboard players operation @s bs.vel.z = #move.canonical_to_local.z bs.data
scoreboard players operation @s bs.vel.x = #x bs.ctx
scoreboard players operation @s bs.vel.y = #y bs.ctx
scoreboard players operation @s bs.vel.z = #z bs.ctx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
# ------------------------------------------------------------------------------------------------------------

execute positioned 0.0 0.0 0.0 rotated ~ 0.0 run tp @s ^ ^ ^10.0
execute store result score #move.canonical_to_local.cos.h bs.data run data get entity @s Pos[2] 100
execute store result score #move.canonical_to_local.sin.h bs.data run data get entity @s Pos[0] -100
execute store result score #i bs.ctx run data get entity @s Pos[2] 100
execute store result score #j bs.ctx run data get entity @s Pos[0] -100
execute positioned 0.0 0.0 0.0 rotated 0.0 ~ run tp @s ^ ^ ^10.0
execute store result score #move.canonical_to_local.cos.v bs.data run data get entity @s Pos[2] 100
execute store result score #move.canonical_to_local.sin.v bs.data run data get entity @s Pos[1] -100
execute store result score #k bs.ctx run data get entity @s Pos[2] 100
execute store result score #l bs.ctx run data get entity @s Pos[1] -100
execute in minecraft:overworld run tp @s -30000000 0 1600
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@

$tp @s ~$(x) ~$(y) ~$(z)

scoreboard players set #move.is_local bs.data 0
execute unless data storage bs:data move{blocks:0b,entities:0b} run function bs.move:collision/handle
execute unless data storage bs:data move{blocks:0b,entities:0b} run function bs.move:collision/handle {type:"canonical"}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@

$tp @s ^$(x) ^$(y) ^$(z)

scoreboard players set #move.is_local bs.data 1
execute unless data storage bs:data move{blocks:0b,entities:0b} run function bs.move:collision/handle
execute unless data storage bs:data move{blocks:0b,entities:0b} run function bs.move:collision/handle {type:"local"}
2 changes: 2 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
### 🐛 Fixes
- ([243](https://github.com/Gunivers/Bookshelf/issues/243)) Fix `bs.health` and `bs.move` unload functions.
- ([254](https://github.com/Gunivers/Bookshelf/issues/254)) Fix interruptions in the execution loop caused by `#bs.schedule:schedule` callbacks.

- ([255](https://github.com/Gunivers/Bookshelf/issues/255)) Fix `bs.move` local velocity collisions.
- ([254](https://github.com/Gunivers/Bookshelf/pull/262)) Fix collisions with paintings for `bs.raycast` and `bs.move`.
- ([256](https://github.com/Gunivers/Bookshelf/issues/256)) Fix XP progress functions at bounds (0% and 100%).

Expand Down
Loading