From: Elijah Cohen Date: Thu, 2 May 2019 21:08:34 +0000 (-0500) Subject: 'finished' collision stuff, about to change gamestate to reflect it X-Git-Url: https://git.eli173.com/?a=commitdiff_plain;h=5b767959a428b8f18ce44db234ad89ab387d6beb;p=pong_br 'finished' collision stuff, about to change gamestate to reflect it --- diff --git a/NOTES.org b/NOTES.org index d7f013c..f0f7f8b 100644 --- a/NOTES.org +++ b/NOTES.org @@ -1,4 +1,8 @@ +WAIT SHIT I THINK THE MAX SPEEDS ARE DETERMINED BY THE FRAMERATE. WHOOPS. TOFIX + + + The general outline of the game is that we have some large number of players $n$, arranged in a $2n$-sided regular polygon, where the player has their pong-space, and is surrounded by two walls. The walls primarily serve the purpose of having the game end nicely when there are two players left: it becomes a regular game of pong. I'm going to limit the number of players to 10 for the time being, I think anything with more than 20 sides might be a bit too much. 20 might be pushing it actually. diff --git a/server/collisions.js b/server/collisions.js index b1e9a74..1934733 100644 --- a/server/collisions.js +++ b/server/collisions.js @@ -1,4 +1,5 @@ +const c = require('./constants.js'); const Coord = require('./coord.js'); const Endpoints = require('./endpoints.js'); // i guess slightly useful here @@ -59,20 +60,59 @@ var segments_intersect = function(e1, e2) { return true; } + var handleWalls = function(ball, walls) { // returns true if there's a collision + // modifies ball's velocity if it encounters an actual collision + // wall is an endpoints for(var wall of walls) { - var nearest = nearest_point_on_line(ball.coord, wall); - if(ball.coord.dist2(nearest) < (ball.radius)**2) { // there's a collision - // so the midpoint of the wall gives a nice normal vector - var norm = ball.coord; + 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))) { + //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.rotate(normal_angle); + ball.speed_up(); + return true; } } - + return false; } -var handlePaddles = function() { +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); + 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))) { + //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... + 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.speed_up(); + return true; + } + } + return false; } + module.exports = {handleWalls: handleWalls, handlePaddles, handlePaddles}; diff --git a/server/constants.js b/server/constants.js index 9ff4e05..a45093a 100644 --- a/server/constants.js +++ b/server/constants.js @@ -23,7 +23,7 @@ var c = { BOARD_RADIUS: 10, // completely arbitrary actually... // paddle DPADDLE: 0.1, - WIDTH_RATIO: 0.1, // paddle is 1/10th of gap rn + WIDTH_RATIO: 0.2, // paddle is 1/10th of gap rn } module.exports = c diff --git a/server/gamestate.js b/server/gamestate.js index 4ac418d..b64e0aa 100644 --- a/server/gamestate.js +++ b/server/gamestate.js @@ -41,6 +41,7 @@ GameState.prototype.update = function(inputs) { this.paddles[i].move(inputs[this.paddles[i].id]); //hacky and bad, requires the inputs be } //move the balls + // can i increase efficiency or sensibility by doing this later? maybe. should i? unlikely rn for(var ball of this.balls) { ball.coord.x += ball.dx; ball.coord.y += ball.dy; diff --git a/web/constants.js b/web/constants.js index add9fdd..1a53e89 100644 --- a/web/constants.js +++ b/web/constants.js @@ -23,7 +23,7 @@ var c = { BOARD_RADIUS: 10, // completely arbitrary actually... // paddle DPADDLE: 0.1, - WIDTH_RATIO: 0.1, // paddle is 1/10th of gap rn + WIDTH_RATIO: 0.2, // paddle is 1/10th of gap rn } diff --git a/web/main.js b/web/main.js index f11df3f..ecafbd8 100644 --- a/web/main.js +++ b/web/main.js @@ -18,6 +18,11 @@ var main = function() { // starts everything, gets us going, setup ish // change the 1's to zoom in i think.. todo ctx.transform(10, 0, 0, 10, ctx.canvas.width/2, ctx.canvas.height/2); // change to setTransform? + // this is just to have everything go easily for testing + var othersockets = []; + for(var i=0; i