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

2013/3/21

Problems with angles and world edges

welleid welleid

2013/3/21

#
Hi, I have a problem with my small projects. I'm trying to create a small breakout game, but I can't figure how to manage the direction of my ball. My problem is, when the ball reach an Edge, I want it to keep going on with the correct angle. For example, if the ball reach the right edge of the world with an angle of 135° (coordinate system of Greenfoot), i want it to continue with an angle of 225°. I tryied different ways, for example :
        if ( atWorldEdge() ) 
        {
            
            if(getX() < (getWorld().getWidth() + 5)) //If ball is at left edge
            {
                if (getRotation() <= 90)  
                {
                    setRotation(270 + getRotation() );
                }
                
                if (getRotation() >= 270)  
                {
                    setRotation(getRotation() - 270 );
                }
            }

            if(getX() > (getWorld().getWidth() - 5)) //if ball is at right edge
            {
                if (getRotation() <= 180)  
                {
                    setRotation(90 + getRotation() );
                }
                
                if (getRotation() >= 180)  
                {
                    setRotation(getRotation() - 90 );
                }
            } 
            
            if(getY() > (getWorld().getHeight() - 5)) //if ball is at top edge
            {
                if (getRotation() <= 90)  
                {
                    setRotation(90 + getRotation() );
                }
                
                if (getRotation() >= 270)  
                {
                    setRotation(getRotation() + 90 );
                }
            } 
            
            if(getY() < (getWorld().getHeight() + 5))
// if ball is at bottom edge
            {
                Greenfoot.stop();
            }
        }
I think my way of defining the edges is wrong, and my calculus method is also wrong. Thanks for the help guys !
Game/maniac Game/maniac

2013/3/21

#
you have over complicated it, try this:
        move(1);
        if(atWorldEdge()){
            if(getX() < 5 || getX() > getWorld().getWidth() - 5){
                setRotation(180-getRotation());
            }else if(getY() < 5){
                setRotation(360-getRotation());
            }else if(getY() > getWorld().getHeight() - 5){
                Greenfoot.stop();
            }
            if(getRotation()==0 || getRotation()==90 || getRotation()==180 || getRotation()==270)
            {
                setRotation(getRotation()+10);
            }
            move(1);
        }
danpost danpost

2013/3/22

#
@Game/maniac, when moving one pixel at a time, the ball can only travel in one of the four cardinal directions. The following takes into account the direction of travel on the bounce. This prevents the ball from glitching on the edges without having to move away from the edge immediately.
if ( ( getX() < getImage().getWidth() / 2 && ( getRotation() > 90 && getRotation() < 270 ) ) ||
               ( getX() > getWorld().getWidth() - getImage.getWidth() / 2 && ( getRotation() > 270 || getRotation() < 90 ) ) )
{
    setRotation( 360 - getRotation() );
}
if ( getY() < getImage().getHeight() / 2 && getRotation() > 180 )
{
    setRotation(540-getRotation());
}
if ( getY() > getWorld().getHeight() - getImage().getHeight() / 2)
{
    Greenfoot.stop();
}
You can still add a random chance of a slight turn on the bounce which would prevent infinite bouncing between two objects and/or edges.
welleid welleid

2013/3/22

#
Thanks guys it works perfectly ! :D
Game/maniac Game/maniac

2013/3/22

#
@danpost I had forgot to put that it works best with the smooth mover class
danpost danpost

2013/3/22

#
For some really intense bounce coding, see the movement code of the Ball class in my Pong Pinball scenario.
You need to login to post a reply.