diff --git a/scripts/game3d.js b/scripts/game3d.js index 0ad6568..173addc 100644 --- a/scripts/game3d.js +++ b/scripts/game3d.js @@ -1,5 +1,5 @@ const BALL_SIZE = 16; -const SPEED = 5; +const SPEED = 10; let Player1; let B1; let ball; @@ -16,17 +16,17 @@ class Ball { this.vel.normalize(); this.pos.x += this.vel.x * this.fac; this.pos.y += this.vel.y * this.fac; - if (this.pos.x < -width / 2 || this.pos.x > width / 2) this.vel.x = -this.vel.x; - if (this.pos.y < -height / 2 || this.pos.y > height / 2) this.vel.y = -this.vel.y; + if (this.pos.x < -width / 2 + BALL_SIZE / 2 || this.pos.x > width / 2 - BALL_SIZE / 2) this.vel.x = -this.vel.x; + if (this.pos.y < -height / 2 + BALL_SIZE / 2 || this.pos.y > height / 2 - BALL_SIZE / 2) this.vel.y = -this.vel.y; if (this.fac != 5) this.fac += this.fac > SPEED ? -1 : 1; // debug - if (this.pos.x < -width / 2) this.pos.x++; - if (this.pos.x > width / 2) this.pos.x--; - if (this.pos.y < -height / 2) this.pos.y++; - if (this.pos.y > height / 2) this.pos.y--; + if (this.pos.x < -width / 2) this.pos.x += 5; + if (this.pos.x > width / 2) this.pos.x -= 5; + if (this.pos.y < -height / 2) this.pos.y += 5; + if (this.pos.y > height / 2) this.pos.y -= 5; } reflect(player) { - if (dist(player.line, player.pos, this.pos.x, this.pos.y) <= player.size) { + if (dist(player.line, player.pos, this.pos.x, this.pos.y) < player.size - BALL_SIZE / 2) { this.vel = player.hit(this); } } @@ -39,54 +39,51 @@ class Ball { fill(200); sphere(BALL_SIZE); pop(); - push(); - strokeWeight(5); - stroke(200, 30, 30); - line(this.pos.x, this.pos.y, this.pos.x + this.vel.x * 40, this.pos.y + this.vel.y * 40); - pop(); } } class Player { constructor() { - this.rot = 0; - this.rot2 = 0; - this.pos = 0; - this.pos2 = 0; + this.rot_1 = 0; + this.rot_2 = 0; + this.pos_1 = 0; + this.pos_2 = 0; this.size = height / 10; - this.line = width / 2 - this.size; - this.line2 = -width / 2 + this.size; + this.line_1 = width / 2 - this.size; + this.line_2 = -width / 2 + this.size; this.rAcc_1 = 0; this.rAcc_2 = 0; + this.pAcc_1 = 0; + this.pAcc_2 = 0; } show() { push(); ambientLight(80); specularMaterial(250); - noStroke(); + stroke(255); shininess(50); - translate(this.line, this.pos, 0); - rotateZ(this.rot); - fill(255); - box(this.size / 20, this.size, this.size); + translate(this.line_1, this.pos_1, 0); + rotateZ(this.rot_1); + fill(255,50); + box(0, this.size, this.size); pop(); } handleKeys() { const rotationSpeed = 0.01; - const positionSpeed = 5; - if (keys[UP_ARROW]) this.pos -= positionSpeed; - if (keys[DOWN_ARROW]) this.pos += positionSpeed; + const positionSpeed = 10; + if (keys[UP_ARROW]) this.pos_1 -= positionSpeed; + if (keys[DOWN_ARROW]) this.pos_1 += positionSpeed; if (keys[LEFT_ARROW]) this.rAcc_1 -= rotationSpeed; if (keys[RIGHT_ARROW]) this.rAcc_1 += rotationSpeed; } update() { - this.pos = constrain(this.pos, -height / 2 + this.size / 2, height / 2 - this.size / 2); + this.pos_1 = constrain(this.pos_1, -height / 2 + this.size / 2, height / 2 - this.size / 2); this.rAcc_1 = constrain(this.rAcc_1, -1, 1); - this.rot += this.rAcc_1; - this.rot = this.rot % PI; + this.rot_1 += this.rAcc_1; + this.rot_1 = this.rot_1 % PI; if (this.rAcc_1 != 0) this.rAcc_1 *= 0.9; } @@ -94,27 +91,28 @@ class Player { const bx = ball.pos.x; const by = ball.pos.y; - let A = createVector(this.line - (this.size / 2) * sin(-this.rot), this.pos - (this.size / 2) * cos(-this.rot)); - let B = createVector(this.line + (this.size / 2) * sin(-this.rot), this.pos + (this.size / 2) * cos(-this.rot)); - let normal = createVector(this.line - (this.size / 2) * cos(this.rot), this.pos - (this.size / 2) * sin(this.rot)); - let normal2 = createVector(this.line + (this.size / 2) * cos(this.rot), this.pos + (this.size / 2) * sin(this.rot)); + let A = createVector(this.line_1 - (this.size / 2) * sin(-this.rot_1), this.pos_1 - (this.size / 2) * cos(-this.rot_1)); + let B = createVector(this.line_1 + (this.size / 2) * sin(-this.rot_1), this.pos_1 + (this.size / 2) * cos(-this.rot_1)); + let normal_1 = createVector(-this.size * cos(this.rot_1) / 2, -this.size * sin(this.rot_1) / 2); + let normal_2 = createVector(-this.size * cos(this.rot_2) / 2, this.size * sin(this.rot_2) / 2); let initialVelocity = createVector(ball.vel.x, ball.vel.y); let shortestDist = abs((A.y - B.y) * bx - (A.x - B.x) * by + (A.x * B.y - A.y * B.x)) / sqrt((A.y - B.y) ** 2 + (A.x - B.x) ** 2); - line(this.line, this.pos, normal.x, normal.y); + line(this.line_1, this.pos_1, this.line_1 + normal_1.x, this.pos_1 + normal_1.y); - normal.normalize(); - normal2.normalize(); + normal_1.normalize(); + normal_2.normalize(); initialVelocity.normalize(); - let dot_product = dotProd(initialVelocity, normal); + let dotProduct_1 = dotProd(initialVelocity, normal_1); + let dotProduct_2 = dotProd(initialVelocity, normal_2); - if (shortestDist <= BALL_SIZE / 2) { + if (shortestDist < BALL_SIZE / 2) { let reflect = createVector(0, 0); // projection of velocity on normal // r=d−2(d⋅n)n - reflect.x = -2 * dot_product * normal.x + initialVelocity.x; - reflect.y = -2 * dot_product * normal.y + initialVelocity.y; + reflect.x = -2 * dotProduct_1 * normal_1.x + initialVelocity.x; + reflect.y = -2 * dotProduct_1 * normal_1.y + initialVelocity.y; return reflect; } return ball.vel; @@ -136,7 +134,6 @@ function keyReleased() { class Background { constructor(platform) { this.platformSize = platform; - } show() { let h = 4 * height / 5; @@ -194,15 +191,21 @@ class Background { // left goal stroke(255); strokeWeight(5); - fill(0, 50); + noFill(); push(); translate(-width / 2, 0); - box(1, h, h / 2); + box(0, h, h / 2); pop(); // right goal push(); translate(width / 2, 0); - box(1, h, h / 2); + box(0, h, h / 2); + pop(); + // boundary + push(); + noFill(); + translate(0, 0, h / 4); + box(width, height, 0); pop(); } }