From 2df26f9662f3476446631a8f246a7b0c8b362965 Mon Sep 17 00:00:00 2001 From: Jonathan Hager Date: Fri, 25 Sep 2020 23:52:19 +0200 Subject: [PATCH] Polygon shit is nice --- core/src/com/trs/game/StaticMath.java | 5 +- core/src/com/trs/game/model/Model.java | 3 +- core/src/com/trs/game/model/Projectile.java | 53 ++++++++++++++------- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/core/src/com/trs/game/StaticMath.java b/core/src/com/trs/game/StaticMath.java index 21bc659..b59372c 100644 --- a/core/src/com/trs/game/StaticMath.java +++ b/core/src/com/trs/game/StaticMath.java @@ -43,8 +43,9 @@ public class StaticMath { public static Polygon createPolygon(int xPos, int yPos, double angle, double width, double length){ float[] points = new float[8]; - double d = Math.sin(Math.toRadians(angle)) * width; - double e = Math.cos(Math.toRadians(angle)) * width; + double phi = (Math.PI / 2) - Math.toRadians(angle); + double d = Math.sin(phi) * width; + double e = Math.cos(phi) * width; double f = Math.sin(Math.toRadians(angle)) * length; double g = Math.cos(Math.toRadians(angle)) * length; diff --git a/core/src/com/trs/game/model/Model.java b/core/src/com/trs/game/model/Model.java index ef693a4..f0480e0 100644 --- a/core/src/com/trs/game/model/Model.java +++ b/core/src/com/trs/game/model/Model.java @@ -2,6 +2,7 @@ package com.trs.game.model; import com.badlogic.gdx.math.Rectangle; +import com.trs.game.StaticMath; import java.util.ArrayList; @@ -13,7 +14,7 @@ public class Model { public Model(){ monster = new Monster(250,150); walls = new ArrayList<>(); - walls.add(new PermWall(20,new Rectangle(250,250,50,25))); + walls.add(new PermWall(20, StaticMath.createPolygon(250,250,30,25, 100))); projectiles = new ArrayList<>(); projectiles.add(new Projectile(200, 200, 1000, 600)); diff --git a/core/src/com/trs/game/model/Projectile.java b/core/src/com/trs/game/model/Projectile.java index 6b4b7ec..270be6b 100644 --- a/core/src/com/trs/game/model/Projectile.java +++ b/core/src/com/trs/game/model/Projectile.java @@ -1,12 +1,8 @@ package com.trs.game.model; -import com.badlogic.gdx.math.Circle; -import com.badlogic.gdx.math.Intersector; import com.badlogic.gdx.math.Polygon; -import com.badlogic.gdx.math.Rectangle; import com.trs.game.StaticMath; -import java.lang.reflect.Array; import java.util.ArrayList; public class Projectile { @@ -16,13 +12,24 @@ public class Projectile { private double movementX; private double movementY; - private Circle circle; + private Polygon polygon; public Projectile(int xPos, int yPos, int xPosMonster, int yPosMonster){ - this.circle = new Circle(xPos, yPos, 5); + float[] positions = new float[8]; + int length = 5; + positions[0] = xPos; + positions[1] = yPos; + positions[2] = xPos + length; + positions[3] = yPos; + positions[4] = xPos + length; + positions[5] = yPos + length; + positions[6] = xPos; + positions[7] = yPos + length; + + this.polygon = new Polygon(positions); // calculating values for movementX and movementY - double angle = StaticMath.calculateAngle((int) circle.x, (int) circle.y, xPosMonster, yPosMonster); + double angle = StaticMath.calculateAngle((int) polygon.getVertices()[0], (int) polygon.getVertices()[1], xPosMonster, yPosMonster); movementX = Math.cos(angle) * SPEED; movementY = Math.sin(angle) * SPEED; @@ -33,16 +40,16 @@ public class Projectile { public void move(ArrayList walls){ checkCollision(walls); - circle.x += this.movementX; - circle.y += this.movementY; + setxPos((int) (getxPos() + this.movementX)); + setyPos((int) (getyPos() + this.movementY)); } private void checkCollision(ArrayList walls){ for(Wall wall : walls){ if(/*Intersector.overlaps(circle, wall.getPolygon())*/ false){ - circle.x -= movementX; - circle.y -= movementY; + setxPos((int) (getxPos() - this.movementX)); + setyPos((int) (getyPos() - this.movementY)); collision(wall); break; @@ -63,18 +70,32 @@ public class Projectile { } public int getxPos() { - return (int) circle.x; + return (int) polygon.getVertices()[0]; } public int getyPos() { - return (int) circle.y; + return (int) polygon.getVertices()[1]; + } + + public void setxPos(int xPos){ + polygon.getVertices()[0] = xPos; + polygon.getVertices()[2] = xPos + 5; + polygon.getVertices()[4] = xPos + 5; + polygon.getVertices()[6] = xPos; + } + + public void setyPos(int yPos){ + polygon.getVertices()[1] = yPos; + polygon.getVertices()[3] = yPos; + polygon.getVertices()[5] = yPos + 5; + polygon.getVertices()[7] = yPos + 5; } public int getRadius(){ - return (int) circle.radius; + return 5; } - public Circle getCircle() { - return circle; + public Polygon getPolygon() { + return polygon; } }