Verlet 积分弹跳

发布于 2024-09-30 01:33:52 字数 424 浏览 7 评论 0原文

我正在尝试学习 Verlet 集成,主要是因为我很无聊,并且想为我正常的“弹跳球”学习练习增添趣味。

我有一个简单的弹跳球 Canvas/HTML5 页面 http://sandbox.electricgrey.com:8080/physicals /。如果您单击它,您会发现球并不总是弹回到相同的高度。有时它较高,有时较低。它为什么要这么做?

是因为我把它简化得太过了吗?我是否真的需要计算部分时间步长,以便准确了解球碰撞的时间/地点,然后从那里继续?

PS:如果您对我的 HTML 或 Javascript 有任何意见,我很乐意听到。我正在学习如何使用 Javascript 进行编码,并且我想确保我正在以正确的方式进行操作。

I am trying to learn Verlet integration, mainly because I'm bored, and want to spice up my normal "bouncing ball" learning exercise.

I have a simple bouncing ball Canvas/HTML5 page at http://sandbox.electricgrey.com:8080/physics/. If you click on it you'll notice that the ball doesn't always bounce back to the same height. Sometimes it is higher, sometimes it is lower. Why is it doing that?

Is it because I am simplifying it too much? Do I really need to calculate partial time steps so I get exactly when/where the ball collides and then continue from there?

PS: If you have any comments about my HTML or Javascript, I would love to hear them. I'm just learning how to code with Javascript, and I want to make sure I am doing it The Right Way™.

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

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

发布评论

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

评论(2

吃不饱 2024-10-07 01:33:52

在 Verlet 算法中,最重要的是计算下一步所采取的时间步长。使用 Verlet 算法时,您使用的是:基本算法还是速度算法?对于与地板的碰撞,你有什么想法?我可以看到碰撞点每次都不在同一高度。在这种情况下,您必须计算碰撞时间 (t1),用 t1 进行移动,进行碰撞,然后用时间 (t_step - t1) 进行移动。我在此模型的第一次实现中使用了此方法

In Verlet algorithm most important is time step taken to calculate next step. With of Verlet Algorithm you use: Basic or Velocity? What is your idea for collision with floor? I can see that collision point in not on the same height at every time. In this case, you have to calculate time to collision (t1) make move with t1, make collision and then make move with time (t_step - t1). I use this method in first implementation of this model

四叶草在未来唯美盛开 2024-10-07 01:33:52

正如阿萨尔所说,你可能需要改变步长。尝试如下操作,看看是否可以通过改变 t 来解决问题。

    function Vertex(x,y,vx,vy,t) {
        this.t = t
        this.x = x;
        this.y = y;
        this.px = x-vx*t;
        this.py = y-vy*t;

        .......
        this.tick = function() {
            ....
            var tx = this.x;
            var ty = this.y;
            this.x = this.x + (this.x - this.px) + this.ax * this.t;
            this.y = this.y + (this.y - this.py) + this.ay * this.t;
            ....
          }  
  ....
}  

您还可以通过更改以下方法来获得一定的准确性,该方法会在球撞击地板时反转球的速度。

if (this.y < this.r) {
    this.y = 2 * this.r - this.y;
    this.py = 2 * this.r - this.py;
}

如果球在时间步的中间弹起,则它在时间步的第一部分沿重力方向行进,但在时间步的最后部分远离重力方向行进。这两种贡献应该部分相互抵消。但在您的代码中,您假设重力在整个时间步长中将球向前拉。您可以通过更改上述方法中的公式以包含包含“this.ax * this.t”的术语来解决此错误。 (但我不完全确定这个术语应该是什么样子)。

调试此类程序的最佳方法是绘制一个图表,其中 x 轴为时间,y 轴为总机械能。然后,您可以轻松确定是否在弹跳期间或正常时间步长期间失去能量。

As Asar said you probably need to change the step size. Try something like the following and see if you can solve the problem by varying t.

    function Vertex(x,y,vx,vy,t) {
        this.t = t
        this.x = x;
        this.y = y;
        this.px = x-vx*t;
        this.py = y-vy*t;

        .......
        this.tick = function() {
            ....
            var tx = this.x;
            var ty = this.y;
            this.x = this.x + (this.x - this.px) + this.ax * this.t;
            this.y = this.y + (this.y - this.py) + this.ay * this.t;
            ....
          }  
  ....
}  

You could also gain some accuracy by changing the following method that, reverses the velocity of the ball, when it hits the floor.

if (this.y < this.r) {
    this.y = 2 * this.r - this.y;
    this.py = 2 * this.r - this.py;
}

If the ball bounces in the middle of a time step, then it travels in the direction of the gravitational force during the first part of the time step, but it travels away from the gravitational force during the last part of the time step. These two contributions should partly cancel each other. But in your code you assume that the gravitational force pulls the ball forward during the entire time step. You can account for this error by changing the formulas in the above method to include a term containing "this.ax * this.t". (But I am not fully sure how this term should look).

The best way to debug a program like this is to draw a graph where you have the time on the x-axis and the total mechanical energy on the y-axis. Then you can easily determine if you lose the energy during the bounces or during the normal time steps.

http://sandbox.electricgrey.com:8080/physics/physics.js

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