From 77d1a53ddbd5536d66072c20a9d3146f1e995b8f Mon Sep 17 00:00:00 2001 From: Elijah Cohen Date: Wed, 1 May 2019 11:45:49 -0500 Subject: [PATCH] SO MUCH SUCCESS on to collisions now --- server/gamestate.js | 2 +- server/paddles.js | 19 +++++++++++++++++++ web/draw.js | 31 +++++++++++++++++-------------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/server/gamestate.js b/server/gamestate.js index 2462914..9681269 100644 --- a/server/gamestate.js +++ b/server/gamestate.js @@ -94,7 +94,7 @@ GameState.prototype.update = function(inputs) { // should be guaranteed to find one.... so... var paddle = this.paddles.find(p => p.id ==lz.id); if((paddle !== undefined) && !collided) { - var padends = paddle.getEndpoints(lz); + var padends = paddle.getPaddlePoints(lz); var nearest = nearest_point_on_line(ball.coord, padends); var dist2 = ball.coord.dist2(nearest); if(dist2 < (ball.radius*ball.radius)) { diff --git a/server/paddles.js b/server/paddles.js index 1d895e9..a98a180 100644 --- a/server/paddles.js +++ b/server/paddles.js @@ -33,6 +33,25 @@ function dist(p1,p2) { } +Paddle.prototype.getPaddlePoints = function(enclosing) { + var encl_len = dist(enclosing.f, enclosing.s); + var pspace_len = encl_len - c.WIDTH_RATIO*encl_len; + var idk_len = (encl_len - pspace_len)/2; // the length between the highest paddle center location and the edge + var paddle_ratio = (this.position+1)/2; // converts the -1-to-1-centered 'position' to a 0-1 ratio + var center_pos = idk_len + paddle_ratio*pspace_len; // length from one of the endpoints (which one? does it matter?) + var fst_pos = center_pos - c.WIDTH_RATIO*encl_len/2; + var snd_pos = center_pos + c.WIDTH_RATIO*encl_len/2; + var fst_pct = fst_pos/encl_len; + var snd_pct = snd_pos/encl_len; + var fst_x = enclosing.f.x*fst_pct + enclosing.s.x*(1-fst_pct); + var fst_y = enclosing.f.y*fst_pct + enclosing.s.y*(1-fst_pct); + var snd_x = enclosing.f.x*snd_pct + enclosing.s.x*(1-snd_pct); + var snd_y = enclosing.f.y*snd_pct + enclosing.s.y*(1-snd_pct); + var fst = new Coord(fst_x, fst_y); + var snd = new Coord(snd_x, snd_y); + return new Endpoints(fst, snd, this.id); +} + Paddle.prototype.getEndpoints = function(enclosing) { // returns an endpoints object for the paddle // given the desired width of said paddle and the enclosing endpoints diff --git a/web/draw.js b/web/draw.js index f6f0c7d..31f45dc 100644 --- a/web/draw.js +++ b/web/draw.js @@ -31,7 +31,7 @@ var draw = function(state, ctx) { } // do something to show the zones for my sanity for(var lz of livingzones) { - drawLine(ctx, xcolor, lz.f, lz.s); + //drawLine(ctx, xcolor, lz.f, lz.s); } // balls for(var b of state.balls) { @@ -43,25 +43,28 @@ var draw = function(state, ctx) { if(paddle === undefined) alert("UH OH, paddle somehow undefined"); // cool this is way simpler since we know these are all alive var pps = getPaddlePoints(paddle, eps); - console.log(pps); drawLine(ctx, pcolor, pps.f, pps.s); } } + var getPaddlePoints = function(paddle, enclosing) { - // returns an endpoints object for the paddle - // given the desired width of said paddle and the enclosing endpoints var encl_len = dist(enclosing.f, enclosing.s); - var pspace_len = encl_len - (2*c.WIDTH_RATIO*encl_len); - var place_on_pspace = pspace_len*(paddle.position+1)/2; - var above_f = pspace_len + c.WIDTH_RATIO; // distance above the first enclosing point (takes advantage of the increasing in angle thing to determine orientations) - var overall_proportion = above_f/encl_len; - var vector = [enclosing.s.x-enclosing.f.x, enclosing.s.y-enclosing.f.y]; - var d = overall_proportion-c.WIDTH_RATIO; - var first = new Coord(vector[0]*d, vector[1]*d); - d = overall_proportion+c.WIDTH_RATIO; - var second = new Coord(vector[0]*d, vector[1]*d); - return new Endpoints(first, second, paddle.id); + var pspace_len = encl_len - c.WIDTH_RATIO*encl_len; + var idk_len = (encl_len - pspace_len)/2; // the length between the highest paddle center location and the edge + var paddle_ratio = (paddle.pos+1)/2; // converts the -1-to-1-centered 'position' to a 0-1 ratio + var center_pos = idk_len + paddle_ratio*pspace_len; // length from one of the endpoints (which one? does it matter?) + var fst_pos = center_pos - c.WIDTH_RATIO*encl_len/2; + var snd_pos = center_pos + c.WIDTH_RATIO*encl_len/2; + var fst_pct = fst_pos/encl_len; + var snd_pct = snd_pos/encl_len; + var fst_x = enclosing.f.x*fst_pct + enclosing.s.x*(1-fst_pct); + var fst_y = enclosing.f.y*fst_pct + enclosing.s.y*(1-fst_pct); + var snd_x = enclosing.f.x*snd_pct + enclosing.s.x*(1-snd_pct); + var snd_y = enclosing.f.y*snd_pct + enclosing.s.y*(1-snd_pct); + var fst = new Coord(fst_x, fst_y); + var snd = new Coord(snd_x, snd_y); + return new Endpoints(fst, snd, paddle.id); } var dist = function(c1, c2) { -- 2.39.2