From fa120593c3f15e5e745dd1d6016e89426b889cac Mon Sep 17 00:00:00 2001
From: Elijah Cohen <eli@eli173.com>
Date: Thu, 2 May 2019 16:44:59 -0500
Subject: [PATCH] broke stuff, gonna fix the framerate issue before proceeding
 though

---
 server/collisions.js |  3 ++-
 server/constants.js  |  2 +-
 server/gamestate.js  | 59 +++++---------------------------------------
 web/constants.js     |  2 +-
 4 files changed, 10 insertions(+), 56 deletions(-)

diff --git a/server/collisions.js b/server/collisions.js
index 1934733..1501320 100644
--- a/server/collisions.js
+++ b/server/collisions.js
@@ -67,8 +67,9 @@ var handleWalls = function(ball, walls) {
     // wall is an endpoints
     for(var wall of walls) {
 	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))) {
+	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);
diff --git a/server/constants.js b/server/constants.js
index a45093a..a5fdce4 100644
--- a/server/constants.js
+++ b/server/constants.js
@@ -10,7 +10,7 @@ var c = {
     // gamestate
     DYING_TIME_IN_FRAMES: 100,
     BOARD_RADIUS: 10,
-    OOB_THRESH: 1, // out-of-bounds threshold
+    OOB_THRESH: 4, // out-of-bounds threshold
     ANGLE_THRESH: 0.2, //radians, needs to acct for various rotatings going on... can prolly wing it
     PADDLE_MVT_BONUS: 0.1, // why this value? who knows. the extra speed from paddles in motion
     // ball
diff --git a/server/gamestate.js b/server/gamestate.js
index b64e0aa..52f63dd 100644
--- a/server/gamestate.js
+++ b/server/gamestate.js
@@ -62,60 +62,13 @@ GameState.prototype.update = function(inputs) {
 
 	// below here probably requires changes
 	// (check the fixt edges first, then the paddles I guess
-	for(var i=0; (i<walls.length) && !collided; i++) {
-	    var nearest = nearest_point_on_line(ball.coord, walls[i]);
-	    var dist2 = ball.coord.dist2(nearest);
-	    if(dist2 < (ball.radius*ball.radius)) {
-		// we have a collision!
-		collided = true;
-		var vec = [walls[i].f.x-walls[i].s.x, walls[i].f.y-walls[i].s.y];
-		var slope_angle = Math.atan2(vec[1],vec[0]);
-		// rotate, reflect, rotate back
-		var vel_coord = new Coord(ball.dx, ball.dy);
-		vel_coord.rotate(-slope_angle);
-		vel_coord.y = -vel_coord.y;
-		vel_coord.rotate(slope_angle);
-		ball.dx = vel_coord.x;
-		ball.dy = vel_coord.y;
-		// increase speed slightly
-		ball.speed_up();
-	    }  
-	}
-	for(var lz of livingzones) {
-	    // get corresponding paddle
-	    // should be guaranteed to find one.... so...
-	    var paddle = this.paddles.find(p => p.id ==lz.id);
-	    if((paddle !== undefined) && !collided) {
-		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)) {
-		    // we have a collision!
-		    collided = true;
-		    var vec = [padends.f.x-padends.s.x, padends.f.y-padends.s.y];
-		    var slope_angle = Math.atan2(vec[1],vec[0]);
-		    // rotate, reflect, rotate back
-		    var vel_coord = new Coord(ball.dx, ball.dy);
-		    vel_coord.rotate(-slope_angle);
-		    vel_coord.y = -vel_coord.y;
-		    // note: any 'too fast' errors will be solved in the 'speed_up' method, so i need not worry here
-		    if(paddle.direction == 'u') { 
-			vel_coord.x += c.PADDLE_MVT_BONUS;
-		    }
-		    else if(paddle.direction == 'd') {
-			vel_coord.x -= c.PADDLE_MVT_BONUS;
-		    }
-		    vel_coord.rotate(slope_angle);
-		    ball.dx = vel_coord.x;
-		    ball.dy = vel_coord.y;
-		    // increase speed slightly
-		    ball.speed_up();
-		}  
-	    }
-	}
-
-
+	if(collisions.handleWalls(ball, walls))
+	    continue;
+	if(collisions.handlePaddles(balls, livingzones, this.paddles))
+	    continue;
+	// i put that last continue but there's nothing left to do...
     }
+    
     // shrink the existing dead zones for the future
     for(var d of this.dead) {
 	if(d.time > 0)
diff --git a/web/constants.js b/web/constants.js
index 1a53e89..884666f 100644
--- a/web/constants.js
+++ b/web/constants.js
@@ -10,7 +10,7 @@ var c = {
     // gamestate
     DYING_TIME_IN_FRAMES: 100,
     BOARD_RADIUS: 10,
-    OOB_THRESH: 1, // out-of-bounds threshold
+    OOB_THRESH: 4, // out-of-bounds threshold
     ANGLE_THRESH: 0.2, //radians, needs to acct for various rotatings going on... can prolly wing it
     PADDLE_MVT_BONUS: 0.1, // why this value? who knows. the extra speed from paddles in motion
     // ball
-- 
2.39.5