Polygon shit is nice

master
Jonathan Hager 5 years ago
parent 5f9514bdf5
commit 2df26f9662

@ -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;

@ -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));

@ -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<Wall> 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<Wall> 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;
}
}

Loading…
Cancel
Save