@@ -7,7 +7,7 @@ const CANVAS_WIDTH = 800; |
| 7 | 7 | const CANVAS_HEIGHT = 400; |
| 8 | 8 | |
| 9 | 9 | // Game constants |
| 10 | | -const BALL_SPEED = 6; |
| 10 | +const BALL_SPEED = 5; |
| 11 | 11 | const BALL_RADIUS = 12; |
| 12 | 12 | const PADDLE_WIDTH = 20; |
| 13 | 13 | const PADDLE_HEIGHT = 80; |
@@ -42,11 +42,11 @@ const SPRING_PARTICLE_RATE = 0.3; |
| 42 | 42 | |
| 43 | 43 | // Bop system constants |
| 44 | 44 | const BOP_FORCE = 1.0; // self explanatory. BOP it. |
| 45 | | -const BOP_RANGE = 500; // also self explanatory. TWIST it. |
| 46 | | -const BOP_DURATION = 300; // traversal duration. SHAKE it. |
| 47 | | -const BOP_COOLDOWN = 500; // also also self expl. PULL it. |
| 48 | | -const ANCHOR_RECOIL = 40; // How far the anchor moves backward during bop |
| 49 | | -const BOP_VELOCITY_BOOST = 12; // Initial velocity boost for paddle |
| 45 | +const BOP_RANGE = 500; // also self explanatory. TWIST it. |
| 46 | +const BOP_DURATION = 1000; // traversal duration. SHAKE it. |
| 47 | +const BOP_COOLDOWN = 0; // also also self expl. PULL it. |
| 48 | +const ANCHOR_RECOIL = 60; // How far the anchor moves backward during bop |
| 49 | +const BOP_VELOCITY_BOOST = 5; // Initial velocity boost for paddle |
| 50 | 50 | |
| 51 | 51 | // ============= BOP SYSTEM ============= |
| 52 | 52 | let bopState = { |
@@ -74,8 +74,9 @@ function handleBopInput(keys, aiEnabled, currentTime, leftPaddle, rightPaddle, l |
| 74 | 74 | // Left player bop - use Left Shift for both modes |
| 75 | 75 | let leftBopPressed = keys['Shift'] && !keys['Control']; |
| 76 | 76 | |
| 77 | + // BOP_COOLDOWN controls the minimum time between bops |
| 77 | 78 | if (leftBopPressed && !bopState.left.active && |
| 78 | | - currentTime - bopState.left.lastBopTime > bopState.left.cooldown) { |
| 79 | + currentTime - bopState.left.lastBopTime > BOP_COOLDOWN) { |
| 79 | 80 | activateBop('left', currentTime, leftPaddle, leftSupport, engine, particles); |
| 80 | 81 | } |
| 81 | 82 | |
@@ -83,8 +84,9 @@ function handleBopInput(keys, aiEnabled, currentTime, leftPaddle, rightPaddle, l |
| 83 | 84 | if (!aiEnabled) { |
| 84 | 85 | let rightBopPressed = keys['Enter']; |
| 85 | 86 | |
| 87 | + // BOP_COOLDOWN controls the minimum time between bops |
| 86 | 88 | if (rightBopPressed && !bopState.right.active && |
| 87 | | - currentTime - bopState.right.lastBopTime > bopState.right.cooldown) { |
| 89 | + currentTime - bopState.right.lastBopTime > BOP_COOLDOWN) { |
| 88 | 90 | activateBop('right', currentTime, rightPaddle, rightSupport, engine, particles); |
| 89 | 91 | } |
| 90 | 92 | } |
@@ -95,7 +97,6 @@ function handleBopInput(keys, aiEnabled, currentTime, leftPaddle, rightPaddle, l |
| 95 | 97 | |
| 96 | 98 | function activateBop(side, currentTime, paddle, support, engine, particles) { |
| 97 | 99 | const Body = Matter.Body; |
| 98 | | - const Engine = Matter.Engine; |
| 99 | 100 | |
| 100 | 101 | bopState[side].active = true; |
| 101 | 102 | bopState[side].startTime = currentTime; |
@@ -104,41 +105,40 @@ function activateBop(side, currentTime, paddle, support, engine, particles) { |
| 104 | 105 | // Calculate direction from support to paddle |
| 105 | 106 | let dx = paddle.position.x - support.position.x; |
| 106 | 107 | let dy = paddle.position.y - support.position.y; |
| 107 | | - |
| 108 | + |
| 108 | 109 | // Normalize direction |
| 109 | 110 | let magnitude = Math.sqrt(dx * dx + dy * dy); |
| 110 | 111 | if (magnitude > 0) { |
| 111 | 112 | dx /= magnitude; |
| 112 | 113 | dy /= magnitude; |
| 113 | 114 | |
| 114 | | - // Calculate anchor recoil distance |
| 115 | | - let anchorRecoilDistance = ANCHOR_RECOIL * 0.4; |
| 116 | | - |
| 117 | | - // Move the support BACKWARD (recoil effect) |
| 118 | | - let newSupportX = support.position.x - dx * anchorRecoilDistance; |
| 119 | | - let newSupportY = support.position.y - dy * anchorRecoilDistance; |
| 120 | | - |
| 121 | | - Body.setPosition(support, { x: newSupportX, y: newSupportY }); |
| 122 | | - |
| 123 | | - // Store original support position for recovery |
| 115 | + // ANCHOR_RECOIL now properly controls the recoil distance |
| 116 | + let anchorRecoilDistance = ANCHOR_RECOIL * 0.3; // Can adjust the 0.3 multiplier |
| 117 | + |
| 118 | + // Move support and paddle backward together |
| 119 | + Body.translate(support, { x: -dx * anchorRecoilDistance, y: -dy * anchorRecoilDistance }); |
| 120 | + Body.translate(paddle, { x: -dx * anchorRecoilDistance, y: -dy * anchorRecoilDistance }); |
| 121 | + |
| 122 | + // Remember where the support started so we can ease it back later |
| 124 | 123 | bopState[side].originalPos = { |
| 125 | | - x: support.position.x + dx * anchorRecoilDistance, |
| 124 | + x: support.position.x + dx * anchorRecoilDistance, |
| 126 | 125 | y: support.position.y + dy * anchorRecoilDistance |
| 127 | 126 | }; |
| 128 | 127 | |
| 129 | | - // Set paddle velocity directly for immediate forward thrust |
| 130 | | - let forwardSpeed = (BOP_RANGE / SPRING_LENGTH) * BOP_VELOCITY_BOOST; |
| 128 | + // BOP_VELOCITY_BOOST controls the initial forward velocity |
| 129 | + let forwardSpeed = BOP_VELOCITY_BOOST; |
| 131 | 130 | Body.setVelocity(paddle, { |
| 132 | 131 | x: paddle.velocity.x + dx * forwardSpeed, |
| 133 | 132 | y: paddle.velocity.y + dy * forwardSpeed |
| 134 | 133 | }); |
| 135 | | - |
| 136 | | - // Apply a strong forward force for continued acceleration |
| 134 | + |
| 135 | + // BOP_FORCE controls the sustained forward thrust |
| 136 | + // Combined with BOP_RANGE for the total force applied |
| 137 | 137 | Body.applyForce(paddle, paddle.position, { |
| 138 | | - x: dx * bopState[side].power * BOP_RANGE * 0.1, |
| 139 | | - y: dy * bopState[side].power * BOP_RANGE * 0.1 |
| 138 | + x: dx * BOP_FORCE * BOP_RANGE * 0.002, // Scaled down for Matter.js |
| 139 | + y: dy * BOP_FORCE * BOP_RANGE * 0.002 |
| 140 | 140 | }); |
| 141 | | - |
| 141 | + |
| 142 | 142 | // Create particle burst for visual feedback |
| 143 | 143 | for (let i = 0; i < 5; i++) { |
| 144 | 144 | let angle = Math.atan2(dy, dx) + (Math.random() - 0.5) * 0.5; |
@@ -155,12 +155,9 @@ function activateBop(side, currentTime, paddle, support, engine, particles) { |
| 155 | 155 | type: 'impact' |
| 156 | 156 | }); |
| 157 | 157 | } |
| 158 | | - |
| 159 | | - // Force collision detection update |
| 160 | | - Engine.update(engine, 0); |
| 161 | 158 | } |
| 162 | 159 | |
| 163 | | - console.log(side + " player BOP!"); |
| 160 | + console.log(`${side} player BOP! Duration: ${BOP_DURATION}ms, Cooldown: ${BOP_COOLDOWN}ms`); |
| 164 | 161 | } |
| 165 | 162 | |
| 166 | 163 | function updateBopStates(currentTime, leftSupport, rightSupport, leftPaddle, rightPaddle) { |
@@ -169,17 +166,19 @@ function updateBopStates(currentTime, leftSupport, rightSupport, leftPaddle, rig |
| 169 | 166 | // Update left bop |
| 170 | 167 | if (bopState.left.active) { |
| 171 | 168 | let elapsed = currentTime - bopState.left.startTime; |
| 172 | | - let progress = elapsed / bopState.left.duration; |
| 169 | + let progress = elapsed / BOP_DURATION; // BOP_DURATION controls how long the effect lasts |
| 173 | 170 | |
| 174 | 171 | if (progress >= 1.0) { |
| 175 | 172 | bopState.left.active = false; |
| 176 | 173 | bopState.left.originalPos = null; |
| 174 | + console.log("Left bop ended after", elapsed, "ms"); |
| 177 | 175 | } else { |
| 178 | 176 | if (bopState.left.originalPos) { |
| 179 | 177 | let support = leftSupport; |
| 180 | 178 | let currentX = support.position.x; |
| 181 | 179 | let currentY = support.position.y; |
| 182 | 180 | |
| 181 | + // Smooth return motion with easing |
| 183 | 182 | let returnSpeed = 0.15 * (1 - Math.pow(1 - progress, 3)); |
| 184 | 183 | let newX = currentX + (bopState.left.originalPos.x - currentX) * returnSpeed; |
| 185 | 184 | let newY = currentY + (bopState.left.originalPos.y - currentY) * returnSpeed; |
@@ -191,14 +190,15 @@ function updateBopStates(currentTime, leftSupport, rightSupport, leftPaddle, rig |
| 191 | 190 | } |
| 192 | 191 | } |
| 193 | 192 | |
| 194 | | - // Update right bop |
| 193 | + // Update right bop (same logic) |
| 195 | 194 | if (bopState.right.active) { |
| 196 | 195 | let elapsed = currentTime - bopState.right.startTime; |
| 197 | | - let progress = elapsed / bopState.right.duration; |
| 196 | + let progress = elapsed / BOP_DURATION; // BOP_DURATION controls how long the effect lasts |
| 198 | 197 | |
| 199 | 198 | if (progress >= 1.0) { |
| 200 | 199 | bopState.right.active = false; |
| 201 | 200 | bopState.right.originalPos = null; |
| 201 | + console.log("Right bop ended after", elapsed, "ms"); |
| 202 | 202 | } else { |
| 203 | 203 | if (bopState.right.originalPos) { |
| 204 | 204 | let support = rightSupport; |
@@ -223,6 +223,7 @@ function limitBopRange(support, paddle) { |
| 223 | 223 | let currentDistance = dist(support.position.x, support.position.y, |
| 224 | 224 | paddle.position.x, paddle.position.y); |
| 225 | 225 | |
| 226 | + // BOP_RANGE controls the maximum extension allowed |
| 226 | 227 | let maxDistance = SPRING_LENGTH + BOP_RANGE; |
| 227 | 228 | if (currentDistance > maxDistance) { |
| 228 | 229 | let dx = paddle.position.x - support.position.x; |
@@ -232,11 +233,14 @@ function limitBopRange(support, paddle) { |
| 232 | 233 | dx /= magnitude; |
| 233 | 234 | dy /= magnitude; |
| 234 | 235 | |
| 236 | + // Clamp paddle position to max range |
| 235 | 237 | let newX = support.position.x + dx * maxDistance; |
| 236 | 238 | let newY = support.position.y + dy * maxDistance; |
| 237 | 239 | |
| 238 | 240 | let currentVel = paddle.velocity; |
| 239 | 241 | Body.setPosition(paddle, { x: newX, y: newY }); |
| 242 | + |
| 243 | + // Dampen velocity when hitting the range limit |
| 240 | 244 | Body.setVelocity(paddle, { |
| 241 | 245 | x: currentVel.x * 0.7, |
| 242 | 246 | y: currentVel.y * 0.7 |
@@ -265,6 +269,9 @@ function createSpringPaddleSystem(side, width, height) { |
| 265 | 269 | frictionAir: side === 'left' ? 0.005 : 0.008, |
| 266 | 270 | isSensor: false, |
| 267 | 271 | slop: 0.01, |
| 272 | + // Add rotational physics |
| 273 | + inertia: PADDLE_MASS * 200, // Lower inertia = more rotation |
| 274 | + frictionAngular: 0.02, // Slight angular damping |
| 268 | 275 | render: { |
| 269 | 276 | fillStyle: side === 'left' ? '#00ff88' : '#ff6464' |
| 270 | 277 | } |
@@ -282,7 +289,10 @@ function createSpringPaddleSystem(side, width, height) { |
| 282 | 289 | bodyB: paddle, |
| 283 | 290 | length: SPRING_LENGTH, |
| 284 | 291 | stiffness: SPRING_STIFFNESS, |
| 285 | | - damping: SPRING_DAMPING |
| 292 | + damping: SPRING_DAMPING, |
| 293 | + // Add angular stiffness to create torque from movement |
| 294 | + angularStiffness: 0.01, // Allows paddle to rotate based on spring tension |
| 295 | + render: { visible: false } |
| 286 | 296 | }); |
| 287 | 297 | |
| 288 | 298 | return { support, paddle, spring }; |
@@ -346,10 +356,17 @@ function resetBall(ball, world, width, height) { |
| 346 | 356 | return ball; |
| 347 | 357 | } |
| 348 | 358 | |
| 359 | +// ============= COLLISION ============= |
| 349 | 360 | // ============= COLLISION ============= |
| 350 | 361 | function setupCollisionHandlers(engine, ball, leftPaddle, rightPaddle, particles) { |
| 351 | 362 | const Body = Matter.Body; |
| 352 | 363 | |
| 364 | + // Track collision state to prevent double-triggering |
| 365 | + let collisionState = { |
| 366 | + left: { inCollision: false, lastCollisionTime: 0 }, |
| 367 | + right: { inCollision: false, lastCollisionTime: 0 } |
| 368 | + }; |
| 369 | + |
| 353 | 370 | Matter.Events.on(engine, 'collisionStart', function(event) { |
| 354 | 371 | let pairs = event.pairs; |
| 355 | 372 | |
@@ -361,38 +378,133 @@ function setupCollisionHandlers(engine, ball, leftPaddle, rightPaddle, particles |
| 361 | 378 | |
| 362 | 379 | let paddle = pair.bodyA === ball ? pair.bodyB : pair.bodyA; |
| 363 | 380 | let isLeftPaddle = paddle === leftPaddle; |
| 381 | + let side = isLeftPaddle ? 'left' : 'right'; |
| 382 | + |
| 383 | + // Prevent multiple collisions in quick succession |
| 384 | + let currentTime = Date.now(); |
| 385 | + if (currentTime - collisionState[side].lastCollisionTime < 100) { |
| 386 | + continue; // Skip if we just had a collision |
| 387 | + } |
| 364 | 388 | |
| 389 | + collisionState[side].lastCollisionTime = currentTime; |
| 390 | + |
| 391 | + // Apply bop boost if paddle is currently bopping |
| 365 | 392 | if ((isLeftPaddle && bopState.left.active) || (!isLeftPaddle && bopState.right.active)) { |
| 366 | | - let ballVel = ball.velocity; |
| 367 | | - let paddleVel = paddle.velocity; |
| 393 | + // Get actual collision point from Matter.js |
| 394 | + let collision = pair.collision; |
| 395 | + let contactPoint = collision.supports[0]; |
| 368 | 396 | |
| 369 | | - let boostX = paddleVel.x * 0.5; |
| 370 | | - let boostY = paddleVel.y * 0.5; |
| 397 | + // Check if this is a valid collision |
| 398 | + let isValidCollision = false; |
| 371 | 399 | |
| 372 | | - Body.setVelocity(ball, { |
| 373 | | - x: ballVel.x * 1.3 + boostX, |
| 374 | | - y: ballVel.y * 1.3 + boostY |
| 375 | | - }); |
| 400 | + if (contactPoint) { |
| 401 | + isValidCollision = true; |
| 402 | + } else { |
| 403 | + // Use penetration depth as fallback to avoid false positives |
| 404 | + const depth = collision.depth || 0; |
| 405 | + isValidCollision = depth > 0.5; |
| 406 | + } |
| 376 | 407 | |
| 377 | | - // Create impact particles |
| 378 | | - for (let j = 0; j < IMPACT_PARTICLES; j++) { |
| 379 | | - let angle = Math.random() * Math.PI * 2; |
| 380 | | - let speed = Math.random() * 6 + 2; |
| 408 | + if (isValidCollision) { |
| 409 | + // During bop, ALWAYS apply boost |
| 410 | + let ballVel = ball.velocity; |
| 411 | + let paddleVel = paddle.velocity; |
| 412 | + |
| 413 | + // Calculate the normal collision response first |
| 414 | + let normal = collision.normal; |
| 415 | + let relativeVelocity = { |
| 416 | + x: ballVel.x - paddleVel.x, |
| 417 | + y: ballVel.y - paddleVel.y |
| 418 | + }; |
| 381 | 419 | |
| 382 | | - particles.push({ |
| 383 | | - x: ball.position.x, |
| 384 | | - y: ball.position.y, |
| 385 | | - vx: Math.cos(angle) * speed - ballVel.x * 0.2, |
| 386 | | - vy: Math.sin(angle) * speed - ballVel.y * 0.2, |
| 387 | | - size: Math.random() * 4 + 2, |
| 388 | | - life: PARTICLE_LIFE, |
| 389 | | - maxLife: PARTICLE_LIFE, |
| 390 | | - color: { r: 255, g: Math.random() * 155 + 100, b: Math.random() * 50 + 100 }, |
| 391 | | - type: 'impact' |
| 392 | | - }); |
| 420 | + // Calculate the velocity along the collision normal |
| 421 | + let velocityAlongNormal = relativeVelocity.x * normal.x + relativeVelocity.y * normal.y; |
| 422 | + |
| 423 | + // Only apply boost if ball is approaching paddle |
| 424 | + if (velocityAlongNormal < 0) { |
| 425 | + // Base reflection velocity (enhanced during bop) |
| 426 | + let restitution = 1.5; // Higher restitution during bop |
| 427 | + let impulse = 2 * velocityAlongNormal * restitution; |
| 428 | + |
| 429 | + // Apply the impulse |
| 430 | + let newVelX = ballVel.x - impulse * normal.x; |
| 431 | + let newVelY = ballVel.y - impulse * normal.y; |
| 432 | + |
| 433 | + // Add paddle velocity influence (more during bop) |
| 434 | + let paddleInfluence = 0.6; // Higher influence during bop |
| 435 | + newVelX += paddleVel.x * paddleInfluence; |
| 436 | + newVelY += paddleVel.y * paddleInfluence; |
| 437 | + |
| 438 | + // Add bop boost in the direction of the normal |
| 439 | + let bopBoostMagnitude = 3; // Extra boost during bop |
| 440 | + newVelX += normal.x * bopBoostMagnitude; |
| 441 | + newVelY += normal.y * bopBoostMagnitude; |
| 442 | + |
| 443 | + // Ensure minimum speed after bop |
| 444 | + let newSpeed = Math.sqrt(newVelX * newVelX + newVelY * newVelY); |
| 445 | + let minSpeed = BALL_SPEED * 1.3; // At least 30% faster than normal |
| 446 | + |
| 447 | + if (newSpeed < minSpeed) { |
| 448 | + let scale = minSpeed / newSpeed; |
| 449 | + newVelX *= scale; |
| 450 | + newVelY *= scale; |
| 451 | + } |
| 452 | + |
| 453 | + // Apply the new velocity |
| 454 | + Body.setVelocity(ball, { |
| 455 | + x: newVelX, |
| 456 | + y: newVelY |
| 457 | + }); |
| 458 | + |
| 459 | + // Move ball slightly away from paddle to prevent sticking |
| 460 | + let separation = 2; // pixels |
| 461 | + Body.setPosition(ball, { |
| 462 | + x: ball.position.x + normal.x * separation, |
| 463 | + y: ball.position.y + normal.y * separation |
| 464 | + }); |
| 465 | + |
| 466 | + // Create impact particles |
| 467 | + for (let j = 0; j < IMPACT_PARTICLES * 2; j++) { // More particles for bop |
| 468 | + let angle = Math.atan2(normal.y, normal.x) + (Math.random() - 0.5) * Math.PI; |
| 469 | + let speed = Math.random() * 8 + 4; |
| 470 | + |
| 471 | + particles.push({ |
| 472 | + x: contactPoint ? contactPoint.x : ball.position.x, |
| 473 | + y: contactPoint ? contactPoint.y : ball.position.y, |
| 474 | + vx: Math.cos(angle) * speed, |
| 475 | + vy: Math.sin(angle) * speed, |
| 476 | + size: Math.random() * 5 + 3, |
| 477 | + life: PARTICLE_LIFE, |
| 478 | + maxLife: PARTICLE_LIFE, |
| 479 | + color: { r: 255, g: Math.random() * 155 + 100, b: 50 }, |
| 480 | + type: 'impact' |
| 481 | + }); |
| 482 | + } |
| 483 | + |
| 484 | + console.log(`BOP HIT! Clean bounce. New speed: ${newSpeed.toFixed(1)}, Side: ${side}`); |
| 485 | + } |
| 393 | 486 | } |
| 394 | 487 | } |
| 395 | 488 | } |
| 396 | 489 | } |
| 397 | 490 | }); |
| 491 | + |
| 492 | + // Reset collision state on collision end |
| 493 | + Matter.Events.on(engine, 'collisionEnd', function(event) { |
| 494 | + let pairs = event.pairs; |
| 495 | + |
| 496 | + for (let i = 0; i < pairs.length; i++) { |
| 497 | + let pair = pairs[i]; |
| 498 | + |
| 499 | + if ((pair.bodyA === ball && (pair.bodyB === leftPaddle || pair.bodyB === rightPaddle)) || |
| 500 | + (pair.bodyB === ball && (pair.bodyA === leftPaddle || pair.bodyA === rightPaddle))) { |
| 501 | + |
| 502 | + let paddle = pair.bodyA === ball ? pair.bodyB : pair.bodyA; |
| 503 | + let isLeftPaddle = paddle === leftPaddle; |
| 504 | + let side = isLeftPaddle ? 'left' : 'right'; |
| 505 | + |
| 506 | + collisionState[side].inCollision = false; |
| 507 | + } |
| 508 | + } |
| 509 | + }); |
| 398 | 510 | } |