简体   繁体   English

Android Canvas。如何计算循环中心之间的差异?

[英]Android Canvas.How to calculate difference between centres in loop?

I new android. 我是新的android。 Trying to implement bouncing ball each other. 试图实现反弹球。 Like this 这样

My BouncingBallView class look like: 我的BouncingBallView类如下所示:

private ArrayList<Ball> balls;

public BouncingBallView(Context context) {
    super(context);
    this.balls = new ArrayList<>();


    for(int i=0; i<2; i++)
        this.balls.add(addBall());
}

public Ball addBall(){

    Ball ball;

    // Init the ball at a random location (inside the box) and moveAngle
    Random rand = new Random();
    int radius = 60;
    int x = rand.nextInt(500 - radius * 2 - 20) + radius + 10;
    int y = rand.nextInt(800- radius * 2 - 20) + radius + 10;
    int speed = 10;
    int angleInDegree = rand.nextInt(360);

    ball = new Ball(x, y, radius, speed, angleInDegree);

    return ball;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    for(int i=0; i < balls.size(); i++)
        balls.get(i).draw(canvas);

    for(int i=0; i < balls.size(); i++){

        balls.get(i).intersect(canvas);
        // Update the ball's state with proper collision response if collided.
        balls.get(i).update();
    }

    for(int i=0; i<balls.size(); i++){
        balls.get(i).collide(balls);

    }


    invalidate();
}

And class Ball has method collide(); 并且Ball类具有方法collide();

public void collide(ArrayList<Ball> balls){
    //Log.d("TEst", "LOG");

    // Calculate difference between centres
    float distX = balls.get(0).getBallX() - balls.get(1).getBallX();
    float distY = balls.get(0).getBallY() - balls.get(1).getBallY();

    // Get distance with Pythagora
    double dist = Math.sqrt((distX * distX) + (distY * distY));

    float r = ballRadius + ballRadius;
    if ((float) dist <= r) {
        Log.d("Collide", "Detected");
        this.ballSpeedX = -this.ballSpeedX;
        this.ballSpeedY = -this.ballSpeedY++;
    }
    /*for(int i=0; i < balls.size(); i++) {

        for(int j=1; j<balls.size(); j++) {
            // Calculate difference between centres
            float distX = balls.get(i).getBallX() - balls.get(j).getBallX();
            float distY = balls.get(i).getBallY() - balls.get(j).getBallY();

            // Get distance with Pythagora
            double dist = Math.sqrt((distX * distX) + (distY * distY));

    *//*double distance = Math.sqrt(((balls.get(0).getBallX() - balls.get(1).getBallX()) * (balls.get(0).getBallX() - balls.get(1).getBallX())) + ((balls.get(0).getBallY()
                - balls.get(1).getBallY()) * (balls.get(0).getBallY() - balls.get(1).getBallY())));*//*

            float r = ballRadius + ballRadius;
            if ((float) dist <= r) {
                 Log.d("Collide", "Detected");
                 this.ballSpeedX = -this.ballSpeedX;
                 this.ballSpeedY = -this.ballSpeedY++;
            }
        }
    }*/

}

Here, I'm using Pythagoras' theorem, a^2 + b^2 = c^2, to figure out the distance between the two circles' centers. 在这里,我使用毕达哥拉斯定理a ^ 2 + b ^ 2 = c ^ 2来计算两个圆心之间的距离。 How to calculate if ball count > 2. I try to in loop but it works very bad(frozen ball). 如果球数> 2,如何计算。我尝试循环播放,但效果非常差(冻结球)。

Full code you can find github 您可以找到完整的代码github

Now work like in this video my bouncing ball video 现在像这个视频一样工作我的弹跳球视频

Thx for help ;) 感谢帮助;)

Ps Sorry so poor English. 抱歉,英语太差了。

You can have a counter for balls in each stage, when a ball is destroyed(if you destroy them) you can change the count and you can check (the counter) in the collide method. 您可以在每个阶段都有一个用于球的计数器,当一个球被销毁时(如果您将其销毁),您可以更改计数并可以在collide方法中检查(计数器)。

But i don't thing that you need to check this condition, if a collision happens then OK else nothing should happen to this method. 但是我没关系,您不需要检查这种情况,如果发生碰撞,那么可以,否则该方法什么也不会发生。

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

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