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

2018/1/21

Wall collision

ImAndrew ImAndrew

2018/1/21

#
I have this method and it have some bug which makes the character to slide over the wall until I move it.The bug starts when I'm touching the wall and pressing 2 buttons at a time.
    void collision()
    {
        if(this.isTouching(Wall.class))
        {
            if(direction == 1) //up
            {
                setLocation(getX(), getY() + speed);
            }
            else if(direction == 2)//down
            {
                setLocation(getX(), getY() - speed);
            }
            else if(direction == 3)//right
            {
                setLocation(getX()  - speed, getY());
            }
            else if(direction == 4)//left
            {
                setLocation(getX()  + speed, getY());
            }
        }
    }
CxVercility CxVercility

2018/1/21

#
Shouldn't it be checking for non-collision?
ImAndrew ImAndrew

2018/1/21

#
CxVercility wrote...
Shouldn't it be checking for non-collision?
What do you mean?
danpost danpost

2018/1/21

#
CxVercility wrote...
Shouldn't it be checking for non-collision?
Same difference -- opposite sign. If non-collision is false -- then collision has occurred.
ImAndrew wrote...
I have this method and it have some bug which makes the character to slide over the wall until I move it.The bug starts when I'm touching the wall and pressing 2 buttons at a time. << Code Omitted
Maybe it is in how you determine the value of the 'direction' field (or variable). Or, maybe it in how you coded the movement that occurs prior to the collision checking. Please show more complete codes for class.
CxVercility CxVercility

2018/1/21

#
danpost wrote...
CxVercility wrote...
Shouldn't it be checking for non-collision?
Same difference -- opposite sign. If non-collision is false -- then collision has occurred.
ImAndrew wrote...
I have this method and it have some bug which makes the character to slide over the wall until I move it.The bug starts when I'm touching the wall and pressing 2 buttons at a time. << Code Omitted
Maybe it is in how you determine the value of the 'direction' field (or variable). Or, maybe it in how you coded the movement that occurs prior to the collision checking. Please show more complete codes for class.
IDK what you mean but i meant that you should only be able to move when ure not touching a wall, so it should be !this.isTouching Then again, only one of the directions should be blocked by the wall.
danpost danpost

2018/1/21

#
CxVercility wrote...
i meant that you should only be able to move when ure not touching a wall
Logically speaking, you are saying that if you are touching a wall, then you should not be able to move. Now, that makes no sense.
CxVercility CxVercility

2018/1/21

#
danpost wrote...
CxVercility wrote...
i meant that you should only be able to move when ure not touching a wall
Logically speaking, you are saying that if you are touching a wall, then you should not be able to move. Now, that makes no sense.
As i have added, "Then again, only one of the directions should be blocked", u cant walk left when there's a wall to your left. Why would you check for wallcollision in order to move?The way he has set it up u can only move when touching a wall Also, else-if only checks until one of them is true and skips all the others then, remove the else and you should be able to use multiple keys
danpost danpost

2018/1/22

#
CxVercility wrote...
As i have added, "Then again, only one of the directions should be blocked", u cant walk left when there's a wall to your left.
That has nothing to do with what the following:
Logically speaking, you are saying that if you are touching a wall, then you should not be able to move. Now, that makes no sense.
CxVercility wrote...
Why would you check for wallcollision in order to move?The way he has set it up u can only move when touching a wall
I seriously doubt that all the movement code was given. The only part given was the collision checking, which (hopefully) reverses the original movement. That is why I asked for more code to be provided.
else-if only checks until one of them is true and skips all the others then, remove the else and you should be able to use multiple keys
The value of 'direction' cannot be two values at the same time; so the use of 'else-if' in the provided code is not a problem.
CxVercility CxVercility

2018/1/22

#
Didn't see that actually, error on my part. We'll see when he posts the rest of the code.
ImAndrew ImAndrew

2018/1/23

#
Actually, after some thoughts I created a new system using the getOneobjectAtOffset method because the isTouching method wasn't good for my collision. Thanks anyway ^-^
You need to login to post a reply.