载入中...
搜索中...
未找到
RTSTerrainTraceInternal.h
浏览该文件的文档.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <limits>
6#include <optional>
7
9
10// Visit every positive-length cell interval, including arbitrarily short corner
11// crossings. The heightfield is piecewise constant, matching the map grid.
12// Inputs come from the validated script world; no state or callback is retained.
13template <class HeightAt>
14std::optional<float> firstTerrainImpact(float fromX, float fromY, float fromHeight, float toX, float toY,
15 float toHeight, float originX, float originY, float cellSize,
16 const HeightAt& heightAt) {
17 const double x = (static_cast<double>(fromX) - originX) / cellSize;
18 const double y = (static_cast<double>(fromY) - originY) / cellSize;
19 const double dx = (static_cast<double>(toX) - fromX) / cellSize;
20 const double dy = (static_cast<double>(toY) - fromY) / cellSize;
21 int cellX = static_cast<int>(std::floor(x));
22 int cellY = static_cast<int>(std::floor(y));
23 const int stepX = dx > 0.0 ? 1 : -1;
24 const int stepY = dy > 0.0 ? 1 : -1;
25 const double infinity = std::numeric_limits<double>::infinity();
26 const double deltaX = dx == 0.0 ? infinity : 1.0 / std::abs(dx);
27 const double deltaY = dy == 0.0 ? infinity : 1.0 / std::abs(dy);
28 double nextX = dx == 0.0 ? infinity : (cellX + (dx > 0.0 ? 1.0 : 0.0) - x) / dx;
29 double nextY = dy == 0.0 ? infinity : (cellY + (dy > 0.0 ? 1.0 : 0.0) - y) / dy;
30 const double heightDelta = static_cast<double>(toHeight) - fromHeight;
31 double enter = 0.0;
32 while (enter < 1.0) {
33 const double leave = std::min({nextX, nextY, 1.0});
34 if (leave > enter) {
35 const double terrain = heightAt(cellX, cellY);
36 const double entryHeight = fromHeight + heightDelta * enter;
37 if (terrain >= entryHeight) return static_cast<float>(enter);
38 if (heightDelta < 0.0) {
39 const double crossing = (terrain - fromHeight) / heightDelta;
40 if (crossing >= enter && crossing < leave) return static_cast<float>(crossing);
41 }
42 }
43 if (leave >= 1.0) break;
44 // Advancing both axes at an exact corner avoids testing cells touched
45 // only at a point. Grid cells use the same half-open convention as navigation.
46 if (nextX <= leave) {
47 cellX += stepX;
48 nextX += deltaX;
49 }
50 if (nextY <= leave) {
51 cellY += stepY;
52 nextY += deltaY;
53 }
54 enter = leave;
55 }
56 return std::nullopt;
57}
58
59} // namespace eve::rts::terrain_internal
double y
double x
std::string terrain
glm::dvec3 dx
float dy
Definition Shadow.cpp:151
std::optional< float > firstTerrainImpact(float fromX, float fromY, float fromHeight, float toX, float toY, float toHeight, float originX, float originY, float cellSize, const HeightAt &heightAt)