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

2016/12/8

Differ reflection angles in PONG

rn42v1r rn42v1r

2016/12/8

#
How do I let reflection angles change depending on the part of the paddle that is hit? angle of entrance = angle of reflection becomes boring pretty quickly...
danpost danpost

2016/12/8

#
Use the difference between the y-coordinate values of both the ball and the paddle to determine the adjustment in the reflection angle. You could also add a slight random change on any bounce.
rn42v1r rn42v1r

2016/12/8

#
That worked nicey, well for one paddle at a time... If I do it like this:
    public void bounce()
    {
        if (isTouching(Players.class))
        {   
            BALL_SPEED = BALL_SPEED * -1;
            
            Actor PlayerRight = getOneIntersectingObject(PlayerRight.class);
            int PlayerRightY;
            int PlayerRightX;
            PlayerRightX = PlayerRight.getX();
            PlayerRightY = PlayerRight.getY();
            
            Actor PlayerLeft = getOneIntersectingObject(PlayerLeft.class);
            int PlayerLeftY;
            int PlayerLeftX;
            PlayerLeftY = PlayerLeft.getY();
            PlayerLeftX = PlayerLeft.getX();
            
            if (getY() - PlayerRightY >= 0 && getX() - PlayerRightX <= 1) 
            {
                setRotation(360 - getRotation() - 20);
            }
            if (getY() - PlayerRightY <= 0 && getX() - PlayerRightX <= 1)
            {
                setRotation(360 - getRotation() + 20);
            }
            
            if (getY() - PlayerLeftY >= 0 && PlayerLeftX - getX() <= 1)
            {
                setRotation(360 - getRotation() + 20); 
            }
            
            if (getY() - PlayerLeftY <= 0 && PlayerLeftX - getX() <= 1)
            {
                setRotation(360 - getRotation() - 20);
            }
        }   
        
        if (isAtUpperEdge() || isAtLowerEdge())
        {
            BALL_SPEED = BALL_SPEED * -1;
            setRotation(360 - (getRotation() - 180));
        }
    }
... I receive a NullPointerException when the ball touches one of the paddles However if I do it like this:
    public void bounce()
    {
        if (isTouching(Players.class))
        {   
            BALL_SPEED = BALL_SPEED * -1;
            
            /*Actor PlayerRight = getOneIntersectingObject(PlayerRight.class);
            int PlayerRightY;
            int PlayerRightX;
            PlayerRightX = PlayerRight.getX();
            PlayerRightY = PlayerRight.getY();*/
            
            Actor PlayerLeft = getOneIntersectingObject(PlayerLeft.class);
            int PlayerLeftY;
            int PlayerLeftX;
            PlayerLeftY = PlayerLeft.getY();
            PlayerLeftX = PlayerLeft.getX();
            
            /*if (getY() - PlayerRightY >= 0 && getX() - PlayerRightX <= 1) 
            {
                setRotation(360 - getRotation() - 20);
            }
            if (getY() - PlayerRightY <= 0 && getX() - PlayerRightX <= 1)
            {
                setRotation(360 - getRotation() + 20);
            }*/
            
            if (getY() - PlayerLeftY >= 0 && PlayerLeftX - getX() <= 1)
            {
                setRotation(360 - getRotation() + 20); 
            }
            
            if (getY() - PlayerLeftY <= 0 && PlayerLeftX - getX() <= 1)
            {
                setRotation(360 - getRotation() - 20);
            }
        }   
        
        if (isAtUpperEdge() || isAtLowerEdge())
        {
            BALL_SPEED = BALL_SPEED * -1;
            setRotation(360 - (getRotation() - 180));
        }
    }
... it at least works for the left paddle...
rn42v1r rn42v1r

2016/12/8

#
Nvm, im stupid, just fixed it:P
danpost danpost

2016/12/9

#
I do not know why you are adding and subtracting 20 to the reflection angle -- I thought you wanted to change the reflection angle depending on where it hit the paddle. The following should work regardless of player:
if (isTouching(Player.class))
{
    Actor player = getOneIntersectingObject(Player.class);
    int height = player.getImage().getHeight();
    int yOffset = player.getY()-getY();
    setRotation(20*yOffset/height-getRotation());
}
rn42v1r rn42v1r

2016/12/9

#
It does change the reflection angle depending on where it hits the paddle. If the ball hits the upper (or lower)part its: angle of entrance +20(or -20)°=reflection angle... But Im gonna try your version too since the ball is having some trouble when it hits right the mid of the paddle. Thanks alot:)
danpost danpost

2016/12/9

#
rn42v1r wrote...
Im gonna try your version too since the ball is having some trouble when it hits right the mid of the paddle.
I had edited my code, thinking I had something backward. However, I did have it right the first time. Line 5 above should read:
int yOffset = getY()-player.getY();
EDIT: no wait, the adjustment reverses depending on the player hit. I will get back shortly. Okay -- try this:
int yOffset = (getY()-player.getY())*(int)Math.signum(getX()-player.getX());
rn42v1r rn42v1r

2016/12/11

#
The ball doesn't mind the collision with one of the paddles and goes right through:(
You need to login to post a reply.