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

2013/10/21

Bouncing - Ball stuck at the edge

Niles Niles

2013/10/21

#
Hey, I have a simple scenario where a ball simply bounces when it hits the wall. Turning 180 degrees does not bounce off according to the angle. In order for it to bounce off walls I used:
1
setRotation(getRotation()*-1);
This works vertically but horizontally the ball gets stuck. Here is the code for the ball:
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
import greenfoot.*;
 
public class ball extends Actor
{
    /**
     * Act - do whatever the ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        move(5);
        checkWalls();
    }   
 
    public void checkWalls(){
        if(atWorldEdge()){
            setRotation(getRotation()*-1);
        }
    }
 
    public boolean atWorldEdge() 
    
        if(getX() < 10 || getX() > getWorld().getWidth() - 10
            return true
        if(getY() < 10 || getY() > getWorld().getHeight() - 10
            return true
        else 
            return false
    
}
Zamoht Zamoht

2013/10/21

#
Changed the code a little and made sure that the ball does not get stuck in 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
public class ball extends Actor 
    private int oldEdge;
    /**
     * Act - do whatever the ball wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */ 
    public void act()  
    
        move(5); 
        checkWalls(); 
    }     
   
    public void checkWalls(){ 
        int edge = atWorldEdge();
        if(edge >= 0 && edge != oldEdge){ 
            setRotation(getRotation()*-1+(180*(edge%2))); 
        
        oldEdge = edge;
    
   
    public int atWorldEdge()   
    {   
        if(getX() < 10) return 1;
        if(getX() > getWorld().getWidth() - 10) return 3;   
        if(getY() < 10) return 2;
        if (getY() > getWorld().getHeight() - 10) return 4;
        return -1;
    }   
}
Niles Niles

2013/10/21

#
Thanks!
chamuzi3 chamuzi3

2014/10/4

#
same problem here i cant seem to make it bounce off that horizontal world edge.
danpost danpost

2014/10/4

#
Try using 'move(-5)' before changing the rotation (see line 17 of the code Zamoht supplied). You may be able to remove 'oldEdge' from the code after inserting this line. However, I think it could still be buggy if both edges are encountered simultaneously. Maybe simplifying might help. Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class ball extends greenfoot.Actor 
    public void act()  
    
        move(5); 
        checkWalls(); 
    }     
   
    public void checkWalls()
    
        boolean atXEdge = getX() < 10 || getX() > getWorld().getWidth()-10;
        boolean atYEdge = getY() < 10 || getY() > getWorld().getHeight()-10;
        if (atXEdge || atYEdge) move(-5);
        if (atXEdge) setRotation(540-getRotation());
        if (atYEdge) setRotation(360-getRotation());
    
}
You need to login to post a reply.