pythagoras is saving our ass

master
Jonathan Hager 5 years ago
parent 85164681f2
commit a5e222a02f

@ -15,6 +15,9 @@ public class Enemy extends FightObject{
public void act(FightPlayer player){ public void act(FightPlayer player){
if(POI == null && !move){ if(POI == null && !move){
double distance = StaticMath.calculateDistance(x, y, player.x, player.y); double distance = StaticMath.calculateDistance(x, y, player.x, player.y);
System.out.println("PLayer pos " + player.x + " " + player.y);
System.out.println("Meine pos " + x + " " + y);
System.out.println("ich bin " + isMelee + "mit Distanz " + distance);
if(isMelee) { if(isMelee) {
if(distance <= 32f) { if(distance <= 32f) {
@ -59,10 +62,10 @@ public class Enemy extends FightObject{
float deltaY = player.y - y; float deltaY = player.y - y;
if(Math.abs(deltaX) >= Math.abs(deltaY)) { if(Math.abs(deltaX) >= Math.abs(deltaY)) {
tempX += -(deltaX / Math.abs(deltaX)) * 32; tempX -= (deltaX / Math.abs(deltaX)) * 32;
} }
else { else {
tempY += -(deltaY / Math.abs(deltaY)) * 32; tempY -= (deltaY / Math.abs(deltaY)) * 32;
} }
POI = new Vector2(tempX, tempY); POI = new Vector2(tempX, tempY);

@ -85,7 +85,7 @@ public class Hostile extends Actor {
facing = 3; facing = 3;
} }
if(StaticMath.calculateDistance(getX(), getY(), POI.x, POI.y, movement.angleRad()) < 1f) { if(StaticMath.calculateDistance(getX(), getY(), POI.x, POI.y) < 1f) {
movement.x = 0; movement.x = 0;
movement.y = 0; movement.y = 0;
} }

@ -153,7 +153,7 @@ public class MovingNpc extends Actor{
facing = 3; facing = 3;
} }
if(StaticMath.calculateDistance(getX(), getY(), POI.x, POI.y, movement.angleRad()) < 10f) { if(StaticMath.calculateDistance(getX(), getY(), POI.x, POI.y) < 10f) {
movementX = 0; movementX = 0;
movementY = 0; movementY = 0;
} }

@ -42,36 +42,21 @@ public class StaticMath {
return (float) alpha; return (float) alpha;
} }
public static float calculateDistance(float xPos1, float yPos1, float xPos2, float yPos2, float angle){
float deltaX = xPos2 - xPos1;
float deltaY = yPos2 - yPos1;
double distance;
if(angle == 0) {
distance = deltaX;
}
else {
distance = Math.abs((deltaY / Math.sin(angle)));
}
return (float) distance;
}
public static float calculateDistance(float xPos1, float yPos1, float xPos2, float yPos2){ public static float calculateDistance(float xPos1, float yPos1, float xPos2, float yPos2){
float deltaX = xPos2 - xPos1; float deltaX = xPos2 - xPos1;
float deltaY = yPos2 - yPos1; float deltaY = yPos2 - yPos1;
double distance; float distance;
double angle = calculateAngle(xPos1, yPos1, xPos2, yPos2); double angle = calculateAngle(xPos1, yPos1, xPos2, yPos2);
if(angle == 0) { if(angle == 0 || angle == Math.PI) {
distance = deltaX; distance = deltaX;
} }
else { else {
distance = Math.abs((deltaY / Math.sin(angle))); distance = Math.abs(deltaX / (float) Math.cos(angle));
} }
return (float) distance; return (float) Math.sqrt((float) Math.pow(deltaX, 2) + (float) Math.pow(deltaY, 2));
//return distance;
} }
} }

Loading…
Cancel
Save