collision algo with Hugo
+ ball.dir added to EventGameUpdate
This commit is contained in:
@@ -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);
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user