about summary refs log tree commit diff stats
path: root/html/space/physics.js
diff options
context:
space:
mode:
Diffstat (limited to 'html/space/physics.js')
-rw-r--r--html/space/physics.js253
1 files changed, 253 insertions, 0 deletions
diff --git a/html/space/physics.js b/html/space/physics.js
new file mode 100644
index 0000000..d4dfe55
--- /dev/null
+++ b/html/space/physics.js
@@ -0,0 +1,253 @@
+// Physics module for handling movement and collisions
+import { inputState } from './input.js';
+import { createProjectile } from './gameState.js';
+
+// Constants
+const MAX_THRUST = 0.5;        // Reduced from 2
+const THRUST_ACCELERATION = 0.01;  // Reduced from 0.05
+const DECELERATION = 0.001;    // Reduced from 0.01
+const BASE_ROTATION_SPEED = 0.001;  // Reduced from 0.005
+const ROTATION_ACCELERATION = 0.0005; // Reduced from 0.002
+const ROTATION_DECELERATION = 0.0002; // Reduced from 0.001
+const MOUSE_SENSITIVITY = 0.03; // Increased from 0.01 for sharper turns
+const MAX_SPEED = 1.0; // Maximum speed in any direction
+
+// Weapon constants
+export const PRIMARY_COOLDOWN = 100; // ms between primary shots
+export const SECONDARY_COOLDOWN = 2000; // ms between secondary shots
+export const PRIMARY_BURST_COUNT = 3; // Number of shots in primary burst
+export const PRIMARY_BURST_DELAY = 50; // ms between burst shots
+
+// Player state
+let playerState = {
+    position: { x: 0, y: 0, z: 0 },
+    velocity: { x: 0, y: 0, z: 0 },
+    rotation: { x: 0, y: 0 },
+    thrust: 0,
+    strafe: 0,
+    weapons: {
+        primary: {
+            lastFired: 0,
+            burstCount: 0,
+            burstTimer: 0
+        },
+        secondary: {
+            lastFired: 0
+        }
+    }
+};
+
+// Initialize physics
+export function initPhysics() {
+    // Reset player state
+    playerState.position = { x: 0, y: 0, z: 0 };
+    playerState.velocity = { x: 0, y: 0, z: 0 };
+    playerState.rotation = { x: 0, y: 0 };
+    playerState.thrust = 0;
+    playerState.strafe = 0;
+}
+
+// Helper function to limit speed in a direction
+function limitSpeed(velocity, maxSpeed) {
+    const speed = Math.sqrt(velocity.x * velocity.x + velocity.y * velocity.y + velocity.z * velocity.z);
+    if (speed > maxSpeed) {
+        const scale = maxSpeed / speed;
+        velocity.x *= scale;
+        velocity.y *= scale;
+        velocity.z *= scale;
+    }
+}
+
+// Update player controls
+export function updatePlayerControls(controls) {
+    // Handle thrust (space bar)
+    if (controls.thrust) {
+        playerState.thrust = Math.min(playerState.thrust + THRUST_ACCELERATION, MAX_THRUST);
+    } else {
+        // Apply deceleration when no thrust input
+        if (playerState.thrust > 0) {
+            playerState.thrust = Math.max(playerState.thrust - DECELERATION, 0);
+        }
+    }
+
+    // Handle vertical strafing (W/S)
+    if (controls.up) {
+        playerState.verticalStrafe = Math.min(playerState.verticalStrafe + THRUST_ACCELERATION, MAX_THRUST);
+    } else if (controls.down) {
+        playerState.verticalStrafe = Math.max(playerState.verticalStrafe - THRUST_ACCELERATION, -MAX_THRUST);
+    } else {
+        // Apply deceleration when no vertical strafe input
+        if (playerState.verticalStrafe > 0) {
+            playerState.verticalStrafe = Math.max(playerState.verticalStrafe - DECELERATION, 0);
+        } else if (playerState.verticalStrafe < 0) {
+            playerState.verticalStrafe = Math.min(playerState.verticalStrafe + DECELERATION, 0);
+        }
+    }
+
+    // Handle horizontal strafing (A/D)
+    if (controls.left) {
+        playerState.horizontalStrafe = Math.min(playerState.horizontalStrafe + THRUST_ACCELERATION, MAX_THRUST);
+    } else if (controls.right) {
+        playerState.horizontalStrafe = Math.max(playerState.horizontalStrafe - THRUST_ACCELERATION, -MAX_THRUST);
+    } else {
+        // Apply deceleration when no horizontal strafe input
+        if (playerState.horizontalStrafe > 0) {
+            playerState.horizontalStrafe = Math.max(playerState.horizontalStrafe - DECELERATION, 0);
+        } else if (playerState.horizontalStrafe < 0) {
+            playerState.horizontalStrafe = Math.min(playerState.horizontalStrafe + DECELERATION, 0);
+        }
+    }
+
+    // Handle mouse-based rotation with smoothing
+    const targetRotationY = controls.mouseX * MOUSE_SENSITIVITY;
+    const targetRotationX = controls.mouseY * MOUSE_SENSITIVITY;
+    
+    // Smooth rotation using lerp with faster response
+    playerState.rotation.y += (targetRotationY - playerState.rotation.y) * 0.2;
+    playerState.rotation.x += (targetRotationX - playerState.rotation.x) * 0.2;
+
+    // Clamp pitch rotation
+    playerState.rotation.x = Math.max(-Math.PI/2, Math.min(Math.PI/2, playerState.rotation.x));
+
+    // Handle weapons with cooldowns
+    const currentTime = Date.now();
+    
+    // Primary weapon (burst fire)
+    if (controls.fire) {
+        const primary = playerState.weapons.primary;
+        if (currentTime - primary.lastFired >= PRIMARY_COOLDOWN && primary.burstCount === 0) {
+            primary.burstCount = PRIMARY_BURST_COUNT;
+            primary.burstTimer = currentTime;
+            firePrimaryWeapon();
+            primary.lastFired = currentTime;
+        }
+    }
+
+    // Secondary weapon (single shot with cooldown)
+    if (controls.secondary && currentTime - playerState.weapons.secondary.lastFired >= SECONDARY_COOLDOWN) {
+        fireSecondaryWeapon();
+        playerState.weapons.secondary.lastFired = currentTime;
+    }
+
+    // Handle burst fire timing
+    const primary = playerState.weapons.primary;
+    if (primary.burstCount > 0 && currentTime - primary.burstTimer >= PRIMARY_BURST_DELAY) {
+        firePrimaryWeapon();
+        primary.burstCount--;
+        primary.burstTimer = currentTime;
+    }
+}
+
+// Update physics
+export function updatePhysics(deltaTime) {
+    // Calculate forward and right vectors based on rotation
+    const forward = {
+        x: Math.sin(playerState.rotation.y) * Math.cos(playerState.rotation.x),
+        y: -Math.sin(playerState.rotation.x),
+        z: Math.cos(playerState.rotation.y) * Math.cos(playerState.rotation.x)
+    };
+
+    const right = {
+        x: Math.cos(playerState.rotation.y),
+        y: 0,
+        z: -Math.sin(playerState.rotation.y)
+    };
+
+    const up = { x: 0, y: 1, z: 0 };
+
+    // Apply thrust in forward direction
+    const thrustVelocity = {
+        x: forward.x * playerState.thrust * deltaTime,
+        y: forward.y * playerState.thrust * deltaTime,
+        z: forward.z * playerState.thrust * deltaTime
+    };
+
+    // Apply horizontal strafe
+    const horizontalStrafeVelocity = {
+        x: right.x * playerState.horizontalStrafe * deltaTime,
+        y: 0,
+        z: right.z * playerState.horizontalStrafe * deltaTime
+    };
+
+    // Apply vertical strafe
+    const verticalStrafeVelocity = {
+        x: 0,
+        y: up.y * playerState.verticalStrafe * deltaTime,
+        z: 0
+    };
+
+    // Add velocities
+    playerState.velocity.x += thrustVelocity.x + horizontalStrafeVelocity.x + verticalStrafeVelocity.x;
+    playerState.velocity.y += thrustVelocity.y + horizontalStrafeVelocity.y + verticalStrafeVelocity.y;
+    playerState.velocity.z += thrustVelocity.z + horizontalStrafeVelocity.z + verticalStrafeVelocity.z;
+
+    // Limit total speed
+    limitSpeed(playerState.velocity, MAX_SPEED);
+
+    // Apply velocity to position
+    playerState.position.x += playerState.velocity.x * deltaTime;
+    playerState.position.y += playerState.velocity.y * deltaTime;
+    playerState.position.z += playerState.velocity.z * deltaTime;
+
+    // Apply friction/drag
+    const drag = 0.99;
+    playerState.velocity.x *= drag;
+    playerState.velocity.y *= drag;
+    playerState.velocity.z *= drag;
+}
+
+// Weapon firing
+function firePrimaryWeapon() {
+    const forward = {
+        x: Math.sin(playerState.rotation.y) * Math.cos(playerState.rotation.x),
+        y: -Math.sin(playerState.rotation.x),
+        z: Math.cos(playerState.rotation.y) * Math.cos(playerState.rotation.x)
+    };
+
+    createProjectile({
+        position: { ...playerState.position },
+        velocity: {
+            x: forward.x * 10 + playerState.velocity.x,
+            y: forward.y * 10 + playerState.velocity.y,
+            z: forward.z * 10 + playerState.velocity.z
+        },
+        type: 'primary'
+    });
+}
+
+function fireSecondaryWeapon() {
+    const forward = {
+        x: Math.sin(playerState.rotation.y) * Math.cos(playerState.rotation.x),
+        y: -Math.sin(playerState.rotation.x),
+        z: Math.cos(playerState.rotation.y) * Math.cos(playerState.rotation.x)
+    };
+
+    createProjectile({
+        position: { ...playerState.position },
+        velocity: {
+            x: forward.x * 5 + playerState.velocity.x,
+            y: forward.y * 5 + playerState.velocity.y,
+            z: forward.z * 5 + playerState.velocity.z
+        },
+        type: 'secondary'
+    });
+}
+
+// Get current player state
+export function getPlayerState() {
+    return playerState;
+}
+
+// Get weapon cooldown states
+export function getWeaponStates() {
+    const currentTime = Date.now();
+    return {
+        primary: {
+            cooldown: Math.max(0, PRIMARY_COOLDOWN - (currentTime - playerState.weapons.primary.lastFired)),
+            burstCount: playerState.weapons.primary.burstCount
+        },
+        secondary: {
+            cooldown: Math.max(0, SECONDARY_COOLDOWN - (currentTime - playerState.weapons.secondary.lastFired))
+        }
+    };
+} 
\ No newline at end of file
mation' href='/ahoang/Nim/commit/compiler/pretty.nim?h=devel&id=fd2a808266f8d4a66b2921c5bc5543d78c92a633'>fd2a80826 ^
2df9b442c ^
fd2a80826 ^
6810a0e3e ^
6810a0e3e ^

2df9b442c ^







6810a0e3e ^





e5be2e9f9 ^


b731e6ef1 ^
e5be2e9f9 ^
6810a0e3e ^







28ad262a4 ^







6810a0e3e ^
b731e6ef1 ^
2df9b442c ^



b731e6ef1 ^
1101a40f9 ^
2df9b442c ^
b731e6ef1 ^



438703f59 ^

2df9b442c ^




















92b8fac94 ^
2df9b442c ^








2df9b442c ^
1101a40f9 ^

2df9b442c ^



































6810a0e3e ^


805959378 ^
e5be2e9f9 ^
ef975d277 ^
6810a0e3e ^
ef975d277 ^
805959378 ^

6810a0e3e ^



2df9b442c ^
28ad262a4 ^

805959378 ^
2df9b442c ^
6810a0e3e ^




d640121ce ^

2df9b442c ^

6810a0e3e ^


30bb68d48 ^

6810a0e3e ^
731c6f908 ^




805959378 ^
6810a0e3e ^

2df9b442c ^


438703f59 ^
2df9b442c ^
1101a40f9 ^
2df9b442c ^










a427648c4 ^






2df9b442c ^







6810a0e3e ^
2df9b442c ^




ef975d277 ^




b731e6ef1 ^
ef975d277 ^
805959378 ^

731c6f908 ^









ef975d277 ^
6810a0e3e ^
ef975d277 ^
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325