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

@@ -93,8 +93,8 @@ class BallClient extends Ball implements GraphicComponent {
this._bounceAlgo(collider); this._bounceAlgo(collider);
soundPongArr[ Math.floor(random(0, soundPongArr.length)) ].play(); soundPongArr[ Math.floor(random(0, soundPongArr.length)) ].play();
} }
/* protected _bouncePlayer(collider: Racket) { /* protected _bounceRacket(collider: Racket) {
this._bouncePlayerAlgo(collider); this._bounceRacketAlgo(collider);
soundRoblox.play(); soundRoblox.play();
} */ } */
} }

View File

@@ -81,11 +81,19 @@ function inGameListener(event: MessageEvent)
function gameUpdate(data: ev.EventGameUpdate) function gameUpdate(data: ev.EventGameUpdate)
{ {
console.log("gameUpdate"); console.log("gameUpdate");
gc.playerLeft.pos.y = Math.floor(data.playerLeft.y);
gc.playerRight.pos.y = Math.floor(data.playerRight.y); gc.playerLeft.pos.y = data.playerLeft.y;
gc.ball.pos.x = Math.floor(data.ball.x); gc.playerRight.pos.y = data.playerRight.y;
gc.ball.pos.y = Math.floor(data.ball.y); gc.ball.pos.x = data.ball.x;
gc.ball.speed = Math.floor(data.ball.speed); gc.ball.pos.y = data.ball.y;
gc.ball.speed = data.ball.speed;
gc.ball.dir.x = data.ball.dirX;
gc.ball.dir.y = data.ball.dirY;
// ERROR: Uncaught TypeError: gc.ball.pos.assign is not a function
// Why ?
// gc.ball.pos.assign( data.ball.x, data.ball.y );
repeatInput(data.lastInputId); // server reconciliation repeatInput(data.lastInputId); // server reconciliation
} }

View File

@@ -97,9 +97,19 @@ class GameSession {
const gc = s.components; const gc = s.components;
const update: ev.EventGameUpdate = { const update: ev.EventGameUpdate = {
type: en.EventTypes.gameUpdate, type: en.EventTypes.gameUpdate,
playerLeft: {y: gc.playerLeft.pos.y}, playerLeft: {
playerRight: {y: gc.playerRight.pos.y}, y: gc.playerLeft.pos.y
ball: {x: gc.ball.pos.x, y: gc.ball.pos.y, speed: gc.ball.speed}, },
playerRight: {
y: gc.playerRight.pos.y
},
ball:{
x: gc.ball.pos.x,
y: gc.ball.pos.y,
dirX: gc.ball.dir.x,
dirY: gc.ball.dir.y,
speed: gc.ball.speed
},
lastInputId: 0 lastInputId: 0
}; };
s.playersMap.forEach( (client) => { s.playersMap.forEach( (client) => {

View File

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

View File

@@ -11,25 +11,26 @@ class Rectangle implements Component {
this.width = width; this.width = width;
this.height = height; this.height = height;
} }
collision(collider: Rectangle): boolean { // Collision WIP. To redo collision(collider: Rectangle): boolean {
var myleft = this.pos.x; const thisLeft = this.pos.x;
var myright = this.pos.x + (this.width); const thisRight = this.pos.x + this.width;
var mytop = this.pos.y; const thisTop = this.pos.y;
var mybottom = this.pos.y + (this.height); const thisBottom = this.pos.y + this.height;
var otherleft = collider.pos.x; const colliderLeft = collider.pos.x;
var otherright = collider.pos.x + (collider.width); const colliderRight = collider.pos.x + collider.width;
var othertop = collider.pos.y; const colliderTop = collider.pos.y;
var otherbottom = collider.pos.y + (collider.height); const colliderBottom = collider.pos.y + collider.height;
if ((mybottom < othertop) if ((thisBottom < colliderTop)
|| (mytop > otherbottom) || (thisTop > colliderBottom)
|| (myright < otherleft) || (thisRight < colliderLeft)
|| (myleft > otherright)) { || (thisLeft > colliderRight)) {
return false; return false;
} }
else else {
return true; return true;
} }
} }
}
class MovingRectangle extends Rectangle implements Moving { class MovingRectangle extends Rectangle implements Moving {
dir: Vector = new Vector(0,0); dir: Vector = new Vector(0,0);
@@ -75,7 +76,7 @@ class Ball extends MovingRectangle {
/* Could be more generic, but testing only Racket is enough, /* Could be more generic, but testing only Racket is enough,
because in Pong collider can only be Racket or Wall. */ because in Pong collider can only be Racket or Wall. */
if (collider instanceof Racket) { if (collider instanceof Racket) {
this._bouncePlayer(collider); this._bounceRacket(collider);
} }
else { else {
this._bounceWall(); this._bounceWall();
@@ -93,13 +94,40 @@ class Ball extends MovingRectangle {
protected _bounceWall() { // Should be enough for Wall protected _bounceWall() { // Should be enough for Wall
this.dir.y = this.dir.y * -1; this.dir.y = this.dir.y * -1;
} }
protected _bouncePlayer(collider: Racket) { protected _bounceRacket(racket: Racket) {
this._bouncePlayerAlgo(collider); this._bounceRacketAlgo(racket);
} }
protected _bouncePlayerAlgo(collider: Racket) { // WIP protected _bounceRacketAlgo(racket: Racket) {
// Bounce for Racket need to be more complexe than this
this.speed += this.baseSpeed/20; 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 h = CanvasWidth / CanvasRatio;
export const w_mid = Math.floor(w/2); export const w_mid = Math.floor(w/2);
export const h_mid = Math.floor(h/2); export const h_mid = Math.floor(h/2);
export const pw = Math.floor(w/50); export const pw = Math.floor(w/60);
export const ph = pw*5; export const ph = pw*6;
export const ballSize = pw; export const ballSize = pw;
export const wallSize = Math.floor(w/100); export const wallSize = Math.floor(w/100);
export const playerSpeed = Math.floor(w/1.5); // pixel per second 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 matchStartDelay = 3000; // millisecond
export const newRoundDelay = 1500; // millisecond export const newRoundDelay = 1500; // millisecond