简体   繁体   English

Java-突破物理,桨碰撞时球的方向如何?

[英]Java - Breakout physics, ball direction on paddle collision?

I'm creating a Breakout game to familiarize myself with Java. 我正在创建一个Breakout游戏以熟悉Java。 Currently I've got everything working but I've noticed that the ball seems to take off in the same direction. 目前,我可以进行所有工作,但是我注意到球似乎朝着同一方向起飞。

I've programmed it so when the ball hits the paddle the Y velocity is reversed. 我已经对它进行了编程,因此当球击中桨叶时,Y速度会反转。 This works, but the ball seems to follow the same route... 这可行,但是球似乎遵循相同的路线...

How would a make the collision seem more realistic? 如何使碰撞看起来更逼真?

When the ball hits the left side of the paddle, I want to redirect it back to the left but I'm not sure at which angle to reflect it. 当球碰到桨的左侧时,我想将其重定向回左侧,但是我不确定以哪个角度反射它。 Can anyone provide a formula to help me work out the angle at which I need to direct the ball upon contact with either the left or right side of the paddle? 谁能提供一个公式来帮助我确定与球拍的左侧或右侧接触时我需要引导球的角度?

If the ball hits the paddle from the top, negate Y velocity. 如果球从顶部撞击桨叶,则抵消Y速度。

If the ball hits the paddle from the side, negate X velocity. 如果球从侧面击中桨,则抵消X速度。

Reversing the vertical velocity is the correct thing to do for a perfectly elastic collision , which is what you want in a breakout game (the ball keeps it's total velocity after the collision). 反转垂直速度是完全弹性碰撞的正确方法,这是您在突破游戏中想要的(球在碰撞后保持其总速度)。

The problem of following the same route can be avoided either by randomizing the initial ball position and velocity, or by changing the collision dynamics to be unrealistic . 通过随机化初始球的位置和速度,或者通过将碰撞动力学改变为不现实 ,可以避免遵循相同路线的问题。 You could either add a small random component to both coordinates (taking care to keep the total velocity the same), or send the ball at a different angle, depending on where it hits the pad (which is what many games of this type do). 您可以在两个坐标上添加一个小的随机分量(注意保持总速度相同),也可以将球以不同的角度发送,具体取决于它击中击球垫的位置(许多此类游戏都这样做) 。

To do the later, you need to calculate the absolute position where the ball hits the pad, normalize it (by subtracting the pad position and dividing by the pad length), and map it into a angle range (maybe -80 to 80 degrees). 要进行后续操作,您需要计算出球击中击球垫的绝对位置,对其进行归一化(通过减去击球垫位置并除以击球垫长度),然后将其映射到一个角度范围(可能为-80到80度) 。 You can then calculate the desired horizontal and vertical velocities using trigonometric functions. 然后,您可以使用三角函数计算所需的水平和垂直速度。

One idea might be to use motion of the paddle to vary the direction a bit. 一种想法可能是使用桨的运动来稍微改变方向。 Say, if the paddle is moving to the right when hitting the ball, change the X velocity of the ball to the right a little in addition to reversing the Y direction. 举例来说,如果击球时桨叶向右移动,除了反转Y方向外,还需要稍微向右移动球的X速度。 That provides a way for the user to actually use some skill to put the right "English" on the ball. 这为用户提供了一种实际使用某种技巧将正确的“英语”施加到球上的方式。

There are two separate problems involved, a design problem and the physics problem. 涉及两个独立的问题,一个设计问题和一个物理问题。 In the end, the physics problem might have the easier solution: 最后,物理问题可能有更简单的解决方案:

  1. Compute the collision point on the paddle 计算桨上的碰撞点
  2. Compute the instantaneous horizontal velocity pvx of the paddle 计算桨的瞬时水平速度pvx
  3. Compute the normed outward surface normal (nx,ny) at the impact point. 计算冲击点处的标准外表面法线(nx,ny)。 This is the design decision, in the middle part of the paddle one probably wants (nx,ny)=(0,1), while on the left side (nx,ny)=(-1,0) or generally in WNW direction, on the right side correspondingly (nx,ny)=(1,0) or in the general ENE position. 这是设计决定,在桨的中间可能要(nx,ny)=(0,1),而在左侧(nx,ny)=(-1,0)或通常沿WNW方向,在右侧对应(nx,ny)=(1,0)或在常规ENE位置。
  4. Use the physics formula for a collision with a moving infinitely massive object to compute the new velocity vector (vx,vy): 使用物理公式与移动的无限大物体碰撞以计算新的速度矢量(vx,vy):

     dot = nx*(vx-pvx)+ny*vy vx = vx - 2*dot*nx vy = vy - 2*dot*ny 

    One may check if the dot product dot is positive to avoid spurious collision computations if paddle and ball do not separate fast enough after collision. 如果在碰撞后桨和球分开得不够快,可以检查点积dot是否为正,以避免虚假的碰撞计算。

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

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