Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Pragyanur committed Jun 26, 2024
1 parent e916b07 commit 9fee549
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions scripts/game3d.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const BALL_SIZE = 16;
const BALL_SIZE = 10;
const SPEED = 10;
let Player1;
let B1;
Expand Down Expand Up @@ -26,7 +26,7 @@ class Ball {
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 - BALL_SIZE / 2) {
if (dist(player.line_1, player.pos_1, this.pos.x, this.pos.y) < player.size - BALL_SIZE / 2) {
this.vel = player.hit(this);
}
}
Expand All @@ -48,31 +48,28 @@ class Player {
this.rot_2 = 0;
this.pos_1 = 0;
this.pos_2 = 0;
this.size = height / 10;
this.size = height / 8;
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);
stroke(255);
shininess(50);
translate(this.line_1, this.pos_1, 0);
rotateZ(this.rot_1);
fill(255,50);
fill(255, 100);
box(0, this.size, this.size);
pop();
}

handleKeys() {
const rotationSpeed = 0.01;
const positionSpeed = 10;
const positionSpeed = 7;
if (keys[UP_ARROW]) this.pos_1 -= positionSpeed;
if (keys[DOWN_ARROW]) this.pos_1 += positionSpeed;
if (keys[LEFT_ARROW]) this.rAcc_1 -= rotationSpeed;
Expand All @@ -84,7 +81,9 @@ class Player {
this.rAcc_1 = constrain(this.rAcc_1, -1, 1);
this.rot_1 += this.rAcc_1;
this.rot_1 = this.rot_1 % PI;

if (this.rAcc_1 != 0) this.rAcc_1 *= 0.9;
if (this.pAcc_1 != 0) this.pAcc_1 *= 0.9;
}

hit(ball) {
Expand All @@ -109,10 +108,9 @@ class Player {

if (shortestDist < BALL_SIZE / 2) {
let reflect = createVector(0, 0);
// projection of velocity on normal
// r=d−2(d⋅n)n
reflect.x = -2 * dotProduct_1 * normal_1.x + initialVelocity.x;
reflect.y = -2 * dotProduct_1 * normal_1.y + initialVelocity.y;
reflect.x = initialVelocity.x - 2 * dotProduct_1 * normal_1.x;
reflect.y = initialVelocity.y - 2 * dotProduct_1 * normal_1.y;
return reflect;
}
return ball.vel;
Expand Down Expand Up @@ -162,7 +160,7 @@ class Background {
plane(width, h / 2);
pop();
// left
fill(255, 0, 0, 70);
fill(100, 0, 0);
noStroke();
push();
translate(-width / 2, -height / 2 + height / 20);
Expand All @@ -175,7 +173,7 @@ class Background {
plane(h / 2, height / 10);
pop();
//right
fill(0, 0, 255, 70);
fill(0, 0, 100);
noStroke();
push();
translate(width / 2, -height / 2 + height / 20);
Expand Down

0 comments on commit 9fee549

Please sign in to comment.