C ++ SFML乒乓球 - 如何计算球从球拍上反弹的角度? [英] C++ SFML Pong - How to calculate angle at which ball bounces off paddle?

查看:125
本文介绍了C ++ SFML乒乓球 - 如何计算球从球拍上反弹的角度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在下面的代码中,玩家的球拍与球的碰撞检测。我需要球根据击中球拍的位置以一定的角度反弹。因此,如果它接近中间,它将大致水平弹跳,如果它离中间更远,角度会增加。



我已经注释掉了什么似乎是公式,但我甚至无法计算sin(角度)部分,因为我无法弄清楚如何获得角度。



这是一个链接,显示解决方案: http://gamedev.stackexchange.com/questions/4253/how-do-you-calculate-where-a-ball-should-go-when-it-bounces-off-the-bar [ ^ ]



但是,在那个链接中,我不明白是什么crossY。如果我能搞清楚,那么我可以很容易地应用这个公式。任何人都可以查看链接并找出intersectY实际存储的内容吗?因为从他在链接中的解释,老实说我无法理解。我只需要知道intersectY的值是什么,以及如何获得该值。



So in the below code there's collision detection for a player's paddle with a ball. I need the ball to bounce off at a certain angle based on where it hits the paddle. So if it's close to the middle it will bounce roughly horizontally, if it's further away from the middle, the angle increases.

I have commented out what seems to be the formula but I'm having trouble even calculating the sin(angle) part because I can't figure out how to even get the angle.

Here's a link which shows the solution: http://gamedev.stackexchange.com/questions/4253/how-do-you-calculate-where-a-ball-should-go-when-it-bounces-off-the-bar[^]

BUT, in that link, I don't understand what intersectY is. If I can figure that out then I can easily apply the formula. Can anyone have a look at the link and figure out what intersectY actually stores? Because from his explanation in the link, I honestly can't understand. I just need to know what intersectY holds the value of, and how to obtain that value.

if (ball.getPosition().x < (player.getPosition().x) //PLAYER COLLISION DETECTION
		&& (ball.getPosition().y + (diameter)) >= player.getPosition().y
		&& ball.getPosition().y <= (player.getPosition().y + 100))
		{
			//xSpeed = xSpeed*cos(angle);
			//ySpeed = ySpeed*sin(angle);
			ySpeed = -ySpeed;
			xSpeed = -xSpeed;
			ball.move(xSpeed * Time.asMilliseconds(), ySpeed * Time.asMilliseconds());
		}

推荐答案

它看起来像是'Y'轴组件,其中球的碰撞发生在paddle



它可能是
it looks like intersectY is the 'Y' axis component of where the collision of the ball happens with the paddle

it could possibly be
ball.getPosition().y

在你的代码中(我不确定你是否知道)会使用球或球员的坐标)

in your code (Im not sure wether you would use the ball or player co-ordinates)


这篇关于C ++ SFML乒乓球 - 如何计算球从球拍上反弹的角度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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