-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroundMechanicsGL.js
109 lines (86 loc) · 3.48 KB
/
groundMechanicsGL.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//groundMechanics.js
import { groundPolygons } from './levelBuilderGL.js';
let platformPositionT = 0;
const platformSpeed = 0.005;
let platformDirection = 1;
export function updateFloatingPlatformPosition(segments) {
platformPositionT += platformSpeed * platformDirection;
const maxT = segments.length - 1;
if (platformPositionT > maxT + 1) {
platformPositionT = maxT + 1;
platformDirection = -1;
} else if (platformPositionT < 0) {
platformPositionT = 0;
platformDirection = 1;
}
let segmentIndex = Math.floor(platformPositionT);
let tInSegment = platformPositionT - segmentIndex;
if (segmentIndex >= segments.length) {
segmentIndex = segments.length - 1;
tInSegment = 1;
} else if (segmentIndex < 0) {
segmentIndex = 0;
tInSegment = 0;
}
const currentSegment = segments[segmentIndex];
const position = currentSegment.getInterpolatedPoint(tInSegment);
const tangent = currentSegment.getTangent(tInSegment);
return { position, tangent };
}
export function calculateBoundingBox(vertices) {
let minX = Infinity, maxX = -Infinity;
let minY = Infinity, maxY = -Infinity;
let minZ = Infinity, maxZ = -Infinity;
vertices.forEach(vertex => {
if (vertex[0] < minX) minX = vertex[0];
if (vertex[0] > maxX) maxX = vertex[0];
if (vertex[1] < minY) minY = vertex[1];
if (vertex[1] > maxY) maxY = vertex[1];
if (vertex[2] < minZ) minZ = vertex[2];
if (vertex[2] > maxZ) maxZ = vertex[2];
});
return { minX, maxX, minY, maxY, minZ, maxZ };
}
function calculateYAtXZ(x, z, vertices) {
if (vertices.every(v => v.y === vertices[0].y)) {
return vertices[0].y;
}
const [p1, p2, p3] = vertices;
const A = (p2[1] - p1[1]) * (p3[2] - p1[2]) - (p2[2] - p1[2]) * (p3[1] - p1[1]);
const B = (p2[2] - p1[2]) * (p3[0] - p1[0]) - (p2[0] - p1[0]) * (p3[2] - p1[2]);
const C = (p2[0] - p1[0]) * (p3[1] - p1[1]) - (p2[1] - p1[1]) * (p3[0] - p1[0]);
const D = -(A * p1[0] + B * p1[1] + C * p1[2]);
if (B === 0) {
console.error("Plane is vertical, can't compute y from x and z alone.");
return NaN;
}
return -(A * x + C * z + D) / B;
}
// export function getHeightAtPosition(x, z, playerFeetY, absGround) {
// let retVal = absGround;
// console.log("Ground Polygons Structure:", JSON.stringify(groundPolygons, null, 2));
// groundPolygons.forEach(({ vertices, boundingBox }) => {
// if (x >= boundingBox.minX && x <= boundingBox.maxX && z >= boundingBox.minZ && z <= boundingBox.maxZ) {
// const yAtXZ = calculateYAtXZ(x, z, vertices);
// if (!isNaN(yAtXZ) && (yAtXZ >= playerFeetY || playerFeetY - yAtXZ <= 1000)) {
// retVal = yAtXZ;
// }
// }
// });
// return retVal;
// }
export function getHeightAtPosition(x, z, playerFeetY, absGround) {
let retVal = absGround;
// console.log("Checking ground polygons...");
groundPolygons.forEach(({ boundingBox }) => {
if (x >= boundingBox.minX && x <= boundingBox.maxX && z >= boundingBox.minZ && z <= boundingBox.maxZ) {
// console.log("Bounding Box:", boundingBox);
const groundHeight = boundingBox.maxY;
// console.log(groundHeight);
if (groundHeight <= playerFeetY || groundHeight - playerFeetY <= 1000) {
retVal = groundHeight;
}
}
});
return retVal;
}