Skip to content

Commit

Permalink
fix: The raycast was skipping the block that the client is inside. (#145
Browse files Browse the repository at this point in the history
)

* fix: The raycast was skipping the block that the client is inside.

* add test
  • Loading branch information
zardoy authored Jan 24, 2025
1 parent 1d58891 commit 63b6489
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/world.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class World extends EventEmitter {

async raycast (from, direction, range, matcher = null) {
const iter = new RaycastIterator(from, direction, range)
let pos = iter.next()
let pos = from
while (pos) {
const position = new Vec3(pos.x, pos.y, pos.z)
const block = await this.getBlock(position)
Expand Down
2 changes: 1 addition & 1 deletion src/worldsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class WorldSync extends EventEmitter {

raycast (from, direction, range, matcher = null) {
const iter = new RaycastIterator(from, direction, range)
let pos = iter.next()
let pos = from
while (pos) {
const position = new Vec3(pos.x, pos.y, pos.z)
const block = this.getBlock(position)
Expand Down
8 changes: 8 additions & 0 deletions test/raycast.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ describe('raycasting', function () {
const down = await world.raycast(head, new Vec3(0, -1, 0), 10)
assert.notStrictEqual(down, null)
})

it('raycast inside a block', async () => {
const head = new Vec3(0, 3.6, 0)
const inside = await world.raycast(head, new Vec3(0, 0, 1), 10)
assert.strictEqual(inside.position.x, 0)
assert.strictEqual(inside.position.y, 3)
assert.strictEqual(inside.position.z, 0)
})
})

0 comments on commit 63b6489

Please sign in to comment.