2D球不正常碰撞 [英] 2d balls not colliding properly

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

问题描述

我只是想为codea的好看的物理游戏。

球碰撞看起来不错,但如果球在相互碰撞速度太慢,他们大棒。我不知道他们为什么这样做。

下面是我撞的功能​​:

 私人无效checkForCollision(ArrayList中<球>球){
    的for(int i = 0; I< balls.size();我++){
        球球= balls.get(我);
        如果(球=这与放大器;!&安培; ball.intersects(本)){
            this.collide(球,假);
        }
    }
}

公共布尔相交(球B){
    双DX = Math.abs(b.posX  -  POSX);
    双DY = Math.abs(b.posY  - 波西);
    双D =的Math.sqrt(DX * DX + DY * DY);
    返回D< =(半径+ b.radius);
}

私人无效碰撞(球球,布尔B){
    双M1 = this.radius;
    双M2 = ball.radius;
    双V1 = this.motionX;
    双V2 = ball.motionX;

    双VX =(M1  - 平方米)* V1 /(M1 + M2)+ 2 *平方米* V2 /(M1 + M2);

    V1 = this.motionY;
    V2 = ball.motionY;
    双VY =(M1  - 平方米)* V1 /(M1 + M2)+ 2 *平方米* V2 /(M1 + M2);

    如果(!B)
        ball.collide(这一点,真正的);
    的System.out.println(VX ++ VY);
    的MotionX = VX * BOUNCEOBJECT;
    motionY = VY * BOUNCEOBJECT;
}
 

但是,这是他们用低速碰撞会发生什么:

所以,你有一个想法?

编辑:

的更新的参宿一的作品很不错......但有一个问题依然存在......如果我添加引力是这样的:

 公共无效物理(){
    motionY + =重力; //< =这部分(重力设置为0.3D)
    checkForCollision(screen.balls);
    keyMove();
    bounceWalls();

    POSX + =的MotionX;
    波西+ = motionY;
}
 

他们仍然进入对方。我认为这是错误的方式来增加重力,或者是不是?

和我想我做了一件错误的碰撞式的,因为他们不属于正确的:

,然后他们慢慢地沉入地下。

编辑: 发现一个惊人的教程:的http://www.ntu.edu.sg/home/ehchua/programming/java/J8a_GameIntro-BouncingBalls.html

解决方案

 (M1  -  M2)* V1 /(M1 + M2)+ 2 * 2 * V2 /(M1 + M2 );
 

这有一个整数值2,请使其2.0f或2.0D然后检查出来。它必须是小速度的问题。 Becuse整型常量autocasts乘以双打。

如果这不起作用,然后点击参宿一的回答将是有益的。

如果你需要真正好的物理,你应该使用,然后将其转换为速度,然后将其转换为移动。看像龙格库塔欧拉积分

集成技术

 力 - >加速 - >速度 - >位移
 

如果发生碰撞时,只需更新的动力则剩余部分将流入。

----> <一个href="http://$c$cflow.org/entries/2010/aug/28/integration-by-example-euler-vs-verlet-vs-runge-kutta/" rel="nofollow">http://$c$cflow.org/entries/2010/aug/28/integration-by-example-euler-vs-verlet-vs-runge-kutta/ &LT; -----

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

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

HTTP://cwx.$p$pnhall.com/bookbind/pubbooks / walker2 /

verlet的整合龙格 - 库塔-4 欧拉积分 preferably的分子动力学(一个很好的例子之间的点弹跳球,如果你ommit的电场和债券)

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.

Here's my collision function:

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:

So do you have an idea?

EDIT:

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.

EDIT: 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);

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.

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

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://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 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)

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

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