2D Gaming - How to reflect a ball off the bat?

♀尐吖头ヾ 提交于 2019-12-24 03:12:33

问题


I am pretty new to XNA & game dev and 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...


回答1:


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




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/2707059/2d-gaming-how-to-reflect-a-ball-off-the-bat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!