From f87c0e2ecd7e1c45eca496b5e9cb7748bb3997b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Tue, 2 Mar 2021 22:25:38 +0100 Subject: [PATCH] Raycasting world now returns intersection location and face --- Sandbox/src/VoxelFun/VoxelTestLayer.bf | 14 ++++++++++---- Sandbox/src/VoxelFun/World.bf | 24 +++++++++++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Sandbox/src/VoxelFun/VoxelTestLayer.bf b/Sandbox/src/VoxelFun/VoxelTestLayer.bf index 6d20ab5..82ec78e 100644 --- a/Sandbox/src/VoxelFun/VoxelTestLayer.bf +++ b/Sandbox/src/VoxelFun/VoxelTestLayer.bf @@ -380,7 +380,7 @@ namespace Sandbox.VoxelFun Renderer.Submit(_quadGeometryBinding, _effect, transform); } - _effect.Variables["BaseColor"].SetData(_squareColor1); + //_effect.Variables["BaseColor"].SetData(_squareColor1); _texture.Bind(); Renderer.Submit(_quadGeometryBinding, _textureEffect, .Scaling(1.5f)); @@ -392,8 +392,6 @@ namespace Sandbox.VoxelFun //Ray ray = .(.(0.5f, 128.5f, 0.5f), .(0, -0.1f, -0.1f)); - _effect.Variables["BaseColor"].SetData(.Red); - _texture.Bind(); Matrix mat = .Translation(ray.Start); @@ -415,15 +413,21 @@ namespace Sandbox.VoxelFun Renderer.Submit(_lineGeometryBinding, _textureEffect, mat3); //Point3 lookedAtBlock = _world.RaycastBlock(ray, 10, _cubeGeo, _textureEffect); - Point3 lookedAtBlock = _world.RaycastBlock(.(_camera.Position, _camera.Transform.Forward), 10, _cubeGeo, _textureEffect); + Point3 lookedAtBlock = _world.RaycastBlock(.(_camera.Position, _camera.Transform.Forward), 10, _cubeGeo, _textureEffect, let intersection, let intersectFace); + + intersectFace.ToString(intersectedFaceName..Clear()); Renderer.Submit(_cubeGeo, _textureEffect, .Translation((Vector3)lookedAtBlock)); + + Renderer.Submit(_cubeGeo, _textureEffect, .Translation(intersection) * .Scaling(0.1f) * .Translation(-0.5f, -0.5f, -0.5f)); _world.ChunkManager.Draw(); Renderer.EndScene(); } + String intersectedFaceName = new String() ~ delete _; + ColorRGBA _squareColor0 = ColorRGBA.CornflowerBlue; ColorRGBA _squareColor1; @@ -450,6 +454,8 @@ namespace Sandbox.VoxelFun ImGui.Begin("Test"); + ImGui.LabelText("Intersected Face", intersectedFaceName); + ImGui.DragFloat("Slow Speed", &movementSpeed, 1.0f, 0.01f, 100.0f); ImGui.DragFloat("Fast Speed", &movementSpeedFast, 1.0f, 1f, 10000.0f); diff --git a/Sandbox/src/VoxelFun/World.bf b/Sandbox/src/VoxelFun/World.bf index 7cf8346..215e0ce 100644 --- a/Sandbox/src/VoxelFun/World.bf +++ b/Sandbox/src/VoxelFun/World.bf @@ -181,7 +181,7 @@ namespace Sandbox.VoxelFun return Vector3.Floor(chunkPosition); } - public Point3 RaycastBlock(Ray ray, float maxDistance, GeometryBinding geo, Effect effect) + public Point3 RaycastBlock(Ray ray, float maxDistance, GeometryBinding geo, Effect effect, out Vector3 intersectionPosition, out BlockFace intersectionFace) { Vector3 start = ray.Start; Vector3 dir = ray.Direction.Normalized(); @@ -194,35 +194,45 @@ namespace Sandbox.VoxelFun Point3 step; + BlockFace xFace; + BlockFace yFace; + BlockFace zFace; + if(dir.X < 0) { + xFace = .Right; step.X = -1; rayLengths.X = (start.X - (float)(walker.X)) * unitStepSize.X; } else { + xFace = .Left; step.X = 1; rayLengths.X = ((float)(walker.X + 1) - start.X) * unitStepSize.X; } if(dir.Y < 0) { + yFace = .Top; step.Y = -1; rayLengths.Y = (start.Y - (float)(walker.Y)) * unitStepSize.Y; } else { + yFace = .Bottom; step.Y = 1; rayLengths.Y = ((float)(walker.Y + 1) - start.Y) * unitStepSize.Y; } if(dir.Z < 0) { + zFace = .Back; step.Z = -1; rayLengths.Z = (start.Z - (float)(walker.Z)) * unitStepSize.Z; } else { + zFace = .Front; step.Z = 1; rayLengths.Z = ((float)(walker.Z + 1) - start.Z) * unitStepSize.Z; } @@ -238,12 +248,16 @@ namespace Sandbox.VoxelFun walker.X += step.X; distance = rayLengths.X; rayLengths.X += unitStepSize.X; + + intersectionFace = xFace; } else { walker.Z += step.Z; distance = rayLengths.Z; rayLengths.Z += unitStepSize.Z; + + intersectionFace = zFace; } } else @@ -253,12 +267,16 @@ namespace Sandbox.VoxelFun walker.Y += step.Y; distance = rayLengths.Y; rayLengths.Y += unitStepSize.Y; + + intersectionFace = yFace; } else { walker.Z += step.Z; distance = rayLengths.Z; rayLengths.Z += unitStepSize.Z; + + intersectionFace = zFace; } } @@ -272,11 +290,15 @@ namespace Sandbox.VoxelFun if(blockData != 0) { + intersectionPosition = start + distance * dir; + return walker; } } + intersectionFace = .None; + intersectionPosition = .(float.NaN); return .(int.MaxValue, int.MaxValue, int.MaxValue); } }