-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmechanicsGL.js
191 lines (163 loc) · 6.61 KB
/
mechanicsGL.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//mechanicsGL.js
import {updateFloatingPlatformPosition, getHeightAtPosition } from './groundMechanicsGL.js';
import { groundPolygons } from './levelBuilderGL.js';
export let floatingPlatformInfo = { position: { x: 0, y: 0, z: 0 }, tangent: { x: 0, y: 0, z: 0 } };
export function updateMovement(ego, gravity, keysPressed, yaw, speed, deltaTime, groundY, absGround, railPath) {
const pace = keysPressed['shift'] ? speed * 1.5 : speed;
const forwardVector = {
x: Math.sin(yaw),
z: -Math.cos(yaw),
};
const strafeVector = {
x: Math.cos(yaw),
z: Math.sin(yaw),
};
if (keysPressed['w']) {
ego.x += forwardVector.x * pace;
ego.z += forwardVector.z * pace;
}
if (keysPressed['s']) {
ego.x -= forwardVector.x * pace;
ego.z -= forwardVector.z * pace;
}
if (keysPressed['d']) {
ego.x += strafeVector.x * pace;
ego.z += strafeVector.z * pace;
}
if (keysPressed['a']) {
ego.x -= strafeVector.x * pace;
ego.z -= strafeVector.z * pace;
}
const playerFeetY = ego.y - 1900;
// Ground collision detection
if (ego.z < 0) {
groundY = getHeightAtPosition(ego.x, ego.z, playerFeetY, absGround);
if (!isNaN(groundY) && playerFeetY < groundY) {
// Land on the ground
ego.y = groundY + 1900; // snap him to ground
ego.velocityY = 0; // Reset velocity
ego.isJumping = false;
ego.isFreeFalling = false;
ego.onGround = true;
}
}
// Check if player is above ground
if ( playerFeetY > groundY) {
ego.onGround = false;
if (!ego.isJumping) ego.isFreeFalling = true;
} else {
ego.onGround = true;
ego.isFreeFalling = false;
}
if (ego.isJumping) {
jump(ego, gravity, deltaTime); // Gravity is negative
} else if (ego.isFreeFalling) {
freeFall(ego, gravity, groundY, deltaTime);
}
floatingPlatformInfo = updateFloatingPlatformPosition(railPath);
if (ego.onGround) {
const { position, tangent } = floatingPlatformInfo;
const platX = position.x;
const platZ = position.z;
const platformRadius = 3200;
const dx = ego.x - platX;
const dz = ego.z - platZ;
const distanceFromPlatformCenter = Math.sqrt(dx * dx + dz * dz);
console.log(distanceFromPlatformCenter);
if (distanceFromPlatformCenter <= platformRadius) {
if (ego.lastPlatformX === null || ego.lastPlatformZ === null) {
ego.lastPlatformX = platX;
ego.lastPlatformZ = platZ;
}
const deltaX = platX - ego.lastPlatformX;
const deltaZ = platZ - ego.lastPlatformZ;
ego.x += deltaX;
ego.z += deltaZ;
ego.lastPlatformX = platX;
ego.lastPlatformZ = platZ;
} else {
ego.lastPlatformX = null;
ego.lastPlatformZ = null;
}
}
}
export function initiateJump(ego, jumpHeight, gravity) {
if (ego.isJumping || ego.isFreeFalling) return; // Prevent multiple jumps
ego.isJumping = true;
ego.isFreeFalling = false;
ego.velocityY = Math.sqrt(-2 * gravity * jumpHeight); // Initial upward velocity
}
export function jump(ego, gravity, deltaTime) {
if (!ego.isFreeFalling) {
ego.velocityY += gravity * deltaTime;
ego.y += ego.velocityY * deltaTime;
if (ego.velocityY <= 0) {
ego.isJumping = false;
ego.isFreeFalling = true;
}
}
}
export function freeFall(ego, gravity, groundY, deltaTime) {
if (!ego.onGround) {
ego.velocityY += gravity * deltaTime;
ego.y += ego.velocityY * deltaTime;
} else {
ego.y = groundY + 1900;
ego.velocityY = 0;
}
}
export function transformGunMatrix(cameraPosition, yawPitch) {
const modelMatrix = mat4.create();
mat4.translate(modelMatrix, modelMatrix, [cameraPosition[0],cameraPosition[1], cameraPosition[2]]);
const rotationMatrix = mat4.create();
mat4.rotateY(rotationMatrix, rotationMatrix, -yawPitch.yaw); // Yaw
mat4.rotateX(rotationMatrix, rotationMatrix, -yawPitch.pitch); // Pitch
mat4.multiply(modelMatrix, modelMatrix, rotationMatrix);
mat4.translate(modelMatrix, modelMatrix, [ 120, -130, -450]);
return modelMatrix;
}
// export function transformMuzMatrix(cameraPosition, height = 20, yawPitch) {
// const modelMatrix = mat4.create();
// mat4.translate(modelMatrix, modelMatrix, [cameraPosition[0], cameraPosition[1], cameraPosition[2]]);
// const rotationMatrix = mat4.create();
// mat4.rotateY(rotationMatrix, rotationMatrix, -yawPitch.yaw); // Yaw
// mat4.rotateX(rotationMatrix, rotationMatrix, -yawPitch.pitch); // Pitch
// mat4.multiply(modelMatrix, modelMatrix, rotationMatrix);
// mat4.translate(modelMatrix, modelMatrix, [ 120, -130 + height, -450]);
// return modelMatrix;
// }
export function shoot(isCharged, ego, bullets, yawPitch, fireRate, loopTime) {
// const scale = isCharged ? chargedBulletScale : 1;
const scale = 0.7;
const gunBarrelOffset = vec3.fromValues(120, -130, -1750);// this is a bit off need to work on this
const startPosition = vec3.fromValues(ego.x, ego.y, ego.z);
const rotationMatrix = mat4.create();
mat4.rotateY(rotationMatrix, rotationMatrix, -yawPitch.yaw);
mat4.rotateX(rotationMatrix, rotationMatrix, -yawPitch.pitch);
vec3.transformMat4(startPosition, gunBarrelOffset, rotationMatrix);
vec3.add(startPosition, startPosition, [ego.x, ego.y, ego.z]);
// const direction = vec3.fromValues( (0.005/fireRate) * Math.sin(loopTime), 0, -1);
const direction = vec3.fromValues( 0, 0, -1);
vec3.transformMat4(direction, direction, rotationMatrix);
vec3.normalize(direction, direction);
bullets.push({
position: { x: startPosition[0], y: startPosition[1], z: startPosition[2] },
direction: { x: direction[0], y: direction[1], z: direction[2] },
scale,
rotationMatrix: mat4.clone(rotationMatrix)
});
}
export function updateBullets(bullets, bulletSpeed, maxDistance, ego) {
bullets.forEach((bullet, index) => {
bullet.position.x += bullet.direction.x * bulletSpeed;
bullet.position.y += bullet.direction.y * bulletSpeed;
bullet.position.z += bullet.direction.z * bulletSpeed;
if (
Math.abs(bullet.position.x - ego.x) > maxDistance ||
Math.abs(bullet.position.y - ego.y) > maxDistance ||
Math.abs(bullet.position.z - ego.z) > maxDistance
) {
bullets.splice(index, 1);
}
});
}