二维球没有正确碰撞 [英] 2d balls not colliding properly

查看:34
本文介绍了二维球没有正确碰撞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想编写一个好看的物理游戏.

I'm just trying to code a nice looking physics game.

球碰撞看起来不错,但如果球碰撞得太慢,它们就会粘"在一起.我不知道他们为什么这样做.

The ball collision looks nice but if the balls are colliding too slow, they "stick" in each other. I have no clue why they do.

这是我的碰撞函数:

private void checkForCollision(ArrayList<Ball> balls) {
    for (int i = 0; i < balls.size(); i++) {
        Ball ball = balls.get(i);
        if (ball != this && ball.intersects(this)) {
            this.collide(ball, false);
        }
    }
}

public boolean intersects(Ball b) {
    double dx = Math.abs(b.posX - posX);
    double dy = Math.abs(b.posY - posY);
    double d = Math.sqrt(dx * dx + dy * dy);
    return d <= (radius + b.radius);
}

private void collide(Ball ball, boolean b) {
    double m1 = this.radius;
    double m2 = ball.radius;
    double v1 = this.motionX;
    double v2 = ball.motionX;

    double vx = (m1 - m2) * v1 / (m1 + m2) + 2 * m2 * v2 / (m1 + m2);

    v1 = this.motionY;
    v2 = ball.motionY;
    double vy = (m1 - m2) * v1 / (m1 + m2) + 2 * m2 * v2 / (m1 + m2);

    if (!b)
        ball.collide(this, true);
    System.out.println(vx + " " + vy);
    motionX = vx * BOUNCEOBJECT;
    motionY = vy * BOUNCEOBJECT;
}

但这就是当它们以低速碰撞时会发生的情况:

But this is what happens when they collide with a low speed:

所以你有什么想法吗?

Alnitak 的更新效果很好...但仍然存在一个问题...如果我像这样添加重力:

The update of Alnitak works very nice... but one problem is still there... if i add gravity like this:

public void physic() {
    motionY += GRAVITY;                  // <= this part (GRAVITY is set to 0.3D)
    checkForCollision(screen.balls);
    keyMove();
    bounceWalls();

    posX += motionX;
    posY += motionY;
}

它们仍然相互移动.我认为这是增加重力的错误方式,不是吗?

They still move into each other. I think this is the wrong way to add gravity, or isn't it?

而且我认为我在碰撞公式上做错了,因为它们不正确:

And I think I did something wrong with the collision formula, because they don't fall right:

!

然后它们慢慢地沉入地下.

and then they slowly sink into the ground.

找到了一个惊人的教程:http://www.ntu.edu.sg/home/ehchua/programming/java/J8a_GameIntro-BouncingBalls.html

found an AMAZING tutorial: http://www.ntu.edu.sg/home/ehchua/programming/java/J8a_GameIntro-BouncingBalls.html

推荐答案

(m1 - m2) * v1 / (m1 + m2) + 2 * m2 * v2 / (m1 + m2);

这是一个整数值 2.请将其设为 2.0f 或 2.0d,然后查看.这一定是小速度的问题.因为整数常量自动转换成双精度.

This has an integer value 2. Please make it 2.0f or 2.0d then check it out. It must be the problem for small speeds. Becuse integer constant autocasts multiplied doubles.

如果这不起作用,那么 Alnitak 的回答会有所帮助.

If this does not work, then Alnitak 's answer would be helpful.

如果你需要真正好的物理,你应该使用 force 然后将它转换为 velocity 然后将它转换为 displacement .查看诸如Runge KuttaEuler Integration

If you need real nice physics, you should use the force then convert it to velocity then convert it to displacement . Look at integrator techniques like Runge Kutta and Euler Integration

Force-->acceleration-->velocity-->displacement

如果发生碰撞,只需更新力,然后其余部分将流动.

if collision occurs, just update the force then the rest will be flowing.

----> http://codeflow.org/entries/2010/aug/28/integration-by-example-euler-vs-verlet-vs-runge-kutta/ <-----

----> http://codeflow.org/entries/2010/aug/28/integration-by-example-euler-vs-verlet-vs-runge-kutta/ <-----

http://www.forums.evilmana.com/game-programming-theory/euler-vs-verlet-vs-rk4-physics/

http://www.newagepublishers.com/samplechapter/001579.pdf

http://cwx.prenhall.com/bookbind/pubbooks/walker2/

Verlet 积分Runge-Kutta-4Euler 积分 之间的一个点,最好用于分子动力学(弹跳球的一个很好的例子,如果你省略了电场和键)

Verlet integration is a point between Runge-Kutta-4 and Euler Integration preferably for molecular dynamics (a good example for bouncing balls if you ommit the electrical fields and bonds)

这篇关于二维球没有正确碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆