Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Fix Raycast and implement tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EtheraelEspeon committed Oct 16, 2023
1 parent 4111a13 commit 565273d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
14 changes: 7 additions & 7 deletions Common/World/Raycast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,28 @@ public static class Raycast {
// Gets the one integer intersection coordinate
// axis * blockPos isolates one coordinate from blockPos
// adding (-1, -1, -1) * the min of axis and (0, 0, 0) offsets the integer coordinate if the vector is intersecting from the other side
var worldPos = axis * blockPos + -ivec3.Ones * ivec3.Min(axis, ivec3.Zero);
dvec3 worldPos = ivec3.Abs(axis) * blockPos + -ivec3.Ones * ivec3.Min(axis, ivec3.Zero);
var direction = delta.Normalized;

// intersect with a yz plane
if (axis.x != 0) {
double time = (start.x - worldPos.x) / direction.x;
worldPos.y = (int)(direction.y * time);
worldPos.z = (int)(direction.z * time);
worldPos.y = start.y + direction.y * time;
worldPos.z = start.z + direction.z * time;
return new(blockPos, worldPos, axis);
}
// intersect with an xz plane
if (axis.y != 0) {
double time = (start.y - worldPos.y) / direction.y;
worldPos.x = (int)(direction.x * time);
worldPos.z = (int)(direction.z * time);
worldPos.x = start.x + direction.x * time;
worldPos.z = start.z + direction.z * time;
return new(blockPos, worldPos, axis);
}
// intersect with a xy plane
if (axis.z != 0) {
double time = (start.z - worldPos.z) / direction.z;
worldPos.x = (int)(direction.x * time);
worldPos.y = (int)(direction.y * time);
worldPos.x = start.x + direction.x * time;
worldPos.y = start.y + direction.y * time;
return new(blockPos, worldPos, axis);
}
}
Expand Down
17 changes: 16 additions & 1 deletion Test/Tests/BlockViewSuite.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using GlmSharp;
using Voxel.Common.Tile;
using Voxel.Common.Util;
using Voxel.Common.World;
using Voxel.Test.Mocks;

Expand All @@ -11,9 +13,22 @@ public class BlockViewSuite : TestSuite {
protected override Dictionary<string, Test> DefineTests()
=> new() {
["Raycast"] = () => {
// Mock block view with all tiles below 0 solid
var mock = new BlockViewMock(pos => pos.y < 0 ? Blocks.Stone : Blocks.Air);

Console.WriteLine(mock.Cast(new(0.5, 10.2, 0.8), new(5.333, -9.4, -5.1674)));
var rng = new Random(0);
double RandomCoord()
=> rng.NextDouble() * 200d - 100d;

// Test downward casts
for (int i = 0; i < 100; i++) {
// world pos on the top surface of the tested block
var randomPos = new dvec3(RandomCoord(), 0, RandomCoord());

var hit = mock.Cast(randomPos + dvec3.UnitY, randomPos - dvec3.UnitY);
Assert(hit?.BlockPos == randomPos.WorldToBlockPosition() - ivec3.UnitY, "Hit correct block");
Assert((hit?.WorldPos - randomPos)?.Length < 0.0001f, $"Hit correct world position");
}
}
};
}

0 comments on commit 565273d

Please sign in to comment.