Breakout Paddle 碰撞角 [英] Breakout Paddle Collision Angle

查看:28
本文介绍了Breakout Paddle 碰撞角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 Breakout 克隆版,但在球与桨的碰撞方面遇到了一些麻烦.我有一个矩形代表球和桨,当它们相交时,代表球速度的 Y 向量被否定(如下所示).这一切正常.问题是当桨向右移动时,我希望它向右轻推球(而不是它只是正常反射),我希望在相反的方向上发生同样的情况,桨正在移动到左边.我不知道如何去做这件事,我已经看过了.任何帮助,将不胜感激.谢谢.

I'm making a Breakout clone and having a little trouble with the ball-to-paddle collisions. I have a rectangle represent both the ball and the paddle and when they intersect, the Y vector representing the ball's velocity is negated (as shown below). That all works fine. The problem is when the paddle is moving to the right I want it to nudge the ball a little to the right (as opposed to it just reflecting off normally) and I want the same to happen in the opposite direction is the paddle is moving to the left. I'm not sure how to go about doing this and I've looked all over. Any help would be appreciated. Thanks.

if (paddleRectangle.Intersects(ballRectangle))
{
    ballVelocity.Y *= -1;
    collision.Play(); //a collision sound
}

基本上我想根据桨的移动方向稍微改变球从桨上弹回的角度.如果桨没有移动,那么球会正常反弹(通过反转球速度的 Y 分量)

Basically I want to slightly change the angle at which the ball bounces off the paddle based on which direction the paddle is moving. If the paddle is not moving, then the ball will bounce normally (by inverting the Y component of the ball's velocity)

推荐答案

将桨的速度矢量添加到桨的法线矢量(这基本上沿桨移动的方向弯曲法线)并标准化结果.将此用作反射的碰撞法线.

Add the paddle's velocity vector to the paddle's normal vector (this basically bends the normal in the direction the paddle is moving) and normalize the result. Use this as the collision normal for reflection.

Vector2 collisionNormal = Vector2.Normalize(paddleNormal + (paddleVelocity * desiredEffectAmount));
ballVelocity = Vector2.Reflect(ballVelocity, collisionNormal);

这篇关于Breakout Paddle 碰撞角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆