#Error in the Collision of the Player Character

1 messages · Page 1 of 1 (latest)

solar river
#

Hi! I hope you are having a good day or night or whatever. I come to you because I am currently working on a game project and I am stuck doing the collisions because for some reason the invisible rectangle made to serve has the collision box of the character is offset, like its not on the center of the character. When it collides with a tile on the right it creates a bigger gap than it does on the left.

I have been following this tutorial and so far it has been incredibly helpful, until now.
https://youtu.be/oPzPpUcDiYY?si=W98I88btmC0LyZrb

I also will provide my own code if necessary (Which I guess it is going to be), but it is not going to fit here so, yeah.

In this video, we will implement collision detection so player character stops when he tries to walk through solid tiles.

Caution: This is a tutorial for Java 2D beginners so it's pretty verbose! (and the pacing is a bit slow...)

If you want to use my assets (images/maps/sound) that are used in this tutorial, here's the link:
https://drive.goo...

▶ Play video
uneven vergeBOT
#

<@&987246883653156906> please have a look, thanks.

solar river
#

I will put here the code that is important for collisions

soft rune
#

can we see the code?

solar river
#

I am struggling to put it here 😭

soft rune
#

ah got it

solar river
#

First part of the Collision Checker


import Entity.Entity;
public class CollisionChecker {
    
    GameMenu gp;
    
    public CollisionChecker (GameMenu gp) {
        this.gp = gp;
    }
    
    public void checkTile (Entity entity) {
        int entityleftWorldX = entity.worldx + entity.solidArea.x;
        int entityrightWorldX = entity.worldx + entity.solidArea.x + entity.solidArea.width;
        int entitytopWorldY = entity.worldy + entity.solidArea.y;
        int entitybottomWorldY = entity.worldy + entity.solidArea.y + entity.solidArea.height;
        
        int entityleftcol = entityleftWorldX / gp.tileSize;
        int entityrightcol = entityrightWorldX / gp.tileSize;
        int entitytoprow = entitytopWorldY / gp.tileSize;
        int entitybottomrow = entitybottomWorldY / gp.tileSize;
        
        int tileNum1, tileNum2;```
#
            case "up":
                entitytoprow = (entitytopWorldY - entity.speed) / gp.tileSize;
                tileNum1 = gp.tileM.mapTileNum[entityleftcol][entitytoprow];
                tileNum2 = gp.tileM.mapTileNum[entityrightcol][entitytoprow];
                if (gp.tileM.tile[tileNum1].collision == true || gp.tileM.tile[tileNum2].collision == true) {
                    entity.collisionOn = true;
                }
                break;
            case "down":
                entitybottomrow = (entitybottomWorldY + entity.speed) / gp.tileSize;
                tileNum1 = gp.tileM.mapTileNum[entityleftcol][entitybottomrow];
                tileNum2 = gp.tileM.mapTileNum[entityrightcol][entitybottomrow];
                if (gp.tileM.tile[tileNum1].collision == true || gp.tileM.tile[tileNum2].collision == true) {
                    entity.collisionOn = true;
                }
                break;
            case "left":
                entityleftcol = (entityleftWorldX - entity.speed) / gp.tileSize;
                tileNum1 = gp.tileM.mapTileNum[entityleftcol][entitytoprow];
                tileNum2 = gp.tileM.mapTileNum[entityleftcol][entitybottomrow];
                if (gp.tileM.tile[tileNum1].collision == true || gp.tileM.tile[tileNum2].collision == true) {
                    entity.collisionOn = true;
                }
                break;
            case "right":
                entityrightcol = (entityrightWorldX + entity.speed) / gp.tileSize;
                tileNum1 = gp.tileM.mapTileNum[entityrightcol][entitytoprow];
                tileNum2 = gp.tileM.mapTileNum[entityrightcol][entitybottomrow];
                if (gp.tileM.tile[tileNum1].collision == true || gp.tileM.tile[tileNum2].collision == true) {
                    entity.collisionOn = true;
                }
                break;
        }
        
    }
    
}```
uneven vergeBOT
solar river
#

Second part

#

Player Character part 1


import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import main.GameMenu;
import main.KeyHandler;
public class Player extends Entity {
    GameMenu gp;
    KeyHandler keyH;
    
    public final int screenX;
    public final int screenY;
    
    public Player (GameMenu gp, KeyHandler keyH) {
        this.gp = gp;
        this.keyH = keyH;
        
        screenX = gp.screenWidth / 2 - (gp.tileSize / 2);
        screenY = gp.screenHeight / 2 - (gp.tileSize / 2);
        
        solidArea = new Rectangle();
        solidArea.x = 0;
        solidArea.y = 0;
        solidArea.width = 64;
        solidArea.height = 64;
        
        setDefaultValues();
        getPlayerImage();
    } 
    public void setDefaultValues() {
        worldx = gp.tileSize * 23;
        worldy = gp.tileSize * 21;
        speed = 4;
        direction = "down";
    }```
uneven vergeBOT
solar river
#

Player Part 2

        try {
            up1 = ImageIO.read(getClass().getResourceAsStream("/player/Fox_WalkF1.png"));
            up2 = ImageIO.read(getClass().getResourceAsStream("/player/Fox_WalkF2.png"));
            down1 = ImageIO.read(getClass().getResourceAsStream("/player/Fox_WalkB1.png"));
            down2 = ImageIO.read(getClass().getResourceAsStream("/player/Fox_WalkB2.png"));
            left1 = ImageIO.read(getClass().getResourceAsStream("/player/Fox_WalkL1.png"));
            left2 = ImageIO.read(getClass().getResourceAsStream("/player/Fox_WalkL2.png"));
            right1 = ImageIO.read(getClass().getResourceAsStream("/player/Fox_WalkR1.png"));
            right2 = ImageIO.read(getClass().getResourceAsStream("/player/Fox_WalkR2.png"));
            
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public void update() {
        if (keyH.upPressed == true || keyH.downPressed == true || keyH.leftPressed == true || keyH.rightPressed == true) {
           if (keyH.upPressed == true) {
            direction = "up";
            }
           if (keyH.downPressed == true) {
            direction = "down";
           }
           if (keyH.leftPressed == true) {
            direction = "left";
           }
           if (keyH.rightPressed == true) {
            direction = "right";
           }```
uneven vergeBOT
soft rune
#

can you show also what is wrong with running the code

#

with making invisible rectangles to visible

solar river
#

Thats the thing, the rectangle is not visible, there's a player character

#

I mean

#

theres a sprite

soft rune
#

in order to debug it, can you make it visible

#

then u can remove it back again

#

maybe it may be cause by the sprite image, in order to detect it

#

is your sprite is at center for sure?

solar river
#

I actually though about making it visible

But I dont know how to actually do that, I have some nebulous idea of it

And about the sprite, It seems to be at the center of the screen, it is the rectangle that is off.

#

But let me check

soft rune
#

you can add it to the scene

#

you just created but didnt add it to scene according to your code

#

so it stays invisible

solar river
#

Thats not all the code

soft rune
#

can i see the sprite / the code running with the problem

#

is your sprite at center?

solar river
#

I will try to make a video then

soft rune
#

okey

#

by the way its 3am in my country, so i may go to sleep soon

#

if i dont answer you, ping me and i will check in the mornnig

solar river
#

cool

#

I will also try to put a notebook doc with all the relevant code for this

soft rune
#

do u know is your sprite at center or not?

#

i think the problem is about sprite itself

#

if you correctly coded as in the video

#

you should fit the invisible rectangle to your own sprite but u said you dont know how to make it visible

#

if your sprite is not centered, then it causes problem

solar river
#

Had to do it on my phone

soft rune
#

can you show the sprite image

#

not necessary send it

#

i need to see the gaps

solar river
#

ok just let me open aseprite

#

but the sprite is a 32x32 image upscaled inside the game to be 64x64

soft rune
#

okey okey

#

we just need to check that is sprite centered or not

solar river
#

you meant this?

#

because by the sprite not being cetenred it may be the camera not being centered

soft rune
#

it seems no problem okey

#

now you need to fit the invisible rectangle according to this sprit

#

wait

#

is this red rectangle good for you?

solar river
#

yeah

soft rune
#

i couldnt find, where did you create and initialize the invisible rectangle, can you show

#

solidArea = new Rectangle();
solidArea.x = 0;
solidArea.y = 0;
solidArea.width = 64;
solidArea.height = 64;

#

oh here?

solar river
#

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
public class Entity {
    public int worldx, worldy;
    public int speed;
    
    public BufferedImage up1, up2, down1, down2, left1, left2, right1, right2;
    public String direction;
    
    public int spriteCounter = 0;
    public int spriteNum = 1;
    
    public Rectangle solidArea;
    public boolean collisionOn = false;
}```
uneven vergeBOT
soft rune
#

okey

#

solidArea = new Rectangle();
solidArea.x = 10;
solidArea.y = 11;
solidArea.width = 12;
solidArea.height = 15;

#

can you try this?

#

in Player class i got them

solar river
#

ok I tried that and it is still weird

soft rune
#

is there a difference?

solar river
#

I will show

soft rune
#

okey

solar river
#

yes

#

the gaps are biger

soft rune
#

so at least we know the error is about fitting the rectangle to the sprite

#

so you must learn that how can you make the rectangle visible

solar river
soft rune
#

it is the same as how did all other things visible

soft rune
#

and i think you need bigger width and height

#

height=22
width=12
x= -11
y= -7

#

i think it would be better

#

so the main logic is, fitting the invisible rectangle to your sprite.
so you need to make it visible until you find the best fitting rectangle.
until you find it, you must change x,y values etc

solar river
sinful topaz
#

Wouldn't you want the bounding box for just the feet/shadow area.

#

for positioning.

#

Not the 2d physical size

#

You want that shadow area to be able to get up into those corners of the walkway.

soft rune
#

good luck to you guys, have fun

#

i will go to sleep its 3:30am here

solar river
#

take care and thanks!

solar river
#

Though right now I am just trying to figure out the correct numbers for it

#

Anyway I will put the code in notes here just in case

sinful topaz
#

In software engineering, rubber duck debugging (or rubberducking) is a method of debugging code by articulating a problem in spoken or written natural language. The name is a reference to a story in the book The Pragmatic Programmer in which a programmer would carry around a rubber duck and debug their code by forcing themselves to explain it, l...

#

Try writing out in english the problem you have in detail as if you were going to bother the busiest developer in the world your problem.

#

It often helps to write as comments what you think you are going to be doing. Later, you remove comments with real code.

solar river
#

so basically just explain the logic of the code?

sinful topaz
#

Yes, in a lot of detail. You will most likely discover your error that way.

#

Showing code without any explanation of what it's supposed to do doesn't really help anyone trying to help you.

#

It looks like your creating a 2.x dimensional game and so probably there is a technique for collision detection that is not just a 2d, this box overlaps that box, solution.

#

So research that.

solar river
uneven vergeBOT
solar river
#

I will try to look up a little

#

there's the code tho

#

But I will try to look a method to draw the rectangle to see how offset it is and maybe find a way to circumvent that

uneven vergeBOT
#

@solar river

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure 👍

soft rune
#

@solar river did you solve it?

real coral