根据速度改变碰撞时球的方向
我正在创建一个乒乓球游戏。我想创造一种根据对球拍的影响来控制球方向的能力。如果球在 vy = 4 处落下; vx=4;
并且桨在 vx = -5 处向左移动;我希望球根据桨移动的速度稍微改变其路线。它可能会降低碰撞时球的 vx 速度,从而使球在向上移动时移动得更直(靠近 Y 轴)。但在我开始疯狂的试错之旅之前,我想知道是否有人知道答案或可能有任何消息来源。
我认为这样做的解决方案可能是测量桨的速度。我的问题是桨是由鼠标控制的并且没有一定的速度。我想弄清楚如何测量鼠标在 x 轴上移动的速度。
我可能会创建一个每隔几秒触发一次的计时器,以确定鼠标在哪里以及它在哪里。计算出差异,这将是速度
如果有人有任何答案,那就太好了。谢谢
I am creating a ping pong game. And I want to create the ability to control the direction of the ball based on the impact on the paddle. If the ball is coming down at vy = 4; vx = 4;
and the paddle is moving to the left at vx = -5; I want the ball to slightly change its course depending on how fast the paddle is moving. It would probably reduce the balls vx speed on collision, therefore making the ball move more straight (close to the Y axis) when it is moving back up. But before I go on a crazy trial and error journey, I was wanting to know if anyone knew the answer or probably have any sources.
I figure the solution for probably doing this would be to measure how fast the paddle is going. My problem is the paddle is controlled by the mouse and has no certain speed. I am trying to figure out how I can measure the speed of my mouse traveling on the x axis.
I am probably going to create a timer that fires every few seconds to determine where the mouse was and where it is at. figure the difference and that will be the speed
If anyone has any answers, that would be great. thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
测量桨速度的最简单方法是保留前一帧中桨位置的缓存,因此通过 x - xPrev,您可以获得该帧中桨运动的增量,或相对速度。
从该速度,您可以将其作为修改器(可能按比例缩小)添加到球反射矢量的 x 速度中。
现在这听起来像是一个简单的游戏,帧率应该不是问题。然而,作为参考,如果您想跟踪速度,可以通过访问前一帧的总时间来进行简单的物理计算。允许在时间上保持一致的行为,与模拟的帧速率无关。
The easiest way to measure the speed of the paddle is to keep a cache of the position of the paddle in the previous frame, thus by having x - xPrev you have the delta of the paddle movement for this frame, or the relative speed.
From that speed you can add it as a modifier(scaled down probably), into the x velocity of the ball's reflected vector.
Now this sounds like a simple game, and framerate should not be a problem. For reference however, if you want to keep track of the velocity, simple Physics calculations could be made by having access to the total time of the previous frame. allowing for consistent behavior time-wise, independent of the framerate of the simulation.
K,我想出了解决办法。不过,仍然需要进行碰撞检测。不是最伟大的。我的球有时会卡在桨中。我正在使用 Flash 对象命中测试。但无论如何。这是我用来让它达到我想要的效果的代码。似乎它有时支持它应该走的方向,有时又不支持。
它有效,但问题是我把速度调为负,所以有时方向会偏离。因为球移动的方向是由 -1 或 1 决定的。如果我将速度调为负值。它可能导致一个数字与它的预期相反。
下面是在桨类中,
我检查球类中的碰撞情况。因为它是击中一切的那个。 代码
下面是我的球类中的 。在检查墙壁类中,它检查是否撞到墙壁或桨。我将我的球拍的参考传递给了球类。
我仍然需要经常眨眼。但如果我努力的话。我想我可以让它做我想做的事。
K, I figure the solution out. Still need to work on my collision detection though. isnt the greatest. my ball sometimes get stuck in the paddle. I am using the flash object hit test. but anyhow. this is the code I used to get it to somewhat do I want it to do. It seems as though it supports the direction it suppose to go sometimes and sometimes it doesnt.
It works but the problem is I knock my speed in the negative so sometimes the directions are off. since the direction the ball moves is determined by a -1 or a 1. if I knock my speed in the negative. it can cause a number to be the opposite of what its suppose to be.
Below is in the paddle class
I check for collision within my ball class. since it is the one hitting everything. below is the code
the following is in my ball class. in the check walls class it checks to see if it hits walls or the paddle. I passed reference of my paddle to the ball class.
I still needs to be twinked ALOT. but if I work on it. i think i can get it to do what i want.
我能够解决这个问题。我在互联网上找到了执行以下操作的方法,并且物理看起来更好一些。球仍然时不时地卡在球拍内
将以下内容更改
为
I was able to some what resolve the issue. I found on the internet to do the following and the physics look some what better. ball still gets stuck within the paddle from time to time
Changed the following from
to