打砖块物理(射弹物理模拟)

发布于 2024-12-14 14:05:31 字数 560 浏览 3 评论 0原文

有一个滑块,我通过每个刻度之间的位置差异来计算其速度(由于通过触摸屏使用它,有时可能会很大,所以也许我应该将其限制在某个任意数字以避免其中一些问题?)

我 有一个具有 3 维方向向量和速​​度的球。

当球与滑块碰撞时,我反转其在 Z 轴上的方向(远离滑块),然后使用滑块速度来操纵其 X(左 <-> 右)方向。所以:

ball.direction.x += (slider_friction * slider_velocity)

现在球更新如下进行:

velocity = ball.velocity * time
ball.direction.normalise()
ball.position = ball.direction * ball.velocity

这似乎工作得很好,除了在某些情况下,它似乎很可能适用于任何球轴为零,导致在某些情况下它永远不会返回滑块。对此有什么好的解决方案吗?当块与块碰撞时,处理块响应的好方法是什么?它应该返回到以直角弹跳,还是应该使用滑块应用的相同弹跳修改器保持反射?此外,对于这种射弹模拟的任何其他物理技巧也将不胜感激。

I have a slider, whose velocity I calculate via the difference between positions each tick (this can sometimes be huge due to using it via a touch screen, so maybe I should clamp this at some arbitrary number to avoid some of these issues?)

I have a ball with a 3 dimensional direction vector and a velocity.

When the ball collides with the slider I invert its direction on the Z axis (going away from the slider) and then use the sliders velocity to manipulate its X (left <-> right) direction. so:

ball.direction.x += (slider_friction * slider_velocity)

Now the ball update is carried out as follows:

velocity = ball.velocity * time
ball.direction.normalise()
ball.position = ball.direction * ball.velocity

This seems to work great, except in some cases it seems to be very possible for any of the balls axis to equal zero, resulting in it never returning to the slider in some cases. What would be a good solution to this? And what would be a good way to handle the response with the blocks when it collides with them? Should it return to bouncing at right angles, or should it maintain reflecting with the same bounce modifiers applied by the slider? Also any other physics tips for this kind of projectile simulation would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咋地 2024-12-21 14:05:31

嗯,这是一个好问题;问题是,一个好的解决方案(即,看起来和感觉起来都像真实物理的解决方案)本质上是使用真实物理的解决方案。幸运的是,这个问题中的大部分牛顿物理学都可以很容易地简化。如果我说得过于冗长,请原谅我,但物理学往往会这样对你。

在此处输入图像描述

因此,为了定义问题,这是松紧带(如,没有吸收能量)打砖块球和桨之间的碰撞。首先,由于你显然已经降低了垂直运动,所以我不会关心这个。接下来的内容都是关于碰撞的水平分量。

球拍将一定量的水平动量传递给球(尽管,由于这是打砖块物理学,所以球拍本身不会损失动量:P)。这可以通过两种方式实现——使球旋转,以及给球一些水平动量(显然,如果球已经具有水平动量或旋转,则增加的动量将......好吧,添加)。

增量动量 + 增量角动量 = 动量桨给出

当然,使用动量可能会很烦人,因为您实际上不必这样做。我假设球和桨具有恒定的质量(也就是说,球不会突然变重,尽管您可以轻松地使用它),因为这样您就可以将每个球的质量从动量方程中分解出来。那么,

水平速度增量 + 角速度 = 桨速度 * 桨质量 / 球质量

要得出一个可以使用的方程,您必须设置桨的动量有多少会进入旋转,以及球的运动有多少。例如,

mass_factor = 2 # ratio between paddle and ball masses
angular_factor = 0.3 # the amount of the paddle's movement which will go into the ball's spin

# and now for the bouncy-bouncy

ball.hVel += (1 - angular_factor) * paddle.hVel * mass_factor * friction or whatever
ball.spin += angular_factor * paddle.hVel * mass_factor * friction or whatever
ball.vVel = - ball.vVel # of course, its vertical velocity reverses

这足以建立一个准现实的反弹,但仍然有一个金块(你不需要解决它,但解决它会让你的打砖块变得惊人)——所有的旋转会发生什么?旋转可以以某种方式用于更有趣的弹跳吗?

因此,回顾一下,旋转是球的外围相对于中心移动的速度。事实是,每当一个旋转的球撞到静止的物体时,它的旋转和速度都会发生变化。如果旋转的球击中静止表面,球会在与其旋转方向相反的方向上受到一点“踢”(如果在接触点测量旋转),并且旋转将会改变。

#Upon collision with a surface (assumed horizontal, with the ball above the surface)
ball.hVel += -(ball.Avel * rate) # Where "rate" is the ratio which determines how much angular velocity decays with each bounce
ball.Avel *= 1 - rate # So the angular velocity decays properly

由于角速度是旋转对称的,因此您只需将不同角度的碰撞(墙壁上的球、天花板上的球)视为其旋转。

唷,这无意中啰嗦了,而且还远未完成,但恕我直言,这足以回答你的问题了。

Hmm, this is a nice problem; the thing is, a good solution (that is, one that looks and feels just like real physics) is by nature one that uses real physics. Luckily, most of the newtonian physics within this problem can be easily simplified. Pardon me if I get overly verbose, but physics tends to do that to you.

enter image description here

So, to define the problem, this is an elastic (as in, no energy is absorbed) collision between an arkanoid ball and a paddle. First of all, since you've obviously got the vertical motion down, I won't concern myself with that. So what follows is all on the horizontal components of the collsions.

The paddle transfers a certain amount of horizontal momentum to the ball (although, since this is Arkanoid physics, the paddle loses no momentum itself :P). This can be recieved in two ways -- by making the ball spin, and by giving the ball some horizontal momentum (obviously, if the ball already has horizontal momentum or spin, the added momentum will be...well, added).

delta momentum + delta angular momentum = momentum paddle gave

Of course, it might be annoying to work with momenta, since you don't really have to. I would asssume that the ball and the paddle have constant mass (that is, the ball does not suddenly become heavier, although you could easily work with that), because then you could factor the mass of each out of your momentum equations. So then,

delta horizontal velocity + delta angular velocity = paddle velocity * mass of paddle / mass of ball

To get an equation you could use out of this, you have to set how much of the momentum from the paddle would go into the spin, and how much would go into ball movement. For example,

mass_factor = 2 # ratio between paddle and ball masses
angular_factor = 0.3 # the amount of the paddle's movement which will go into the ball's spin

# and now for the bouncy-bouncy

ball.hVel += (1 - angular_factor) * paddle.hVel * mass_factor * friction or whatever
ball.spin += angular_factor * paddle.hVel * mass_factor * friction or whatever
ball.vVel = - ball.vVel # of course, its vertical velocity reverses

This will be enough to set up a quasi-realistic bounce, but one nugget remains (which you don't need to adress, but adressing it would make your Arkanoid amazing) -- what happens to all that spin? Can the spin be used somehow for more interesting bounces?

So, to recap, your spin is the speed at which the periphery of the ball is moving relative to the center. Thing is, whenever a spinning ball bounces against something stationary, its spin changes as well as its velocity. If a spinning ball hits a stationary surface, the bal gets a little "kick" in the direction opposite that of its spin (if spin is measured at the point of contact), and the spin will change.

#Upon collision with a surface (assumed horizontal, with the ball above the surface)
ball.hVel += -(ball.Avel * rate) # Where "rate" is the ratio which determines how much angular velocity decays with each bounce
ball.Avel *= 1 - rate # So the angular velocity decays properly

Since angular velocity is rotationally symmetric, you would just treat collisions at different angles (balls on walls, balls on ceilings) as rotations of this.

Phew, that was unintentionally long-winded, and it is nowhere near complete, but it's enough to answer your question IMHO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文