-
Notifications
You must be signed in to change notification settings - Fork 0
/
levelBuilder.js
37 lines (29 loc) · 2.01 KB
/
levelBuilder.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
// levelBuilder.js
// Contains functions that help build the level in the game by placing objects in the scene
import { translateObj, rotateObj} from './utils.js';
import * as renderUtils from './renderUtils.js';
import {calculateBoundingBox} from './groundMechanics.js';
export const groundPolygons = [];
export function placeObj(obj, rot, loc, ego, fovSlider, canvas, pitch, yaw, color, fillShape = false, thickness = 1, groundElement = 0) {
const rotatedObj = rotateObj(obj, rot.angle, rot.axis);
const translatedObj = translateObj(rotatedObj, loc.x, loc.y, loc.z);
const projectedObj = translatedObj.map(corner => renderUtils.projectPoint(corner, ego, fovSlider, canvas, pitch, yaw)).filter(point => point !== null);
renderUtils.drawObj(projectedObj, color, canvas, fillShape, thickness, ego.y);
const boundingBox = calculateBoundingBox(translatedObj);
if (groundElement === 1) groundPolygons.push({ vertices: translatedObj, boundingBox });
}
export function drawGroundSegments(base, grid, ego, canvas, fovSlider, pitch, yaw, dy, startZ, endZ, xOff, yOff = 0, rot = {angle: 0, axis:[0, 1, 0]}) {
const segmentSize = 1000;
for (let z = startZ; z >= endZ; z -= segmentSize) {
placeObj(base, {angle: rot.angle, axis: rot.axis}, {x: xOff, y: yOff, z: z}, ego, fovSlider, canvas, pitch, yaw, "ground", true, 1, 1);
placeObj(grid, {angle: rot.angle, axis: rot.axis}, {x: xOff, y: yOff, z: z}, ego, fovSlider, canvas, pitch, yaw, "black", false, 1, 1);
}
}
export function drawFloatingPlatform(obj, grid, ego, canvas, fovSlider, pitch, yaw, dy, platformData) {
const { position, tangent } = platformData;
const xOff = position.x;
const zOff = position.z;
const angle = -1*Math.atan2(tangent.z, tangent.x);
placeObj(obj, {angle: angle, axis:[0, 1, 0]}, {x: xOff, y: 0, z: zOff}, ego, fovSlider, canvas, pitch, yaw, "#5C4033", true, 1, 1);
placeObj(grid, {angle: angle, axis:[0, 1, 0]}, {x: xOff, y: 0, z: zOff}, ego, fovSlider, canvas, pitch, yaw, "black", false, 1, 1);
}