简体   繁体   English

如何计算球的下一方向

[英]How to calculate ball next direction

I don't know how to calculate the next direction of a ball when it touches to wall. 我不知道当球碰到墙壁时如何计算下一个方向。

Example the ball is moving X +5 and Y +3 but after when it touches to wall, the direction should change like on real life. 例如,球在移动X +5和Y +3,但是当它碰到墙壁后,方向应该像现实生活中那样改变。 (Like a billiard) (像台球一样)

http://i.stack.imgur.com/Y5AOZ.png http://i.stack.imgur.com/Y5AOZ.png

The simplest simulation mechanic is: if you hit a side wall, multiply x velocity by -1 . 最简单的模拟机制是:如果碰到侧壁,则将x速度乘以-1 If you hit a top or bottom wall, multiply y velocity by -1 . 如果碰到顶壁或底壁,请将y速度乘以-1

Assuming your rails are a rectangle comprised of lines that are parallel to the x-axis or y-axis of your coordinate system (ie straight lines), then this will satisfy the angle of incidence = angle of reflection constraint. 假设您的轨道是由平行于坐标系的x轴或y轴的线(即直线)组成的矩形,则将满足入射角=反射角约束。 If not, then you have to do some math involving the angles. 如果不是,则必须进行一些涉及角度的数学运算。

You may also want to consider the impulse of the walls and the friction between the ball and the surface of your table to calculate how much slower the ball moves after a collision and over time. 您可能还需要考虑墙壁的推动力以及球与桌子表面之间的摩擦力 ,以计算球在碰撞后随时间推移运动的速度变慢了多少。 Otherwise your balls won't stop moving. 否则,您的球将不会停止移动。 Both of these would (most likely) be a simple constant that affects both the x and y components of your velocity in the same way (ie multiply both by the same constant, eg 0.99 for friction, etc). 这两个(很可能)都是一个简单的常数,它以相同的方式影响速度的x和y分量(即,将它们乘以相同的常数,例如0.99,以表示摩擦,等等)。 This should give you a reasonably accurate simulation, with some tuning. 这应该给您一个合理准确的模拟,并进行一些调整。

Real life physics has the angle of incidence equalling the angle of reflection. 现实生活中物理学的入射角等于反射角。 In other words, if the ball comes in at a 30 degree angle, it is bounced out at a 30 degree angle. 换句话说,如果球以30度角进入,则以30度角弹出。

You need two points (on the ball's path) to calculate the angle that it hits the wall. 您需要两个点(在球的路径上)来计算它撞击墙壁的角度。 Use that same angle as the "reflection angle". 使用与“反射角”相同的角度。 Make sure you adjust as needed to use the same frame of reference for everything. 确保根据需要进行调整以对所有内容使用相同的参考系。

The routine that calculates the next position of the ball is responsible for keeping it on this track. 计算球的下一个位置的例程负责将其保持在该轨迹上。

Basic algebra and trig gives you the equations that you solve to get the values you need. 基本代数和三角函数为您提供了求解所需的方程式。 For example, the slope (angle) of a line is given by y = mx + c. 例如,直线的斜率(角度)由y = mx + c给出。 (y,x are the coords, m is the slope, c is a constant). (y,x是坐标,m是斜率,c是常数)。 This is pretty straightforward - it's just basic algebra. 这非常简单-只是基本的代数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM