From: Elijah Cohen Date: Thu, 2 May 2019 22:53:32 +0000 (-0500) Subject: fixt a bunch, but the angle of reflection is still wrong... X-Git-Url: https://git.eli173.com/?a=commitdiff_plain;h=d975d3a934594cda181e422496eedb02643c45af;p=pong_br fixt a bunch, but the angle of reflection is still wrong... --- diff --git a/server/collisions.js b/server/collisions.js index 1501320..dba68d2 100644 --- a/server/collisions.js +++ b/server/collisions.js @@ -49,14 +49,15 @@ var segments_intersect = function(e1, e2) { var x_val = ((x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)); var y_val = ((x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4))/((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4)); - if(Math.min(x1, x2) < x_val && x_val < Math.max(x1, x2)) - return false; - if(Math.min(y1, y2) < y_val && y_val < Math.max(y1, y2)) - return false; - if(Math.min(x3, x4) < x_val && x_val < Math.max(x3, x4)) - return false; - if(Math.min(y3, y4) < y_val && y_val < Math.max(y3, y4)) - return false; + + if(x_val < Math.min(x1,x2)) return false; + if(x_val > Math.max(x1,x2)) return false; + if(y_val < Math.min(y1,y2)) return false; + if(y_val > Math.max(y1,y2)) return false; + if(x_val < Math.min(x3,x4)) return false; + if(x_val > Math.max(x3,x4)) return false; + if(y_val < Math.min(y3,y4)) return false; + if(y_val > Math.max(y3,y4)) return false; return true; } @@ -66,16 +67,17 @@ var handleWalls = function(ball, walls) { // modifies ball's velocity if it encounters an actual collision // wall is an endpoints for(var wall of walls) { - var next_spot = new Coord(ball.coord.x + ball.dx, ball.coord.y + ball.dy); + var next_spot = new Coord(ball.coord.x + ball.dx/c.FPS, ball.coord.y + ball.dy/c.FPS); if(segments_intersect(wall, new Endpoints(ball.coord, next_spot))) { //there's a collision - console.log("int"); var wall_normal = new Coord((wall.f.x+wall.s.x)/2, (wall.f.y+wall.s.y)/2); // given by the midpoint var normal_angle = Math.atan2(wall_normal.x, wall_normal.y); var vel_vec = new Coord(ball.dx, ball.dy); vel_vec.rotate(-normal_angle); - vel_vec.x = -vel_vec.x; // i'm fairly certain it's x... + vel_vec.y = -vel_vec.y; // i'm fairly certain it's x... vel_vec.rotate(normal_angle); + ball.dx = vel_vec.x; + ball.dy = vel_vec.y; ball.speed_up(); return true; } @@ -88,26 +90,28 @@ var handlePaddles = function(ball, lzs, paddles) { // same, but with paddles and speed movement bonus // i iterate over lzs, get matching paddle for(var lz of lzs) { - var paddle = this.paddles.find(p => p.id==lz.id); + var paddle = paddles.find(p => p.id==lz.id); if(paddle === undefined) { // this is badd... console.log("paddle not found..."); return false; } var wall = paddle.getPaddlePoints(lz); // - var next_spot = new Coord(ball.coord.x + ball.dx, ball.coord.y + ball.dy); - if(segments_intersect(walls, new Endpoints(ball.coord, next_spot))) { + var next_spot = new Coord(ball.coord.x + ball.dx/c.FPS, ball.coord.y + ball.dy/c.FPS); + if(segments_intersect(wall, new Endpoints(ball.coord, next_spot))) { //there's a collision var wall_normal = new Coord((wall.f.x+wall.s.x)/2, (wall.f.y+wall.s.y)/2); // given by the midpoint var normal_angle = Math.atan2(wall_normal.x, wall_normal.y); var vel_vec = new Coord(ball.dx, ball.dy); vel_vec.rotate(-normal_angle); - vel_vec.x = -vel_vec.x; // i'm fairly certain it's x... + vel_vec.y = -vel_vec.y; // i'm fairly certain it's x... if(paddle.direction == 'u') vel_vec.y += c.PADDLE_MVT_BONUS; else if(paddle.direction == 'd') vel_vec.y -= c.PADDLE_MVT_BONUS; vel_vec.rotate(normal_angle); + ball.dx = vel_vec.x; + ball.dy = vel_vec.y; ball.speed_up(); return true; } @@ -116,4 +120,4 @@ var handlePaddles = function(ball, lzs, paddles) { } -module.exports = {handleWalls: handleWalls, handlePaddles, handlePaddles}; +module.exports = {handleWalls: handleWalls, handlePaddles, handlePaddles, segments_intersect: segments_intersect}; diff --git a/server/constants.js b/server/constants.js index a5fdce4..28e8b73 100644 --- a/server/constants.js +++ b/server/constants.js @@ -1,10 +1,12 @@ +var mspf = 100; var c = { // matchmaker WS_PORT: 6789, - NUM_PLAYERS: 8, - MS_PER_FRAME: 100, + NUM_PLAYERS: 4, + MS_PER_FRAME: mspf, + FPS: 1000/mspf, WAIT_TIME: 60000, // 1 minute MAX_GAMES: 5, // the most games allowed to go on at once, to be tweaked as needed for purposes // gamestate diff --git a/server/endpoints.js b/server/endpoints.js index bc08424..f3641b2 100644 --- a/server/endpoints.js +++ b/server/endpoints.js @@ -17,6 +17,10 @@ var AnglePair = function(f,s,id) { this.id = id; } +Endpoints.prototype.getLength = function() { + return Math.sqrt(this.f.dist2(this.s)); +} + Endpoints.prototype.getAngles = function() { // gets an anglepair for the endpoints. does assume the first angle is before second var fst = Math.atan2(this.f.y, this.f.x); diff --git a/server/gamestate.js b/server/gamestate.js index 52f63dd..89a40fc 100644 --- a/server/gamestate.js +++ b/server/gamestate.js @@ -40,12 +40,6 @@ GameState.prototype.update = function(inputs) { for(var i=0;i (e.id != -1)); @@ -56,16 +50,18 @@ GameState.prototype.update = function(inputs) { var collided = false; // first, see if the ball is moving towards the center (should solve most problems with things var currd2 = ball.coord.dist2origin(); - var nextd2 = new Coord(ball.coord.x + ball.dx, ball.coord.y + ball.dy).dist2origin(); + var nextd2 = new Coord(ball.coord.x + ball.dx/c.FPS, ball.coord.y + ball.dy/c.FPS).dist2origin(); if(nextd2 < currd2) continue; // below here probably requires changes // (check the fixt edges first, then the paddles I guess - if(collisions.handleWalls(ball, walls)) + if(collisions.handleWalls(ball, walls)) { continue; - if(collisions.handlePaddles(balls, livingzones, this.paddles)) + } + if(collisions.handlePaddles(ball, livingzones, this.paddles)) { continue; + } // i put that last continue but there's nothing left to do... } @@ -112,6 +108,11 @@ GameState.prototype.update = function(inputs) { if(this.balls.length < (this.numPlayers-this.dead.length)-1) { this.balls.push(new Ball()); } + //move the balls + for(var ball of this.balls) { + ball.coord.x += ball.dx/c.FPS; + ball.coord.y += ball.dy/c.FPS; + } } GameState.prototype.getState = function() { diff --git a/web/constants.js b/web/constants.js index 884666f..ecf70aa 100644 --- a/web/constants.js +++ b/web/constants.js @@ -3,7 +3,7 @@ var c = { // matchmaker WS_PORT: 6789, - NUM_PLAYERS: 8, + NUM_PLAYERS: 4, MS_PER_FRAME: 100, WAIT_TIME: 60000, // 1 minute MAX_GAMES: 5, // the most games allowed to go on at once, to be tweaked as needed for purposes