mirror of
https://github.com/Noratrieb/Java2DGame.git
synced 2026-01-14 14:05:01 +01:00
collision detected works now!
This commit is contained in:
parent
70610c7c53
commit
0c1178d1d1
8 changed files with 62 additions and 39 deletions
|
|
@ -41,7 +41,6 @@ public class Coords {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Vector2D getWorldCoordsSize(Vector2D value) {
|
public static Vector2D getWorldCoordsSize(Vector2D value) {
|
||||||
//TODO h not w
|
|
||||||
double x = (value.x / Master.SCREEN_Y_COORDINATES * master.getH());
|
double x = (value.x / Master.SCREEN_Y_COORDINATES * master.getH());
|
||||||
double y = (value.y / Master.SCREEN_Y_COORDINATES * master.getH());
|
double y = (value.y / Master.SCREEN_Y_COORDINATES * master.getH());
|
||||||
return new Vector2D(x, y);
|
return new Vector2D(x, y);
|
||||||
|
|
|
||||||
|
|
@ -41,8 +41,9 @@ public class Master extends JPanel {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All GameObjects that can be drawn
|
* All GameObjects that can be drawn
|
||||||
|
* Has different render layers, 0 is on the bottom
|
||||||
*/
|
*/
|
||||||
private final ArrayList<Drawable> drawables;
|
private final ArrayList<ArrayList<Drawable>> drawables;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All physics objects that exist
|
* All physics objects that exist
|
||||||
|
|
@ -74,6 +75,7 @@ public class Master extends JPanel {
|
||||||
objectBuffer = new ArrayList<>();
|
objectBuffer = new ArrayList<>();
|
||||||
collidables = new ArrayList<>();
|
collidables = new ArrayList<>();
|
||||||
drawables = new ArrayList<>();
|
drawables = new ArrayList<>();
|
||||||
|
drawables.add(new ArrayList<>());
|
||||||
|
|
||||||
create(new Grid());
|
create(new Grid());
|
||||||
|
|
||||||
|
|
@ -111,7 +113,7 @@ public class Master extends JPanel {
|
||||||
|
|
||||||
Graphics2D g2d = (Graphics2D) g.create();
|
Graphics2D g2d = (Graphics2D) g.create();
|
||||||
|
|
||||||
drawables.forEach(o -> o.draw(g2d));
|
drawables.forEach(l -> l.forEach(o -> o.draw(g2d)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -169,16 +171,45 @@ 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method has to be called for every newly created GameObject
|
||||||
|
*
|
||||||
|
* @param obj The new object
|
||||||
|
* @param renderLayer The render layer the object will be put on, 0 is below everything
|
||||||
|
*/
|
||||||
|
public void create(GameObject obj, int renderLayer) {
|
||||||
objectBuffer.add(obj);
|
objectBuffer.add(obj);
|
||||||
if (obj instanceof Collidable) {
|
if (obj instanceof Collidable) {
|
||||||
collidables.add((Collidable) obj);
|
collidables.add((Collidable) obj);
|
||||||
}
|
}
|
||||||
drawables.add(obj);
|
addDrawable(obj, renderLayer);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addDrawable(Drawable d) {
|
/**
|
||||||
drawables.add(d);
|
* Add a new Drawable to the render list
|
||||||
|
* @param d The drawable
|
||||||
|
* @param layer The layer it should be put on (>=0)
|
||||||
|
*/
|
||||||
|
public void addDrawable(Drawable d, int layer) {
|
||||||
|
|
||||||
|
if (layer < 0) {
|
||||||
|
|
||||||
|
throw new IllegalArgumentException("Layer must be at least 9");
|
||||||
|
}
|
||||||
|
|
||||||
|
//layer exists check
|
||||||
|
int layerDif = layer - (drawables.size()-1);
|
||||||
|
if (layerDif > 0) {
|
||||||
|
for (int i = 0; i < layerDif; i++) {
|
||||||
|
drawables.add(new ArrayList<>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
drawables.get(layer).add(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,8 @@
|
||||||
package core.physics;
|
package core.physics;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class stores information about a collision
|
||||||
|
*/
|
||||||
public class Collision {
|
public class Collision {
|
||||||
|
|
||||||
Collidable a;
|
Collidable a;
|
||||||
|
|
@ -13,10 +16,18 @@ public class Collision {
|
||||||
this.haveCollided = haveCollided;
|
this.haveCollided = haveCollided;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the Collidable a, the one that called the collision testing
|
||||||
|
* @return The Collidable a
|
||||||
|
*/
|
||||||
public Collidable getA() {
|
public Collidable getA() {
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the Collidable b, the one that a collided width
|
||||||
|
* @return The Collidable a
|
||||||
|
*/
|
||||||
public Collidable getB() {
|
public Collidable getB() {
|
||||||
return b;
|
return b;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,11 @@ package core.physics;
|
||||||
|
|
||||||
import core.Drawable;
|
import core.Drawable;
|
||||||
import core.math.Vector2D;
|
import core.math.Vector2D;
|
||||||
import objects.GameObject;
|
|
||||||
|
|
||||||
public abstract class Hitbox implements Drawable {
|
public abstract class Hitbox implements Drawable {
|
||||||
|
|
||||||
|
public abstract void moveTo(Vector2D x1, Vector2D size);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return super.toString();
|
return super.toString();
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package core.physics;
|
package core.physics;
|
||||||
|
|
||||||
|
import core.Coords;
|
||||||
import core.Master;
|
import core.Master;
|
||||||
import core.math.Vector2D;
|
import core.math.Vector2D;
|
||||||
|
|
||||||
|
|
@ -10,8 +11,6 @@ import java.awt.*;
|
||||||
*/
|
*/
|
||||||
public class RectHitBox extends Hitbox {
|
public class RectHitBox extends Hitbox {
|
||||||
|
|
||||||
int w,h;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The corners of the rectangle like this:
|
* The corners of the rectangle like this:
|
||||||
* <p>x1 x2</p>
|
* <p>x1 x2</p>
|
||||||
|
|
@ -30,14 +29,16 @@ public class RectHitBox extends Hitbox {
|
||||||
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));
|
||||||
this.y2 = Vector2D.add(x1, new Vector2D(size.x, size.y));
|
this.y2 = Vector2D.add(x1, new Vector2D(size.x, size.y));
|
||||||
|
Master.getMaster().addDrawable(this, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Move the hitbox to a new position
|
* Move the hitbox to a new position
|
||||||
*
|
*
|
||||||
* @param x1
|
* @param x1 The new position
|
||||||
* @param size
|
* @param size The new size
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public void moveTo(Vector2D x1, Vector2D size) {
|
public void moveTo(Vector2D x1, Vector2D size) {
|
||||||
this.x1 = x1.copy();
|
this.x1 = x1.copy();
|
||||||
this.x2 = Vector2D.add(x1, new Vector2D(size.x, 0));
|
this.x2 = Vector2D.add(x1, new Vector2D(size.x, 0));
|
||||||
|
|
@ -105,8 +106,7 @@ public class RectHitBox extends Hitbox {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean coll = !(allAbove || allBelow || allLeft || allRight);
|
return !(allAbove || allBelow || allLeft || allRight);
|
||||||
return coll;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -151,31 +151,10 @@ public class RectHitBox extends Hitbox {
|
||||||
@Override
|
@Override
|
||||||
public void draw(Graphics2D g2d) {
|
public void draw(Graphics2D g2d) {
|
||||||
|
|
||||||
this.w = w;
|
Vector2D abs = Coords.getWorldCoords(x1);
|
||||||
int h = (int) (w / Master.SCREEN_RATIO);
|
Vector2D sizeAbs = Coords.getWorldCoordsSize(Vector2D.subtract(y2, x1));
|
||||||
int xAbs = (int) getWorldCoords(x1.x, true);
|
|
||||||
int yAbs = (int) getWorldCoords(x1.y, false);
|
|
||||||
int sizeXAbs = (int) getWorldCoordsSize(x2.x - x1.x, true);
|
|
||||||
int sizeYAbs = (int) getWorldCoordsSize(y1.y - x1.y, false);
|
|
||||||
|
|
||||||
g2d.fillRect(xAbs, yAbs, sizeXAbs, sizeYAbs);
|
g2d.drawRect((int)abs.x, (int)abs.y, (int)sizeAbs.y, (int)sizeAbs.y);
|
||||||
g2d.setPaint(Color.MAGENTA);
|
g2d.setPaint(Color.MAGENTA);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public double getWorldCoords(double value, boolean isX) {
|
|
||||||
if (isX) {
|
|
||||||
return (value / (Master.SCREEN_Y_COORDINATES * Master.SCREEN_RATIO) * w);
|
|
||||||
} else {
|
|
||||||
return (value / Master.SCREEN_Y_COORDINATES * h);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public double getWorldCoordsSize(double value, boolean isX) {
|
|
||||||
if (isX) {
|
|
||||||
return (value / Master.SCREEN_Y_COORDINATES * w);
|
|
||||||
} else {
|
|
||||||
return (value / Master.SCREEN_Y_COORDINATES * h);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ public abstract class GameObject implements Drawable {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>The update method is called every frame before the {@link #draw(Graphics2D, int, Master)} method by the master object on each object. Everything
|
* <p>The update method is called every frame before the {@link #draw(Graphics2D)} method by the master object on each object. Everything
|
||||||
* that is needed for the game to work should be here in this method.</p>
|
* that is needed for the game to work should be here in this method.</p>
|
||||||
* <p>No drawing should be made in this method. The {@code debug} method can be called on the master.</p>
|
* <p>No drawing should be made in this method. The {@code debug} method can be called on the master.</p>
|
||||||
* <p>This function is <i>NOT</i> intended to be called manually.</p>
|
* <p>This function is <i>NOT</i> intended to be called manually.</p>
|
||||||
|
|
@ -50,12 +50,14 @@ public abstract class GameObject implements Drawable {
|
||||||
* @param target The target position
|
* @param target The target position
|
||||||
*/
|
*/
|
||||||
public void moveTo(Vector2D target) {
|
public void moveTo(Vector2D target) {
|
||||||
Vector2D oldPos = position;
|
Vector2D oldPos = position.copy();
|
||||||
this.position = target;
|
this.position = target;
|
||||||
|
|
||||||
if (this instanceof Collidable) {
|
if (this instanceof Collidable) {
|
||||||
|
((Collidable) this).getHitbox().moveTo(position, size);
|
||||||
if (master.doesCollide((Collidable) this)) {
|
if (master.doesCollide((Collidable) this)) {
|
||||||
this.position = oldPos;
|
this.position = oldPos;
|
||||||
|
((Collidable) this).getHitbox().moveTo(oldPos, size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue