Surfaceview中2D游戏中的碰撞检测

时间:2014-06-03 10:48:06

标签: java android

我希望水果和水果之间发生碰撞武器。武器将击中水果&水果将消失。我必须检测水果边界,每当武器边界发现水果边界果实应该消失。水果和水果。武器通过使用数组进行解析来填充。水果和武器类从具有一些共同字段的类Sprite扩展。还有另一个类,其中等式线。

DrawindPanel.java

类DrawingPannel扩展SurfaceView实现了SurfaceHolder.Callback {

PanelThread _thread;

public DrawingPannel(Context context, AttributeSet attrib) { 
    super(context, attrib); 
    getHolder().addCallback(this);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    Sprite clickedSprrite = isItemClicked(event.getX(), event.getY());
    GameUtil.lastTaped = new Point((int) event.getX(), (int) event.getY());

    ArrayList<Weapon> weapons = WeaponManager.weapons;

    for(int i = 0; i < weapons.size(); i++) {
        Weapon w = weapons.get(i);
        if(w.isSelected()) {
            w.setState(1);
        }

    }

返回super.onTouchEvent(event);     }

private Sprite isItemClicked(float x, float y) {
    //Checking if the tapped location has any item
    Sprite fruit;
    for(int i = 0 ; i < FruitManager.fruit.size(); i++) {
        fruit = FruitManager.fruit.get(i);
        float currentX = fruit.getCurrentX();
        float currentY = fruit.getCurrentY();
        float height = fruit.getCurrentBitmap().getHeight();
        float width = fruit.getCurrentBitmap().getWidth();

        if(x > currentX && x < currentX + width && y > currentY && y < currentY + height)
            return fruit;

    }
    return null;
}

public DrawingPannel(Context context) {
    super(context);
    getHolder().addCallback(this);
}

@Override 
public void onDraw(Canvas canvas) { 
    Log.i("DrawingPannel", "onDraw");
    canvas.drawBitmap(GamePlayActivity.background.getCurrentBitmap(),0, 0, new Paint());
    UIObjectManager.getInstance().onDraw(canvas);

}
@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) { 


}
@Override
public void surfaceCreated(SurfaceHolder holder) {


    setWillNotDraw(false); //Allows us to use invalidate() to call onDraw()


    _thread = new PanelThread(getHolder(), this); //Start the thread that
    _thread.setRunning(true);                     //will make calls to 
    _thread.start();                              //onDraw()
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    try {
        //_thread.setRunning(false);                //Tells thread to stop
        _thread.join();                           //Removes thread from mem.
    } catch (InterruptedException e) {}
}

}

1 个答案:

答案 0 :(得分:0)

最简单的碰撞检测代码如下:

private void checkCollisions()
{
    GameObject go1 = null;
    GameObject go2 = null;
    for (int i = 0; i < gameObjects.size(); i++)
    {
        go1 = gameObjects.get(i);
        for (int j = i+1; j < gameObjects.size(); j++)
        {
            go2 = gameObjects.get(j);
            if (go1 != go2)
            {
                if (go1.checkCollision(go2))
                {
                    go1.interact(go2);
                }
            }
        }
    }
}

如果边界圆的碰撞如下:

public boolean checkCollision(GameObject go)
{
    float distX = (go.getCalculatedX()-this.getCalculatedX());
    float distY = (go.getCalculatedY()-this.getCalculatedY());

    if((distX*distX + distY*distY)<= ((this.size+go.size)*(this.size+go.size)))
    {
        return true;
    }
    return false;
}

对象之间交互的最简单的非访问者模式解决方案类似于以下内容:

@Override
public void interact(GameObject go)
{
    if ((owner instanceof Player && go instanceof Enemy) || (owner instanceof Enemy && go instanceof Player))
    {
        Unit u = (Unit) go;
        u.sufferDamage(this.damage);
        this.detachable = true;
        explode();
    }
}

编辑:将发生此类型的collisionDetection的更新函数如下:

public void updateWorld(float delta)
{
    for(float t = 0; t < delta; t += EPSILON)
    {
        float t_end = t + EPSILON;
        if(t_end > delta)
        {
            t_end = delta;
        }
        float dt = t_end - t;

        for(int i = 0; i < gameObjects.size(); i++)
        {
            gameObjects.get(i).update(dt);
        }

        /*for(int i = 0; i < gameObjects.size(); i++)
        {
            for(int j = 0; j < gameObjects.size(); j++)
            {
                if(i != j)
                {
                    gameObjects.get(i).collidesWith(gameObjects.get(j));
                }
            }
        }*/

        checkCollisions();

        for(int i = 0; i < gameObjects.size(); i++)
        {
            if(gameObjects.get(i).isDetachable() == true)
            {
                gameObjects.remove(i--);
            }
        }

        timer -= dt;
        if(timer < 0)
        {
            onGameEnd();
            break;
        }
    }
}
编辑:你的情况是这样的:

private void checkCollisions()
{
    Weapon go1 = null;
    Fruit go2 = null;
    for (int i = 0; i < weapons.size(); i++)
    {
        go1 = weapons.get(i);
        for (int j = 0; j < fruits.size(); j++)
        {
            go2 = fruits.get(j);
            if (go1 != go2)
            {
                if (go1.checkCollision(go2))
                {
                    go1.interact(go2);
                }
            }
        }
    }
}

武器将具有与水果相互作用的功能。假设这里缺少共同的超类。