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;
}
// 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;
}
// 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;
}
}
-module.exports = {handleWalls: handleWalls, handlePaddles, handlePaddles};
+module.exports = {handleWalls: handleWalls, handlePaddles, handlePaddles, segments_intersect: segments_intersect};
for(var i=0;i<this.paddles.length;i++) {
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;
- }
//
var endpoints = field.genAllEndpoints(this.numPlayers, this.dead);
var livingzones = endpoints.filter(e => (e.id != -1));
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...
}
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() {