【问题标题】:Android: Is there a way to program "collision detection" between two instances of an object?Android:有没有办法在一个对象的两个实例之间编程“碰撞检测”?
【发布时间】:2014-09-29 22:36:09
【问题描述】:

关于该主题还有其他问题,但这个问题有所不同。

基本上,我有一个名为“Player”的类,我将其乘以一定次数。此类在画布中生成随机坐标,位图向这些坐标移动。现在的问题是它们中的一些相互重叠,使其看起来“不切实际”。

我在“Player”类中尝试了一个简单的“if”语句,但它不起作用,因为该类的每个实例只计算其变量而忽略其他实例的变量。

代码如下:

我有这个第一类和另一个嵌套的类:

  public class MainActivity extends Activity{

    Game gameView;
    float k,l;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        gameView = new Game(this);
        setContentView(gameView);        
    }

    public class Game extends SurfaceView implements Runnable {

        Canvas canvas;
        SurfaceHolder ourHolder;
        Thread ourThread = null;
        boolean isRunning = true;
        Player[] player = new Player[3];

        public Game(Context context) {
            super(context);
            ourHolder = getHolder();
            ourThread = new Thread(this);
            ourThread.start();
        for(int i=0; player.length < i; i++){
            player[i] = new Player(context);
        }
        }

        public void run() {
            while(isRunning) {
                    if(!ourHolder.getSurface().isValid())
                        continue;
                        canvas = ourHolder.lockCanvas();                
                        canvas.drawRGB(200, 200, 200);

                    for ( int i = 0; player.length < i; i++){
                        player[i].draw(canvas);
                        player[i].move();
                    }

                    ourHolder.unlockCanvasAndPost(canvas);
                }
            }       
        }        
}

这是播放器类:

public class Player {

    Bitmap base;
    float x = (float) (Math.random()*200);
    float y = (float) (Math.random()*200);
    float e = (float) (Math.random()*200);
    float r = (float) (Math.random()*200);

    public Player(Context context) {
        super();
        base = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
    }

    public void draw(Canvas canvas) {

        if(x > e-5 && x < e+5 && y > r-5 && y < r+5){
            e = (float) (Math.random()*canvas.getWidth());
            r = (float) (Math.random()*canvas.getHeight());
        }

        canvas.drawBitmap(base, x - base.getWidth()/2, y - base.getHeight()/2, null);
    }

public void move () {
        //Here's just the code that makes the bitmap move.
    }
}

您可能已经看到,变量“e”和“r”在位图的坐标(x 和 y)每次靠近它们时创建随机值,然后“x”和“y”变量增加或减少它们的值以匹配“e”和“r”坐标。

现在我想要的是变量“x”和“y”与其他实例的变量“x”和“y”交互,这样它们就不会重叠。有没有办法做到这一点?

非常感谢。

【问题讨论】:

  • Google Fu + "边界框" -> ??? -> 利润。

标签: java android bitmap r.java-file


【解决方案1】:

在您的 Player 类中,将 X 和 Y 公开(不推荐)或为它们创建访问器:

public void setX(float x) {
    this.x = x;
}

public int getX() {
    return x;
}

现在,在你的 run() 方法中,你可以做这样的事情(借用你已有的代码):

for ( int i = 0; player.length < i; i++){
    player[i].draw(canvas);
    player[i].move();
}
...
for (int i = 0; i < player.length - 1; i++) {
    if (player[i].getX() > player[i + 1].getX() - 5 && 
        player[i].getX() < player[i + 1].getX() + 5 && 
        player[i].getY() > player[i + 1].getY() - 5 && 
        player[i].getY() < player[i + 1].getY() + 5) {

    // Do your update here!
    // You may need to create other methods...or you can just
    // create random X & Y for the player.
}

这是一个非常简单的方法。请记住,如果您有很多玩家,您可以将一个玩家移动到另一个玩家身上,因此您可能需要在移动玩家后再次测试以确保它是清晰的。

【讨论】:

  • 非常感谢您的回答!我没有想到这一点。我试试看。
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 2013-03-19
  • 1970-01-01
  • 2015-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-20
相关资源
最近更新 更多