This site requires JavaScript, please enable it in your browser!
Greenfoot back
Th0t_slayer
Th0t_slayer wrote ...

2022/1/18

Wall collision not working

Th0t_slayer Th0t_slayer

2022/1/18

#
Hi there, i'm having problem with the collision detection, the player is supposed to stop when he reaches the wall, either by approaching it from the right or the left, but when he approaches it from the left he just teleports to the right! Here's the code:
private void checkIsOnWall(){
            Actor wall=getOneIntersectingObject(VerticalWall.class);
            if(wall!=null){
            if(velocityX>=0){
                velocityX=0;
                setLocation(wall.getX()-getImage().getWidth()+15, getY());  
            }//this part doesn't work
            if(velocityX<=0){
                velocityX=0;
                setLocation(wall.getX()+getImage().getWidth()-15, getY());
                }
            } 
        }
    }
danpost danpost

2022/1/18

#
To get a better idea of what your code does, here it is again:
private void checkIsOnWall()
{
    Actor wall = getOneIntersectingObject(VerticalWall.class);
    if (wall != null)
    {
        if (velocityX >= 0)
        {
            velocityX = 0;
            setLocation(wall.getX()-getImage().getWidth()+15, getY());  
        }//this part doesn't work
        if (velocityX <= 0)
        {
            velocityX = 0;
            setLocation(wall.getX()+getImage().getWidth()-15, getY());
        }
    } 
}
I presume the last closing curly bracket, which I omitted here, was for closing the class codes and was extraneous to the method.
danpost danpost

2022/1/18

#
I have a feeling the signs on the plus and minus 15 in the setLocation statements are backward.
Th0t_slayer Th0t_slayer

2022/1/23

#
I tried to change the plus and minus 15 but it still doesn't work, it makes the player teleport from the left side to the right side of the wall. I really don't know why
danpost danpost

2022/1/24

#
Th0t_slayer wrote...
I tried to change the plus and minus 15 but it still doesn't work, it makes the player teleport from the left side to the right side of the wall. I really don't know why
Try this (presuming your world cell size is equal to one):
public void checkIsOnWall()
{
    Actor wall= getOneIntersectingObject(VerticalWall.class);
    if (actor == null) return;
    int direction = (int)Math.signum(velocityX);
    setLocation(wall.getX()-direction*(wall.getImage().getWidth()+getImage().getWidth()+1)/2, getY());
    velocityX = 0;
}
Spock47 Spock47

2022/1/24

#
I guess the problem is that BOTH if blocks are executed if the player collides with the wall from the left. Let's have a detailed look at that scenario (colliding with a wall with velocityX > 0):
private void checkIsOnWall()
{
    Actor wall = getOneIntersectingObject(VerticalWall.class);
    if (wall != null)
    {
        if (velocityX >= 0)
        {
            velocityX = 0;
            setLocation(wall.getX()-getImage().getWidth()+15, getY());  
        }//this part doesn't work
        if (velocityX <= 0)
        {
            velocityX = 0;
            setLocation(wall.getX()+getImage().getWidth()-15, getY());
        }
    } 
}
- (line 6) first, the program enters the if-block for condition "velocityX >= 0". - (line 8) velocityX is set to 0. - (line 11) since velocityX is now 0, the second if-block is entered. - (line 14) the position of the player is set to "right of the wall". There you see the problem. There are twothree easy possibilities to fix this: 1. Just add an "else" in front of the second if -> "else if (velocityX <= 0)" This way you can be sure that only one of the two if-blocks are executed for any situation. 2. Does it really make sense to enter any of the two if-blocks if velocityX is 0? If you only collide with a wall because of the player moving, this should not be possible. In that case, you could change the if-conditions to "if (velocityX > 0)" and "if (velocityX < 0)". If on the other hand, the game also sees the possibility that a player gets somehow thrown into the wall without normal movement ("teleportation") and you want to make sure that the player immeditately gets thrown out of the wall, then solution possibility 1 above is your choice. Edit: 3. danpost was faster than me again. So using his solution is another easy possibility to fix it. ;) Note: Where does the "15" come from? Would this value still make sense if the image width would be different? See also: https://en.wikipedia.org/wiki/Magic_number_(programming) Live long and prosper, Spock47
You need to login to post a reply.