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

2017/3/7

Bounce off walls at opposite angle

101929ha 101929ha

2017/3/7

#
From another forum , I found code for bouncing off walls at an equal and opposite angle, however, the code is incomplete and I cannot work it out for the top and left walls. this is what I have:
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 void bounceoffwall()
{
     if (isAtEdge())
    {
         if (getY() == 0 || getY() == getWorld().getHeight()-1) /**bottom*/
        {
            setRotation(360-getRotation());
        }
         if (getX()==0 || getX() == getWorld().getWidth()-1) /**right*/
        {
            setRotation(180-getRotation());
        }
        if (getY() == 0) /**top*/
        {
            setRotation(int-getRotation());
        }
        if (getX() == 0) /**left*/
        {
            setRotation(int-getRotation());
        }
        
         
 
         
        //left edge:  getX() == 0
        //right edge:  getX() == getWorld().getWidth()-1
        //top edge:  getY() == 0
        //bottom edge:  getY() == getWorld().getHeight()-1
    }
}
Please help and thanks in advance.
danpost danpost

2017/3/7

#
It appears that you tried to add the extra code from line 13 through to line 20 (and probably the comments as well). Remove those lines. This will probably result in what you got from the other forum. I re-wrote and simplified the final code below and revised the comments to show what there actually was in the original code:
1
2
3
4
5
6
7
8
9
10
11
public void bounceoffwall()
{
    if (getY() == 0 || getY() == getWorld().getHeight()-1) /** top or bottom */
    {
        setRotation(360-getRotation());
    }
    if (getX()==0 || getX() == getWorld().getWidth()-1) /** left or right */
    {
        setRotation(180-getRotation());
    }
}
101929ha 101929ha

2017/3/8

#
YAY! It works. Thanks danpost!
You need to login to post a reply.