Create some useful geometric methods

This commit is contained in:
2021-03-04 21:58:31 +01:00
parent b8d2fdda53
commit f8190a9d46
6 changed files with 87 additions and 2 deletions

View File

@@ -2,6 +2,9 @@ package com.bartlomiejpluta.base.api.game.entity;
import org.joml.Vector2i;
import static java.lang.Math.PI;
import static org.joml.Math.atan2;
public enum Direction {
UP(0, -1),
DOWN(0, 1),
@@ -19,4 +22,37 @@ public enum Direction {
public Vector2i asVector() {
return new Vector2i(x, y);
}
public Direction opposite() {
switch (this) {
case UP:
return DOWN;
case RIGHT:
return LEFT;
case DOWN:
return UP;
case LEFT:
return RIGHT;
}
throw new IllegalArgumentException();
}
public static Direction ofVector(Vector2i vector) {
// X Versor = [1, 0]
// dot = 1 * vector.x + 0 * vector.y = vector.x
// det = 1 * vector.y - 0 * vector.x = vector.y
// angle = atan2(det, dot) = atan2(vector.y, vector.x)
float angle = atan2(vector.y, vector.x);
if (-PI / 4 < angle && angle < PI / 4) {
return RIGHT;
} else if (PI / 4 <= angle && angle <= 3 * PI / 4) {
return DOWN;
} else if (3 * PI / 4 < angle && angle < 5 * PI / 4) {
return LEFT;
} else {
return UP;
}
}
}

View File

@@ -23,4 +23,12 @@ public interface Entity extends Placeable, Renderable, Updatable {
void setSpeed(float speed);
void setAnimationSpeed(float speed);
boolean isMoving();
int chebyshevDistance(Entity other);
int manhattanDistance(Entity other);
Direction getDirectionTowards(Entity target);
}

View File

@@ -15,13 +15,18 @@ public interface Placeable {
void moveRotation(float rotation);
float getScaleX();
void setScaleX(float scale);
float getScaleY();
void setScaleY(float scale);
void setScale(float scale);
void setScale(float scaleX, float scaleY);
float euclideanDistance(Placeable other);
Matrix4f getModelMatrix();
}