collision optimization

This commit is contained in:
nora 2020-12-13 13:28:38 +01:00
parent 0c1178d1d1
commit 90c5950e0b
11 changed files with 61 additions and 24 deletions

View file

@ -63,7 +63,7 @@ public class Master extends JPanel {
/** /**
* The current width and height of the game area * The current width and height of the game area
*/ */
private int w,h; private int w, h;
/** /**
* Create a new master object * Create a new master object
@ -171,13 +171,13 @@ public class Master extends JPanel {
* @param obj The new object * @param obj The new object
*/ */
public void create(GameObject obj) { public void create(GameObject obj) {
create(obj, 0); create(obj, obj.getLayer());
} }
/** /**
* This method has to be called for every newly created GameObject * This method has to be called for every newly created GameObject
* *
* @param obj The new object * @param obj The new object
* @param renderLayer The render layer the object will be put on, 0 is below everything * @param renderLayer The render layer the object will be put on, 0 is below everything
*/ */
public void create(GameObject obj, int renderLayer) { public void create(GameObject obj, int renderLayer) {
@ -191,7 +191,8 @@ public class Master extends JPanel {
/** /**
* Add a new Drawable to the render list * Add a new Drawable to the render list
* @param d The drawable *
* @param d The drawable
* @param layer The layer it should be put on (>=0) * @param layer The layer it should be put on (>=0)
*/ */
public void addDrawable(Drawable d, int layer) { public void addDrawable(Drawable d, int layer) {
@ -202,7 +203,7 @@ public class Master extends JPanel {
} }
//layer exists check //layer exists check
int layerDif = layer - (drawables.size()-1); int layerDif = layer - (drawables.size() - 1);
if (layerDif > 0) { if (layerDif > 0) {
for (int i = 0; i < layerDif; i++) { for (int i = 0; i < layerDif; i++) {
drawables.add(new ArrayList<>()); drawables.add(new ArrayList<>());
@ -227,13 +228,9 @@ public class Master extends JPanel {
for (Collidable c : collidables) { for (Collidable c : collidables) {
double distance = Vector2D.distance(c.getCenterPos(), col.getCenterPos()); double distance = Vector2D.distance(c.getCenterPos(), col.getCenterPos());
if (c != col /*&& !(distance > c.getSize().magnitude() && distance > col.getSize().magnitude())*/) { if (c != col && (distance < c.getHitbox().getSize() + col.getHitbox().getSize())) {
System.out.println("\n\nCOLL POSSIBLE");
System.out.println("This: " + c.getHitbox() + "\n\nother: " + col.getHitbox());
if (c.collidesWith(col)) { if (c.collidesWith(col)) {
System.err.println("COLL");
collides = true; collides = true;
} }
} }
@ -251,8 +248,8 @@ public class Master extends JPanel {
public void destroy(GameObject gameObject) { public void destroy(GameObject gameObject) {
objects.remove(gameObject); objects.remove(gameObject);
drawables.remove(gameObject); drawables.get(gameObject.getLayer()).remove(gameObject);
if(gameObject instanceof Collidable){ if (gameObject instanceof Collidable) {
collidables.remove(gameObject); collidables.remove(gameObject);
} }
} }

View file

@ -1,5 +1,6 @@
package core; package core.math;
import core.Master;
import core.math.Vector2D; import core.math.Vector2D;
/** /**
@ -7,7 +8,6 @@ import core.math.Vector2D;
* <p>In this system, the screen is always 100 high and 100 * the screen ration wide. * <p>In this system, the screen is always 100 high and 100 * the screen ration wide.
* This class is the used to convert these coordinates into the true Java 2D coordinates for the drawing</p> * This class is the used to convert these coordinates into the true Java 2D coordinates for the drawing</p>
*/ */
//TODO make this return a vector2d instead of the isX
public class Coords { public class Coords {
/** /**

View file

@ -1,6 +1,7 @@
package core.physics; package core.physics;
import core.math.Vector2D; import core.math.Vector2D;
import core.physics.hitboxes.Hitbox;
public interface Collidable { public interface Collidable {

View file

@ -1,12 +1,20 @@
package core.physics; package core.physics.hitboxes;
import core.Drawable; import core.Drawable;
import core.math.Vector2D; import core.math.Vector2D;
public abstract class Hitbox implements Drawable { public abstract class Hitbox implements Drawable {
private final boolean isTrigger;
protected Hitbox(boolean isTrigger) {
this.isTrigger = isTrigger;
}
public abstract void moveTo(Vector2D x1, Vector2D size); public abstract void moveTo(Vector2D x1, Vector2D size);
public abstract double getSize();
@Override @Override
public String toString() { public String toString() {
return super.toString(); return super.toString();

View file

@ -1,6 +1,6 @@
package core.physics; package core.physics.hitboxes;
import core.Coords; import core.math.Coords;
import core.Master; import core.Master;
import core.math.Vector2D; import core.math.Vector2D;
@ -18,6 +18,22 @@ public class RectHitBox extends Hitbox {
*/ */
private Vector2D x1, y1, x2, y2; private Vector2D x1, y1, x2, y2;
/**
* Create a new RectHitbox with the position of the top left point {@code x1} and the size
*
* @param x1 The top left point
* @param size the size
* @param isTrigger Whether the hitbox is a trigger (only collision detection, not restricting movement
*/
public RectHitBox(Vector2D x1, Vector2D size, boolean isTrigger) {
super(isTrigger);
this.x1 = x1;
this.x2 = Vector2D.add(x1, new Vector2D(size.x, 0));
this.y1 = Vector2D.add(x1, new Vector2D(0, size.y));
this.y2 = Vector2D.add(x1, new Vector2D(size.x, size.y));
Master.getMaster().addDrawable(this, 1);
}
/** /**
* Create a new RectHitbox with the position of the top left point {@code x1} and the size * Create a new RectHitbox with the position of the top left point {@code x1} and the size
* *
@ -25,6 +41,7 @@ public class RectHitBox extends Hitbox {
* @param size the size * @param size the size
*/ */
public RectHitBox(Vector2D x1, Vector2D size) { public RectHitBox(Vector2D x1, Vector2D size) {
this(x1, size, false);
this.x1 = x1; this.x1 = x1;
this.x2 = Vector2D.add(x1, new Vector2D(size.x, 0)); this.x2 = Vector2D.add(x1, new Vector2D(size.x, 0));
this.y1 = Vector2D.add(x1, new Vector2D(0, size.y)); this.y1 = Vector2D.add(x1, new Vector2D(0, size.y));
@ -46,6 +63,12 @@ public class RectHitBox extends Hitbox {
this.y2 = Vector2D.add(x1, new Vector2D(size.x, size.y)); this.y2 = Vector2D.add(x1, new Vector2D(size.x, size.y));
} }
@Override
public double getSize() {
Vector2D size = Vector2D.subtract(y2, x1);
return size.magnitude();
}
/** /**
* Check whether two hitboxes collide with each other * Check whether two hitboxes collide with each other
* *
@ -154,7 +177,7 @@ public class RectHitBox extends Hitbox {
Vector2D abs = Coords.getWorldCoords(x1); Vector2D abs = Coords.getWorldCoords(x1);
Vector2D sizeAbs = Coords.getWorldCoordsSize(Vector2D.subtract(y2, x1)); Vector2D sizeAbs = Coords.getWorldCoordsSize(Vector2D.subtract(y2, x1));
g2d.drawRect((int)abs.x, (int)abs.y, (int)sizeAbs.y, (int)sizeAbs.y); g2d.drawRect((int)abs.x, (int)abs.y, (int)sizeAbs.x, (int)sizeAbs.y);
g2d.setPaint(Color.MAGENTA); g2d.setPaint(Color.MAGENTA);
} }
} }

View file

@ -1,6 +1,6 @@
package objects; package objects;
import core.Coords; import core.math.Coords;
import core.Drawable; import core.Drawable;
import core.Master; import core.Master;
import core.math.Vector2D; import core.math.Vector2D;
@ -23,6 +23,8 @@ public abstract class GameObject implements Drawable {
protected Master master; protected Master master;
protected int layer;
public GameObject(double x, double y, double xSize, double ySize) { public GameObject(double x, double y, double xSize, double ySize) {
this(new Vector2D(x, y), new Vector2D(xSize, ySize)); this(new Vector2D(x, y), new Vector2D(xSize, ySize));
} }
@ -33,6 +35,7 @@ public abstract class GameObject implements Drawable {
this.velocity = new Vector2D(); this.velocity = new Vector2D();
mainColor = Color.BLACK; mainColor = Color.BLACK;
this.master = Master.getMaster(); this.master = Master.getMaster();
this.layer = 0;
} }
@ -133,4 +136,8 @@ public abstract class GameObject implements Drawable {
public Vector2D getCenterPosition() { public Vector2D getCenterPosition() {
return new Vector2D(position.x - size.x / 2, position.y - size.y / 2); return new Vector2D(position.x - size.x / 2, position.y - size.y / 2);
} }
public int getLayer() {
return layer;
}
} }

View file

@ -1,10 +1,10 @@
package objects.ships; package objects.ships;
import core.Coords; import core.math.Coords;
import core.math.Vector2D; import core.math.Vector2D;
import core.physics.Collidable; import core.physics.Collidable;
import core.physics.Hitbox; import core.physics.hitboxes.Hitbox;
import core.physics.RectHitBox; import core.physics.hitboxes.RectHitBox;
import objects.GameObject; import objects.GameObject;
import java.awt.*; import java.awt.*;

View file

@ -2,8 +2,8 @@ package objects.world;
import core.math.Vector2D; import core.math.Vector2D;
import core.physics.Collidable; import core.physics.Collidable;
import core.physics.Hitbox; import core.physics.hitboxes.Hitbox;
import core.physics.RectHitBox; import core.physics.hitboxes.RectHitBox;
import objects.GameObject; import objects.GameObject;
import java.awt.*; import java.awt.*;

View file

@ -1,6 +1,7 @@
package core.physics; package core.physics;
import core.math.Vector2D; import core.math.Vector2D;
import core.physics.hitboxes.RectHitBox;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;

Binary file not shown.