From: Elijah Cohen <eli@eli173.com>
Date: Thu, 2 May 2019 23:39:31 +0000 (-0500)
Subject: some good changes, not fixt yet though
X-Git-Url: https://git.eli173.com/?a=commitdiff_plain;h=1d84f62b33e432640a5961180d05a85071b7436a;p=pong_br

some good changes, not fixt yet though
---

diff --git a/server/collisions.js b/server/collisions.js
index dba68d2..324d2a9 100644
--- a/server/collisions.js
+++ b/server/collisions.js
@@ -67,17 +67,30 @@ 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/c.FPS, ball.coord.y + ball.dy/c.FPS);
+	var next_spot = new Coord(ball.coord.x + ball.dx/c.FPS, ball.coord.y + ball.dy/c.FPS); // the next spot
 	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.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;
+	    // okay, i understand now, gotta do translations as well as the rotations
+	    // vector for next_spot needs to rotate, then be translated so that current ball spot is taken away,
+	    // putting vector with tail at the origin. then i do the reflection, and translate back, then rotate back,
+	    // and i might be good from there
+	    var ball_spot = new Coord(ball.coord.x, ball.coord.y);
+	    console.log("-------------------------------------");
+	    console.log(next_spot);
+	    ball_spot.rotate(-normal_angle);
+	    next_spot.rotate(-normal_angle);
+	    console.log(next_spot);
+	    next_spot.translate(-ball_spot.x, -ball_spot.y); // now it's a vector with tail at the origin i think
+	    console.log(next_spot);
+	    next_spot.x = -next_spot.x; // next translate back then rotate back then done?
+	    next_spot.translate(ball_spot.x, ball_spot.y);
+	    ball_spot.rotate(normal_angle);
+	    next_spot.rotate(normal_angle); // nope gotta subtract back to get it to be a proper 
+	    next_spot.translate(-ball_spot.x, -ball_spot.y); // now i'm good i think
+	    ball.dx = next_spot.x;
+	    ball.dy = next_spot.y;
 	    ball.speed_up();
 	    return true;
 	}
@@ -102,9 +115,10 @@ var handlePaddles = function(ball, lzs, paddles) {
 	    //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);
+	    // okay, i understand now. I have to do the vector addition and translation thing
 	    var vel_vec = new Coord(ball.dx, ball.dy);
 	    vel_vec.rotate(-normal_angle);
-	    vel_vec.y = -vel_vec.y; // i'm fairly certain it's x...
+	    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')
diff --git a/server/coord.js b/server/coord.js
index 5d4b665..0506191 100644
--- a/server/coord.js
+++ b/server/coord.js
@@ -28,5 +28,10 @@ Coord.prototype.rotate = function(th) {
     this.x = new_x;
     this.y = new_y;
 }
+Coord.prototype.translate = function (a,b) {
+    this.x += a;
+    this.y += b;
+}
+
 
 module.exports = Coord;