John Strauser
John Strauser

Reputation: 3

How do I handle the reaction of a circle and a semi-circle colliding in java?

I have a circle (not player controlled) and a semi-circle (player controlled) colliding incorrectly. The detection method works fine and returns a boolean for when the collision occurs. However, the reaction does not work properly. The problem that I notice the most is that when the semi-circle is not moving the circle goes straight through the semi-circle without changing speeds. Both the circle and the semi-circle have ints called speedX and speedY. For reference, the class ball is the circle and the class player is the semi-circle. My code is as follows:

if(contact==true)
   runBounce();

public void runBounce()
{
    int speedX=ball.getSpeedX()+(player.getSpeedX()/2);
    int speedY=ball.getSpeedY()+(player.getSpeedY()/2);
    if (player.getSpeedX()==0 && player.getSpeedY()==0) 
    {
        bounceOff();
    }
    else
    {
        ball.setSpeedX(speedX);
        ball.setSpeedY(speedY);
    }
}

public void bounceOff()
{
   ball.setSpeedX(ball.getSpeedX()*-1);
   ball.setSpeedY(ball.getSpeedX()*-1);
}

Why does my code not work and what could I do to fix it?

Upvotes: 0

Views: 82

Answers (1)

Tony K.
Tony K.

Reputation: 5605

You last line is your problem...you've used the revers X on Y with a typo.

Upvotes: 2

Related Questions