From ed210d957959759312d907f9d0d6950a8440b878 Mon Sep 17 00:00:00 2001 From: Jonathan Hager Date: Sat, 26 Sep 2020 10:47:34 +0200 Subject: [PATCH] CollisionPolygons --- core/src/com/trs/game/StaticMath.java | 42 +++++++++++++++++++++ core/src/com/trs/game/model/Model.java | 2 +- core/src/com/trs/game/model/PermWall.java | 5 ++- core/src/com/trs/game/model/Projectile.java | 20 +++++++--- core/src/com/trs/game/model/TempWall.java | 5 ++- 5 files changed, 66 insertions(+), 8 deletions(-) diff --git a/core/src/com/trs/game/StaticMath.java b/core/src/com/trs/game/StaticMath.java index b59372c..d016ca2 100644 --- a/core/src/com/trs/game/StaticMath.java +++ b/core/src/com/trs/game/StaticMath.java @@ -61,4 +61,46 @@ public class StaticMath { return new Polygon(points); } + public static Polygon[] createCollisionPolygons(Polygon wall){ + Polygon[] collisionPolygons = new Polygon[4]; + + // first line of polygon + float[] newVertices = wall.getVertices(); + + // last four coordinates are all incremented by one, so the polygon is 1 width polygon + for(int i = 4; i < 8; i++){ + newVertices[i] = newVertices[i-4] + 1; + } + collisionPolygons[0] = new Polygon(newVertices); + + // third line of polygon + newVertices = wall.getVertices(); + + // last four coordinates are all decremented by one, so the polygon is 1 width polygon + for(int i = 4; i < 8; i++){ + newVertices[i-4] = newVertices[i] - 1; + } + collisionPolygons[2] = new Polygon(newVertices); + + // second line of polygon + newVertices = wall.getVertices(); + + newVertices[0] = newVertices[2]; + newVertices[1] = newVertices[3] + 1; + newVertices[6] = newVertices[4]; + newVertices[7] = newVertices[5] + 1; + collisionPolygons[1] = new Polygon(newVertices); + + // fourth line of polygon + newVertices = wall.getVertices(); + + newVertices[2] = newVertices[0]; + newVertices[3] = newVertices[1] - 1; + newVertices[4] = newVertices[6]; + newVertices[5] = newVertices[7] - 1; + collisionPolygons[3] = new Polygon(newVertices); + + return collisionPolygons; + } + } diff --git a/core/src/com/trs/game/model/Model.java b/core/src/com/trs/game/model/Model.java index 21c2ff5..24f028d 100644 --- a/core/src/com/trs/game/model/Model.java +++ b/core/src/com/trs/game/model/Model.java @@ -15,7 +15,7 @@ public class Model { walls.add(new PermWall(60, StaticMath.createPolygon(250,250, 60,25, 100))); projectiles = new ArrayList<>(); - projectiles.add(new Projectile(315, 500, 315, 0)); + projectiles.add(new Projectile(270, 500, 270, 0)); } public void timerStep(){ diff --git a/core/src/com/trs/game/model/PermWall.java b/core/src/com/trs/game/model/PermWall.java index 37c0560..74b48e4 100644 --- a/core/src/com/trs/game/model/PermWall.java +++ b/core/src/com/trs/game/model/PermWall.java @@ -1,6 +1,7 @@ package com.trs.game.model; import com.badlogic.gdx.math.Polygon; +import com.trs.game.StaticMath; public class PermWall implements Wall { @@ -18,7 +19,9 @@ public class PermWall implements Wall { } public double getRotation() { - return rotation; + float[] vertices = polygon.getVertices(); + + return Math.toDegrees(StaticMath.calculateAngle((int) vertices[0], (int) vertices[1], (int) vertices[6], (int) vertices[7])); } public void setRotation(double rotation) { diff --git a/core/src/com/trs/game/model/Projectile.java b/core/src/com/trs/game/model/Projectile.java index b4aa36a..e3518cd 100644 --- a/core/src/com/trs/game/model/Projectile.java +++ b/core/src/com/trs/game/model/Projectile.java @@ -48,10 +48,6 @@ public class Projectile { private void checkCollision(ArrayList walls){ for(Wall wall : walls){ if(Intersector.overlapConvexPolygons(polygon, wall.getPolygon())){ - - setxPos((int) (getxPos() - this.movementX)); - setyPos((int) (getyPos() - this.movementY)); - collision(wall); break; } @@ -61,8 +57,22 @@ public class Projectile { private void collision(Wall wall){ System.out.println("Collision"); + Polygon[] collisionPolygons = StaticMath.createCollisionPolygons(wall.getPolygon()); + + double collisionAngle = 0; + for(Polygon collision : collisionPolygons){ + if(Intersector.overlapConvexPolygons(polygon, collision)){ + float[] vertices = collision.getVertices(); + collisionAngle = StaticMath.calculateAngle((int) vertices[0], (int) vertices[1], (int) vertices[6], (int) vertices[7]); + break; + } + } + + setxPos((int) (getxPos() - this.movementX)); + setyPos((int) (getyPos() - this.movementY)); + double alpha = Math.atan(movementY/movementX); - double delta = (Math.PI / 2) - Math.toRadians(wall.getRotation()); + double delta = (Math.PI / 2) - collisionAngle; double beta = +((Math.PI / 2) - alpha - delta); double phi = beta + Math.toRadians(wall.getRotation()); diff --git a/core/src/com/trs/game/model/TempWall.java b/core/src/com/trs/game/model/TempWall.java index 7d7fbaa..a5de9ca 100644 --- a/core/src/com/trs/game/model/TempWall.java +++ b/core/src/com/trs/game/model/TempWall.java @@ -2,6 +2,7 @@ package com.trs.game.model; import com.badlogic.gdx.math.Polygon; +import com.trs.game.StaticMath; public class TempWall implements Wall { @@ -21,7 +22,9 @@ public class TempWall implements Wall { } public double getRotation() { - return rotation; + float[] vertices = polygon.getVertices(); + + return Math.toDegrees(StaticMath.calculateAngle((int) vertices[0], (int) vertices[1], (int) vertices[6], (int) vertices[7])); } public void setRotation(double rotation) {