collision detection (CLEAN) working

This commit is contained in:
Asecave
2021-06-22 18:12:12 +02:00
parent 71f6f86769
commit d5c8e7fa42
3 changed files with 59 additions and 14 deletions

View File

@@ -8,7 +8,7 @@ public abstract class Entity implements Ticker {
protected float velX, velY;
protected float accelleration = 0.012f;
protected float friction = 0.2f;
protected float width, height;
protected float width = 1f, height = 1f;
@Override
public void tick() {
@@ -27,4 +27,57 @@ public abstract class Entity implements Ticker {
protected abstract void update();
public void checkTileCollisions(Map map) {
for (int x = (int) (posX - 2); x < posX + 2; x++) {
for (int y = (int) (posY - 2); y < posY + 2; y++) {
if (x >= 0 && x < map.getWidth() && y >= 0 && y < map.getHeight()) {
if (map.getTile(x, y).isCollidable()) {
if (posY + height / 2 > y && posY - height / 2 < y + 1 && posX + width / 2 > x && posX - width / 2 < x + 1) {
float distN = Math.abs((y + 1 + height / 2) - posY);
float distE = Math.abs((x + 1 + width / 2) - posX);
float distS = Math.abs((y - height / 2) - posY);
float distW = Math.abs((x - width / 2) - posX);
if (distN <= distE && distN < distS && distN < distW) {
onBlockCollision(new boolean[] { true, false, false, false }, x, y);
} else if (distE <= distS && distE < distN && distE < distW) {
onBlockCollision(new boolean[] { false, true, false, false }, x, y);
} else if (distS <= distW && distS < distN && distS < distE) {
onBlockCollision(new boolean[] { false, false, true, false }, x, y);
} else if (distW <= distS && distW < distN && distW < distE) {
onBlockCollision(new boolean[] { false, false, false, true }, x, y);
}
}
}
}
}
}
}
public void onBlockCollision(boolean[] sides, int blockX, int blockY) {
if (sides[0]) {
if (velY < 0) {
velY = 0;
}
posY = blockY + 1 + height / 2;
}
if (sides[1]) {
if (velX < 0) {
velX = 0;
}
posX = blockX + 1 + width / 2;
}
if (sides[2]) {
if (velY > 0) {
velY = 0;
}
posY = blockY - height / 2;
}
if (sides[3]) {
if (velX > 0) {
velX = 0;
}
posX = blockX - width / 2;
}
}
}

View File

@@ -1,10 +1,6 @@
package main;
import java.awt.Color;
import java.awt.Graphics2D;
import ea.ActionFigur;
import ea.BoundingRechteck;
import ea.Figur;
public class LivingEntity extends Entity {
@@ -14,15 +10,7 @@ public class LivingEntity extends Entity {
protected float spriteOffsetX, spriteOffsetY;
public LivingEntity(Figur figur, String name) {
actionFigur = new ActionFigur(figur, name) {
@Override
public void zeichnen(Graphics2D g, BoundingRechteck r) {
g.setColor(Color.GREEN);
g.drawRect((int) actionFigur.positionX(), (int) actionFigur.positionY(), (int) actionFigur.getBreite(), (int) actionFigur.getHoehe());
g.drawRect((int) (posX * Tile.getSize()), (int) (posY * Tile.getSize()), (int) (width * Tile.getSize()), (int) (height * Tile.getSize()));
super.zeichnen(g, r);
}
};
actionFigur = new ActionFigur(figur, name);
}
@Override

View File

@@ -14,6 +14,8 @@ public class Player extends LivingEntity {
width = 0.7f;
height = 0.8f;
spriteOffsetY = -0.14f;
posX = 4f;
posY = 4f;
loader.getPlayer(0).animationsGeschwindigkeitSetzen(200);
loader.getPlayer(1).animationsGeschwindigkeitSetzen(50);
@@ -27,6 +29,7 @@ public class Player extends LivingEntity {
@Override
protected void update() {
System.out.println(posX);
if (!((actionFigur.aktuellesVerhalten().equals("strike"))
&& actionFigur.aktuelleFigur().aktuellesBild() < actionFigur.aktuelleFigur().animation().length - 1)) {
if (!((actionFigur.aktuellesVerhalten().equals("swipe"))
@@ -56,6 +59,7 @@ public class Player extends LivingEntity {
zustandSetzen("strike");
}
}
checkTileCollisions(main.getWorld().getCurrentMap());
super.update();
} else {
velX = 0;