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

2016/2/4

Wall Collision Issues

awent0428 awent0428

2016/2/4

#
I've had some issues with wall collisions in my. Could someone please tell me how to improve it? Click here to go to the game.
Super_Hippo Super_Hippo

2016/2/5

#
An easy way for collision is this:
1
2
3
4
5
6
7
8
9
10
int dx=0, dy=0;
if (Greenfoot.isKeyDown("right")) dx++;
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("up")) dy--;
if (Greenfoot.isKeyDown("down")) dy++;
if (dx!=0 || dy!=0)
{
    setLocation(getX()+dx, getY()+dy);
    if (isTouching(Box.class)) setLocation(getX()-dx, getY()-dy);
}
awent0428 awent0428

2016/2/5

#
Super_Hippo wrote...
An easy way for collision is this:
1
2
3
4
5
6
7
8
9
10
int dx=0, dy=0;
if (Greenfoot.isKeyDown("right")) dx++;
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("up")) dy--;
if (Greenfoot.isKeyDown("down")) dy++;
if (dx!=0 || dy!=0)
{
    setLocation(getX()+dx, getY()+dy);
    if (isTouching(Box.class)) setLocation(getX()-dx, getY()-dy);
}
I don't need my character to continue moving when a key is pressed, but thanks!
Sirtuck Sirtuck

2016/2/7

#
This is what I use to move my actor and check for walls. If you want to look at the scenario I can upload it or try to explain this one better. The only times you can have problems is if you set the spd variable to be higher than the width/height of the wall.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
double vspd, hspd, spd = 2;
 
public void act()
{
    vspd = Boolean.compare(Greenfoot.isKeyDown("s"), Greenfoot.isKeyDown("w")) * spd;
    hspd = Boolean.compare(Greenfoot.isKeyDown("d"), Greenfoot.isKeyDown("a")) * spd;
    move();
}   
 
private void move() {
    List<Actor> walls = getObjectsInRange(40, Wall.class);
    boolean canMove = true;
     
    for(Actor wall : walls) {
        double rightCheck = (wall.getX() - wall.getImage().getWidth()/2) - (getX() + getImage().getWidth()/2);
        double leftCheck = (getX() - getImage().getWidth()/2) - (wall.getX() + wall.getImage().getWidth()/2);
        double topCheck = (getY() - getImage().getHeight()/2) - (wall.getY() + wall.getImage().getHeight()/2);
        double bottomCheck = (wall.getY() - wall.getImage().getHeight()/2) - (getY() + getImage().getHeight()/2);
         
        if(hspd > 0 && topCheck < 0 && bottomCheck < 0 && leftCheck < 0) {
            if(rightCheck - hspd < 0 && rightCheck != 0) hspd = Math.abs(rightCheck);
            else if(rightCheck == 0) hspd = 0;
        }
         
        if(hspd < 0 && topCheck < 0 && bottomCheck < 0 && rightCheck < 0) {
            if(leftCheck + hspd < 0 && leftCheck != 0) hspd = -Math.abs(leftCheck);
            else if(leftCheck == 0) hspd = 0;
        }
         
        if(vspd < 0 && rightCheck < 0 && leftCheck < 0 && bottomCheck < 0) {
            if(topCheck + vspd < 0 && topCheck != 0) vspd = -Math.abs(topCheck);
            else if(topCheck == 0) vspd = 0;
        }
         
        if(vspd > 0 && rightCheck < 0 && leftCheck < 0 && topCheck < 0) {
            if(bottomCheck - vspd < 0 && bottomCheck != 0) vspd = Math.abs(bottomCheck);
            else if(bottomCheck == 0) vspd = 0;
        }
    }
     
    if(canMove) setLocation(getX() + (int)hspd, getY() + (int)vspd);
}
}
danpost danpost

2016/2/7

#
At line 12 you are declaring a boolean variable called 'canMove' and setting its value to 'true'. At line 41 you are using it as a condition in setting the actors location. However, nowhere in between is there any possibility that its values be changed to 'false'. So, you cannot expect it to NOT move at any time (unless both 'hspd' and 'vspd' have zero value).
Sirtuck Sirtuck

2016/2/7

#
Oh yeah that was from an earlier version and I forgot to remove that part. It should always do the setLocation. Testing it this morning there is a weird case where if you are aligned on the corner and move in that direction then it moves you into the wall. This probably could be fixed by doing a similar check as the others when hspd > 0 && vspd > 0. Not positive but that should give you a good direction.
You need to login to post a reply.