views:

231

answers:

3

Hi there I am pretty new to XNA & game dev and am stuck at ball reflection. My ball is reflecting once it hits the bat, but only in one angle, no matter which 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 is retracting its way back. How do I make it look like the ball is reflecting off the bat?

I have seen other posts but they are on 3D front I am too new to translate it to 2D terms...

Thanks Sid

+4  A: 

I'm pretty sure you need to reflect off the bat's normal instead of the ball's velocity and it's normal.

Michael Dorgan
ah beat'd. +1 for that
Robb
A: 

You should probably first calculate how far off horizontal the bat is (as in, how far along it is in the swing), then rotate it 90 degrees to get the angle you need for the ball.

RCIX
This doen't take into account the ball's current velocity. You'll still need to reflect from this vector instead of just assigning it.
Michael Dorgan
Could you please take some time to explain your idea? I have reflected the ball over bat's velocity and it's normal normal but it is still going constantly at 45 degrees in Q2. Why is the ball not taking any variables into consideration?
sid
Basically, if the bat is straight left-right when the ball hits it, the ball should more-or-less go straight back. If the end of the bat (that the player is not holding) is closer to the bottom of the screen than the end the player is (assuming the pitch comes in from the top), then the ball goes left. If it's farther from the screen, then the ball goes right. Sorry for the muddled explanation.
RCIX
@Michael Dorgan: Perhaps i'm not thinking clearly right now, but no matter what the ball's velocity is, when it hits the bat and the bat is at a given angle then the ball will go in the same direction (the distance is another matter).
RCIX
Imagine the bat is being swung around the batter and contacts the ball at a 45 degree angle (the bat is 45 degrees to the incoming ball's velocity) because he swung early. The resultant reflect should have the ball shoot off at a 90 degrees from the original balls velocity resulting in a foul ball. Please don't make me resort to ascii art on this :)
Michael Dorgan
Angle of incidence = angle of reflection. The difference between the two is *not* 90 degrees unless the ball comes in at 45 degrees off from the surface normal of the bat.
LarsH
+1  A: 

Referring to MSDN Reflect Method, it looks like you want to reflect from the bat's velocity and normal.

Robb
I have tried that earlier. I have just replaced ball with bat in above codeand I dont know how n why, but its going exactly at 45 degrees in 2nd quadrant no matter which angle the bat is at even if it has to go thru the bat itself!!I have also tried changing the rotation of the ball, but its just not taking it into account.. What should I do!?
sid
Check out this SO question: http://stackoverflow.com/questions/2212042/xna-collision-detection-vector2-reflect-help-calculating-the-normal-of-a-circThat looks about what you are trying to tackle with a pretty clear explanation.
Robb