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

2013/9/23

Can anyone help with the Code for bouncing an object

JERRY**5 JERRY**5

2013/9/23

#
object may be like a stone , ball , etc.
Gevater_Tod4711 Gevater_Tod4711

2013/9/23

#
If you mean that your object should bounce of with equal angles you can use this method:
public void bounceOffWorldEdge()  {
        if (getX() < 20) {
            if (getRotation() < 180) {
                setRotation(180-getRotation());
            }
            else if (getRotation() >= 180) {
                setRotation((180-getRotation()));
            }
            if (getX() < 20) {
                for (int i = 0; i < 5 && getX() < 20; i++) {
                    setLocation(getX()+5, getY());
                }
            }
        }
        else if (getX() > getWorld().getWidth()-20) {
            if (getRotation() <= 90) {
                setRotation(180-getRotation());
            }
            else if (getRotation() > 270) {
                setRotation(180+(360-getRotation()));
            }
            if (getX() > getWorld().getWidth()-20) {
                for (int i = 0; i < 5 && getX() > getWorld().getWidth()-20; i++) {
                    setLocation(getX()-5, getY());
                }
            }
        }
        else if (getY() < 20) {
            if (getRotation() <= 270) {
                setRotation(90+(270-getRotation()));
            }
            else if (getRotation() > 270) {
                setRotation(90-(getRotation()-270));
            }
            if (getY() < 20) {
                for (int i = 0; i < 5 && getY() < 20; i++) {
                    setLocation(getX(), getY()+5);
                }
            }
        }
        else if (getY() > getWorld().getHeight() - 20) {
            if (getRotation() <= 90) {
                setRotation(270+(90-getRotation()));
            }
            else if (getRotation() > 90) {
                setRotation(270-(getRotation()-90));
            }
            if (getY() > getWorld().getHeight() - 20) {
                for (int i = 0; i < 5 && getY() > getWorld().getHeight() - 20; i++) {
                    setLocation(getX(), getY()-5);
                }
            }
        }
    }
Otherwhise you have to specify what you mean with bouncing an object.
JERRY**5 JERRY**5

2013/9/23

#
The idea of my scenerio is that balls should come randomly from any top corner of the screen and should exit from the other side of the screen . The ball should bounce from one side of the screen and keep bouncing till it reaches the other end
danpost danpost

2013/9/23

#
To randomly spawn objects from either top corner use the following in the world class:
// for a world class act method
public void act()
{
    if (Greenfoot.getRandomNumber(100) < 2)
    {
        int rand = Greenfoot.getRandomNumber(2);
        addObject(new Ball(1-2*rand), (getWidth()-1)*rand, 32);
    }
}
// for the Ball class, use the following instance fields
int direction; // horizontal direction; assigned in constructor
int vSpeed = 0; // vertical speed
int hSpeed = 2 // horizontal speed; adjust as needed
// for the Ball constructor
public Ball(int dir)
{
    direction = dir; // save horizontal direction to move
}
// Ball class act method
public void act()
{
    setLocation(getX(), getY()+(++vSpeed)); // move vertically adding gravity
    if (getY() >= getWorld().getHeight()-getImage().getHeight()/2 && vSpeed > 0) vSpeed = -vSpeed; // check height, bounce
    setLocation(getX()+direction*hSpeed, getY()); // move horizontally
    if (getX()-(getWorld().getWidth()-1)*(direction+1)/2 == 0) getWorld().removeObject(this); // check edge, remove
}
JERRY**5 JERRY**5

2013/9/24

#
Thanks
JERRY**5 JERRY**5

2013/9/24

#
All the balls falls from the same co-ordinate and if we want to change the co-ordinates then what to do. whenever the ball falls it should change its co-ordinate.
vela vela

2015/4/15

#
how to make it bounce only when it hit the block and come back down?
danpost danpost

2015/4/15

#
@vela, please do not revive old discussion threads. Start a new one. Explain what you are trying to do, first in general, then in detail. Supply attempted code. Be specific.. Include all relevant information and codes necessary to help resolve the issue.
You need to login to post a reply.