mirror of
https://github.com/Noratrieb/Java2DGame.git
synced 2026-01-14 14:05:01 +01:00
better collision
This commit is contained in:
parent
90c5950e0b
commit
69f784b913
8 changed files with 16 additions and 32 deletions
|
|
@ -2,6 +2,7 @@ package core;
|
|||
|
||||
import core.math.Vector2D;
|
||||
import core.physics.Collidable;
|
||||
import core.physics.Collision;
|
||||
import objects.DebugPos;
|
||||
import objects.ships.BattleShip;
|
||||
import objects.GameObject;
|
||||
|
|
@ -217,21 +218,19 @@ public class Master extends JPanel {
|
|||
/**
|
||||
* Check whether a collidables collide with another one
|
||||
*
|
||||
* @param col The collidable to be checked
|
||||
* @param collidable The collidable to be checked
|
||||
* @return True if it collides with something, false if it doesn't - Should return a Collision
|
||||
*/
|
||||
@Deprecated
|
||||
//TODO make it return a Collision
|
||||
public boolean doesCollide(Collidable col) {
|
||||
boolean collides = false;
|
||||
public Collision doesCollide(Collidable collidable) {
|
||||
Collision collides = null;
|
||||
|
||||
for (Collidable c : collidables) {
|
||||
double distance = Vector2D.distance(c.getCenterPos(), col.getCenterPos());
|
||||
for (Collidable other : collidables) {
|
||||
double distance = Vector2D.distance(other.getCenterPos(), collidable.getCenterPos());
|
||||
|
||||
if (c != col && (distance < c.getHitbox().getSize() + col.getHitbox().getSize())) {
|
||||
if (other != collidable && (distance < other.getHitbox().getSize() + collidable.getHitbox().getSize())) {
|
||||
|
||||
if (c.collidesWith(col)) {
|
||||
collides = true;
|
||||
if (other.getHitbox().collidesWith(collidable.getHitbox())) {
|
||||
collides = new Collision(collidable, other);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import core.physics.hitboxes.Hitbox;
|
|||
|
||||
public interface Collidable {
|
||||
|
||||
boolean collidesWith(Collidable o);
|
||||
Hitbox getHitbox();
|
||||
Vector2D getCenterPos();
|
||||
Vector2D getSize();
|
||||
|
|
|
|||
|
|
@ -5,15 +5,12 @@ package core.physics;
|
|||
*/
|
||||
public class Collision {
|
||||
|
||||
Collidable a;
|
||||
Collidable b;
|
||||
private final Collidable a;
|
||||
private final Collidable b;
|
||||
|
||||
boolean haveCollided;
|
||||
|
||||
public Collision(Collidable a, Collidable b, boolean haveCollided) {
|
||||
public Collision(Collidable a, Collidable b) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.haveCollided = haveCollided;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -31,8 +28,4 @@ public class Collision {
|
|||
public Collidable getB() {
|
||||
return b;
|
||||
}
|
||||
|
||||
public boolean isHaveCollided() {
|
||||
return haveCollided;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package core.physics.hitboxes;
|
|||
|
||||
import core.Drawable;
|
||||
import core.math.Vector2D;
|
||||
import core.physics.Collision;
|
||||
|
||||
public abstract class Hitbox implements Drawable {
|
||||
|
||||
|
|
@ -15,6 +16,8 @@ public abstract class Hitbox implements Drawable {
|
|||
|
||||
public abstract double getSize();
|
||||
|
||||
public abstract boolean collidesWith(Hitbox other);
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return super.toString();
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ public abstract class GameObject implements Drawable {
|
|||
|
||||
if (this instanceof Collidable) {
|
||||
((Collidable) this).getHitbox().moveTo(position, size);
|
||||
if (master.doesCollide((Collidable) this)) {
|
||||
if (master.doesCollide((Collidable) this) != null) {
|
||||
this.position = oldPos;
|
||||
((Collidable) this).getHitbox().moveTo(oldPos, size);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,11 +33,6 @@ public class Submarine extends GameObject implements Collidable {
|
|||
moveTo(centerRelPos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean collidesWith(Collidable o) {
|
||||
return hitbox.collidesWith(o.getHitbox());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Hitbox getHitbox() {
|
||||
return hitbox;
|
||||
|
|
|
|||
|
|
@ -26,11 +26,6 @@ public class Wall extends GameObject implements Collidable {
|
|||
public void update() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean collidesWith(Collidable o) {
|
||||
return this.hitbox.collidesWith(o.getHitbox());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Hitbox getHitbox() {
|
||||
return hitbox;
|
||||
|
|
|
|||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue