CollisionPolygons

master
Jonathan Hager 5 years ago
parent 72e091610b
commit ed210d9579

@ -61,4 +61,46 @@ public class StaticMath {
return new Polygon(points); 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;
}
} }

@ -15,7 +15,7 @@ public class Model {
walls.add(new PermWall(60, StaticMath.createPolygon(250,250, 60,25, 100))); walls.add(new PermWall(60, StaticMath.createPolygon(250,250, 60,25, 100)));
projectiles = new ArrayList<>(); projectiles = new ArrayList<>();
projectiles.add(new Projectile(315, 500, 315, 0)); projectiles.add(new Projectile(270, 500, 270, 0));
} }
public void timerStep(){ public void timerStep(){

@ -1,6 +1,7 @@
package com.trs.game.model; package com.trs.game.model;
import com.badlogic.gdx.math.Polygon; import com.badlogic.gdx.math.Polygon;
import com.trs.game.StaticMath;
public class PermWall implements Wall { public class PermWall implements Wall {
@ -18,7 +19,9 @@ public class PermWall implements Wall {
} }
public double getRotation() { 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) { public void setRotation(double rotation) {

@ -48,10 +48,6 @@ public class Projectile {
private void checkCollision(ArrayList<Wall> walls){ private void checkCollision(ArrayList<Wall> walls){
for(Wall wall : walls){ for(Wall wall : walls){
if(Intersector.overlapConvexPolygons(polygon, wall.getPolygon())){ if(Intersector.overlapConvexPolygons(polygon, wall.getPolygon())){
setxPos((int) (getxPos() - this.movementX));
setyPos((int) (getyPos() - this.movementY));
collision(wall); collision(wall);
break; break;
} }
@ -61,8 +57,22 @@ public class Projectile {
private void collision(Wall wall){ private void collision(Wall wall){
System.out.println("Collision"); 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 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 beta = +((Math.PI / 2) - alpha - delta);
double phi = beta + Math.toRadians(wall.getRotation()); double phi = beta + Math.toRadians(wall.getRotation());

@ -2,6 +2,7 @@ package com.trs.game.model;
import com.badlogic.gdx.math.Polygon; import com.badlogic.gdx.math.Polygon;
import com.trs.game.StaticMath;
public class TempWall implements Wall { public class TempWall implements Wall {
@ -21,7 +22,9 @@ public class TempWall implements Wall {
} }
public double getRotation() { 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) { public void setRotation(double rotation) {

Loading…
Cancel
Save