Fixed collision detection

This commit is contained in:
Thomas
2020-08-28 16:09:47 +02:00
parent cec5fca5c4
commit 8add12d4c3
5 changed files with 23 additions and 8 deletions

View File

@@ -93,9 +93,10 @@ export default {
this.context.stroke() this.context.stroke()
}, },
renderDebug (player) { renderDebug (player) {
this.context.beginPath()
const canvasX = this.canvas.width / 2 const canvasX = this.canvas.width / 2
const canvasY = this.canvas.height / 2 const canvasY = this.canvas.height / 2
/*
this.context.beginPath()
this.context.lineTo(canvasX, canvasY) this.context.lineTo(canvasX, canvasY)
this.context.lineTo(this.mouse.x, this.mouse.y) this.context.lineTo(this.mouse.x, this.mouse.y)
this.context.lineWidth = 1 this.context.lineWidth = 1
@@ -111,12 +112,6 @@ export default {
this.context.strokeStyle = 'yellow' this.context.strokeStyle = 'yellow'
this.context.stroke() this.context.stroke()
this.context.beginPath()
this.context.arc(this.mouse.x, this.mouse.y, 25, 0, 2 * Math.PI, false)
this.context.lineWidth = 1
this.context.strokeStyle = 'yellow'
this.context.stroke()
const canvasX2 = this.canvas.width / 2 const canvasX2 = this.canvas.width / 2
const canvasY2 = this.canvas.height / 2 const canvasY2 = this.canvas.height / 2
this.context.beginPath() this.context.beginPath()
@@ -124,11 +119,18 @@ export default {
this.context.lineWidth = 1 this.context.lineWidth = 1
this.context.strokeStyle = 'purple' this.context.strokeStyle = 'purple'
this.context.stroke() this.context.stroke()
*/
this.context.fillText('player x: ' + player.x + ' y:' + player.y, 10, 10) this.context.fillText('player x: ' + player.x + ' y:' + player.y, 10, 10)
this.context.fillText('a:' + player.angle + ' a_t' + player.targetAngle, 10, 20) this.context.fillText('a:' + player.angle + ' a_t' + player.targetAngle, 10, 20)
this.context.fillText('canvasX: ' + canvasX + ' canvasY:' + canvasY, 10, 30) this.context.fillText('canvasX: ' + canvasX + ' canvasY:' + canvasY, 10, 30)
this.context.fillText('walls: ' + player.walls.length, 10, 40) this.context.fillText('walls: ' + player.walls.length, 10, 40)
this.context.beginPath()
this.context.arc(this.mouse.x, this.mouse.y, 25, 0, 2 * Math.PI, false)
this.context.lineWidth = 1
this.context.strokeStyle = player.color
this.context.stroke()
}, },
mouseEvent (event) { mouseEvent (event) {
var rect = this.canvas.getBoundingClientRect() var rect = this.canvas.getBoundingClientRect()

View File

@@ -71,6 +71,9 @@ public class GameManager implements IGameManager {
// COLLISIONS // COLLISIONS
game.getPlayers().forEach((id2, player2) -> { game.getPlayers().forEach((id2, player2) -> {
for (var i = 0; i < player2.getWalls().size() - 2; i++) { for (var i = 0; i < player2.getWalls().size() - 2; i++) {
// Pour evité la collision avec un mur venant d'être placé par le même joueur
if (id.equals(id2) && i >= player2.getWalls().size() - 4) break;
Wall wallA = player2.getWalls().get(i); Wall wallA = player2.getWalls().get(i);
Wall wallB = player2.getWalls().get(i + 1); Wall wallB = player2.getWalls().get(i + 1);
if (isCloseToWall(wallA.getX(), wallA.getY(), wallB.getX(), wallB.getY(), player.getX(), player.getY(), if (isCloseToWall(wallA.getX(), wallA.getY(), wallB.getX(), wallB.getY(), player.getX(), player.getY(),
@@ -101,7 +104,11 @@ public class GameManager implements IGameManager {
player.setY(player.getY() + game.getSettings().getPlayerSpeed() * Math.sin(player.getAngle())); player.setY(player.getY() + game.getSettings().getPlayerSpeed() * Math.sin(player.getAngle()));
}); });
SocketUtils.broadcast(game, "gameUpdate", new ObjectMapper().writeValueAsString(game.getPlayers().values())); // Broadcast une fois sur deux
if (game.isUpdateNeeded()) {
SocketUtils.broadcast(game, "gameUpdate", new ObjectMapper().writeValueAsString(game.getPlayers().values()));
game.setUpdateNeeded(false);
} else game.setUpdateNeeded(true);
} }
private void killPlayer(String id) { private void killPlayer(String id) {
@@ -127,11 +134,15 @@ public class GameManager implements IGameManager {
} }
private boolean isCrossingLine (double xa, double ya, double xb, double yb, double xc, double yc, double radius) { private boolean isCrossingLine (double xa, double ya, double xb, double yb, double xc, double yc, double radius) {
/*
var a = xc - xa; var a = xc - xa;
var b = xb - xa; var b = xb - xa;
var c = yc - ya; var c = yc - ya;
var d = yb - ya; var d = yb - ya;
var result = (a * d - b * c) / Math.sqrt(Math.pow(xa - xb, 2) + Math.pow(ya - yb, 2)); var result = (a * d - b * c) / Math.sqrt(Math.pow(xa - xb, 2) + Math.pow(ya - yb, 2));
*/
var result = Math.abs((yb - ya) * xc - (xb - xa) * yc + xb * ya - yb * xa)/ Math.sqrt(Math.pow(xb - xa, 2) + Math.pow(yb - ya, 2));
return result < radius; return result < radius;
} }

View File

@@ -16,8 +16,10 @@ public class Game {
private Map<String, Player> players; private Map<String, Player> players;
private Map<String, WebSocketSession> sessions; private Map<String, WebSocketSession> sessions;
private GameSettings settings; private GameSettings settings;
private boolean updateNeeded;
public Game() { public Game() {
this.updateNeeded = false;
this.players = new HashMap<>(); this.players = new HashMap<>();
this.sessions = new HashMap<>(); this.sessions = new HashMap<>();
this.settings = new GameSettings(); this.settings = new GameSettings();