18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 11:34:57 +01:00

fix(editor): the ray to the terrain doesn't come up under a chunk through a gap, and starts behind the camera in the top view

Coming down where there's no terrain and finding itself under the ground of the next chunk, the ray gives up and the depth
read back says where the ground is. In the top view it starts 10 km behind the plane of the camera, as far as the view sees,
so the ground higher than the camera is met too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DDcMn1Q7wdYkbxDUWscNHZ
This commit is contained in:
Claude
2026-11-11 11:36:24 +01:00
parent f1f315d629
commit 9a4d5a4688
2 changed files with 20 additions and 6 deletions

View File

@@ -815,13 +815,17 @@ bool terrain_streamer::raycast(glm::dvec3 const &Origin, glm::dvec3 const &Direc
if (glm::length(Direction) < 1e-9)
return false;
auto const direction{glm::normalize(Direction)};
// above the ground (or where there's none) at the start of a step, below it at its end: the crossing is in between
auto const above = [&](double const T) {
// 1 above the ground, -1 under it, 0 where there's no terrain
auto const side = [&](double const T) {
auto const point{Origin + direction * T};
double height;
return false == height_at(point.x, point.z, height) || point.y > height;
if (false == height_at(point.x, point.z, height))
return 0;
return point.y > height ? 1 : -1;
};
if (false == above(0.0))
auto const above = [&](double const T) { return side(T) >= 0; };
auto last{side(0.0)};
if (last < 0)
return false; // starts under the ground
double previous{0.0};
double t{0.0};
@@ -829,11 +833,17 @@ bool terrain_streamer::raycast(glm::dvec3 const &Origin, glm::dvec3 const &Direc
{
// finer near the camera, where the brush is aimed most
t = std::min(Reach, t + std::max(0.25, t * 0.004));
if (above(t))
auto const now{side(t)};
if (now >= 0)
{
previous = t;
last = now;
continue;
}
// under the ground coming from where there's no terrain: the side of a chunk seen through a gap, not the ground
// seen on the screen; the depth read back says where that is
if (last == 0)
return false;
auto low{previous}, high{t};
for (int i = 0; i < 24; ++i)
{