2D Gaming - How to deflect the ball off the bat?

I am new to XNA and game developer and am stuck with ball reflection. My ball reflects when it hits the bat, but only at one angle, no matter what angle the bat is at.

Here's the code:

if (BallRect.Intersects(BatRect))
    {
        Vector2 NormBallVelocity = Ball.velocity;
        NormBallVelocity.Normalize();
        NormBallVelocity = Vector2.Reflect(Ball.velocity, NormBallVelocity);
        Ball.velocity = NormBallVelocity;
    }

      

The ball moves back. How do I make the ball bounce off the bat?

I've seen other posts, but they are on the 3D front. I am too new to translate it into 2D terms ...

+2


a source to share


3 answers


I'm sure you need to reflect the normal bat, not the speed of the ball, and that's okay.



+4


a source


Referring to the MSDN Reflection Method , it looks like you want to reflect the speed and rate of a bat.



+1


a source


You should probably first calculate how far from the horizontal the bat is (like how far it is in the swing), then rotate it 90 degrees to get the angle the ball needs.

-1


a source







All Articles