I’m working on a JavaFX project inspired by a game creation tutorial by Rysnow on YouTube (https://www.youtube.com/watch?v=oPzPpUcDiYY&t=1225s). I’ve followed the tutorial closely but am having trouble with the collision detection part. Despite printing the coordinates to debug, I can’t seem to get it working correctly.
Here is what I’ve tried so far:
- Followed the tutorial step-by-step
- Printed the coordinates of the hero to check the positions
I’ve uploaded my project files here: MediaFire link.
Could anyone help me figure out what I’m doing wrong with the collision detection? Any insights or advice would be greatly appreciated.
Thank you!
*
import HERO.Entity;
public class CollisionChecker {
ScreenSettings sSettings;
public CollisionChecker(ScreenSettings sSettings) {
this.sSettings = sSettings;
}
public void checkTile(Entity entity) {
int leftX = entity.worldX + (int) entity.rec.getX();
int rightX = entity.worldX + (int) (entity.rec.getX() + entity.rec.getWidth());
int topY = entity.worldY + (int) entity.rec.getY();
int bottomY = entity.worldY + (int) (entity.rec.getY() + entity.rec.getHeight());
int entityLeftCol = leftX / sSettings.tileSize;
int entityRightCol = rightX / sSettings.tileSize;
int entityTopRow = topY / sSettings.tileSize;
int entityBottomRow = bottomY / sSettings.tileSize;
int tileNum1, tileNum2;
switch (entity.direction) {
case "top":
entityTopRow = (topY - entity.speed) / sSettings.tileSize;
tileNum1 = sSettings.cMap.map[entityLeftCol][entityTopRow];
tileNum2 = sSettings.cMap.map[entityRightCol][entityTopRow];
System.out.println(sSettings.cMap.tile[tileNum1].collision);
if (sSettings.cMap.tile[tileNum1].collision || sSettings.cMap.tile[tileNum2].collision) {
// entity.collision = true;
}
break;
case "down":
entityBottomRow = (bottomY + entity.speed) / sSettings.tileSize;
tileNum1 = sSettings.cMap.map[entityLeftCol][entityBottomRow];
tileNum2 = sSettings.cMap.map[entityRightCol][entityBottomRow];
System.out.println(sSettings.cMap.tile[tileNum1].collision);
if (sSettings.cMap.tile[tileNum1].collision || sSettings.cMap.tile[tileNum2].collision) {
entity.collision = true;
}
break;
case "left":
entityLeftCol = (leftX - entity.speed) / sSettings.tileSize;
tileNum1 = sSettings.cMap.map[entityLeftCol][entityTopRow];
tileNum2 = sSettings.cMap.map[entityLeftCol][entityBottomRow];
System.out.println(sSettings.cMap.tile[tileNum1].collision);
if (sSettings.cMap.tile[tileNum1].collision || sSettings.cMap.tile[tileNum2].collision) {
entity.collision = true;
}
break;
case "right":
entityRightCol = (rightX + entity.speed) / sSettings.tileSize;
tileNum1 = sSettings.cMap.map[entityRightCol][entityTopRow];
tileNum2 = sSettings.cMap.map[entityRightCol][entityBottomRow];
System.out.println(sSettings.cMap.tile[tileNum1].collision);
if (sSettings.cMap.tile[tileNum1].collision || sSettings.cMap.tile[tileNum2].collision) {
entity.collision = true;
}
break;
}
}
}
``
``` public Hero(ScreenSettings settings) {
this.settings = settings;
this.control = this.settings.control;
this.canvas = new Canvas(settings.WIDTH, settings.HEIGHT);
this.screenX= settings.WIDTH /2 -(settings.tileSize/2);
this.screenY = settings.HEIGHT /2 -(settings.tileSize/2);
this.tileSize = settings.OrigTile *4;
rec = new Rectangle(8,16,32,32);
rec.setFill(Color.ALICEBLUE);
Default();
}
//core repaint
public void reDrawCharacter(){
Movement();
}
public void Default(){
direction = "left";
worldY = settings.tileSize *10;
worldX = settings.tileSize *10;
gc = canvas.getGraphicsContext2D();
PlayerChangeMovement();
settings.pane.getChildren().add(canvas);
}
public void Movement() {
if (control.top) {
worldY-=speed;
direction="top";
}
if(control.down){
worldY+=speed;
direction="down";
}
if (control.right) {
worldX-=speed;
direction = "right";
}
if(control.left){
worldX+=speed;
direction = "left";
}
collision = false;
settings.cCheker.checkTile(this);
if (!control.left&&!control.right&&!control.down&&!control.top) {
StandBy();
}else{
PlayerChangeMovement();
}
}
```
```package HERO;
import javafx.scene.image.Image;
import javafx.scene.shape.Rectangle;
public class Entity{
Image img;
public int worldX;
public int worldY;
public int speed = 9;
public int spireteNum;
public boolean collision;
public String direction;
public Rectangle rec;
}```