collision algo with Hugo

+ ball.dir added to EventGameUpdate
This commit is contained in:
LuckyLaszlo
2022-11-25 14:54:17 +01:00
parent 3474d54a2b
commit 4aafbac1a5
6 changed files with 93 additions and 37 deletions

View File

@@ -26,9 +26,19 @@ class EventMatchmakingComplete extends ServerEvent {
}
class EventGameUpdate extends ServerEvent {
playerLeft = {y: 0};
playerRight = {y: 0};
ball = {x: 0, y: 0, speed: 0};
playerLeft = {
y: 0
};
playerRight = {
y: 0
};
ball = {
x: 0,
y: 0,
dirX: 0,
dirY: 0,
speed: 0
};
lastInputId = 0;
constructor() { // TODO: constructor that take GameComponentsServer maybe ?
super(en.EventTypes.gameUpdate);

View File

@@ -11,23 +11,24 @@ class Rectangle implements Component {
this.width = width;
this.height = height;
}
collision(collider: Rectangle): boolean { // Collision WIP. To redo
var myleft = this.pos.x;
var myright = this.pos.x + (this.width);
var mytop = this.pos.y;
var mybottom = this.pos.y + (this.height);
var otherleft = collider.pos.x;
var otherright = collider.pos.x + (collider.width);
var othertop = collider.pos.y;
var otherbottom = collider.pos.y + (collider.height);
if ((mybottom < othertop)
|| (mytop > otherbottom)
|| (myright < otherleft)
|| (myleft > otherright)) {
collision(collider: Rectangle): boolean {
const thisLeft = this.pos.x;
const thisRight = this.pos.x + this.width;
const thisTop = this.pos.y;
const thisBottom = this.pos.y + this.height;
const colliderLeft = collider.pos.x;
const colliderRight = collider.pos.x + collider.width;
const colliderTop = collider.pos.y;
const colliderBottom = collider.pos.y + collider.height;
if ((thisBottom < colliderTop)
|| (thisTop > colliderBottom)
|| (thisRight < colliderLeft)
|| (thisLeft > colliderRight)) {
return false;
}
else
else {
return true;
}
}
}
@@ -75,7 +76,7 @@ class Ball extends MovingRectangle {
/* Could be more generic, but testing only Racket is enough,
because in Pong collider can only be Racket or Wall. */
if (collider instanceof Racket) {
this._bouncePlayer(collider);
this._bounceRacket(collider);
}
else {
this._bounceWall();
@@ -93,13 +94,40 @@ class Ball extends MovingRectangle {
protected _bounceWall() { // Should be enough for Wall
this.dir.y = this.dir.y * -1;
}
protected _bouncePlayer(collider: Racket) {
this._bouncePlayerAlgo(collider);
protected _bounceRacket(racket: Racket) {
this._bounceRacketAlgo(racket);
}
protected _bouncePlayerAlgo(collider: Racket) { // WIP
// Bounce for Racket need to be more complexe than this
protected _bounceRacketAlgo(racket: Racket) {
this.speed += this.baseSpeed/20;
this.dir.x = this.dir.x * -1;
let x = this.dir.x * -1;
const angleFactorDegree = 60;
const angleFactor = angleFactorDegree / 90;
const racketHalf = racket.height/2;
const ballMid = this.pos.y + this.height/2;
const racketMid = racket.pos.y + racketHalf;
let impact = ballMid - racketMid;
const horizontalMargin = racketHalf * 0.10;
if (impact < horizontalMargin && impact > -horizontalMargin) {
impact = 0;
}
else if (impact > 0) {
impact = impact - horizontalMargin;
}
else if (impact < 0) {
impact = impact + horizontalMargin;
}
let y = impact / (racketHalf - horizontalMargin) * angleFactor;
this.dir.assign(x, y);
/*
// Normalize Vector (for consistency in speed independent of direction)
const normalizationFactor = Math.abs(y) + Math.abs(x);
this.dir.assign(x/normalizationFactor, y/normalizationFactor);
*/
}
}

View File

@@ -7,12 +7,12 @@ export const w = CanvasWidth;
export const h = CanvasWidth / CanvasRatio;
export const w_mid = Math.floor(w/2);
export const h_mid = Math.floor(h/2);
export const pw = Math.floor(w/50);
export const ph = pw*5;
export const pw = Math.floor(w/60);
export const ph = pw*6;
export const ballSize = pw;
export const wallSize = Math.floor(w/100);
export const playerSpeed = Math.floor(w/1.5); // pixel per second
export const ballSpeed = Math.floor(w/1.5); // pixel per second
export const ballSpeed = Math.floor(w/2); // pixel per second
export const matchStartDelay = 3000; // millisecond
export const newRoundDelay = 1500; // millisecond