与精灵碰撞

时间:2013-12-17 19:20:49

标签: java android collision-detection game-engine

所以这就是正在发生的事情,游戏一直停止,因为两个圆圈相互碰撞,我试图通过确保ID是方形ID来防止这种情况,我试图让圆圈碰撞在一起然后忽略碰撞,但它不这样做,我不知道如何阻止它相互碰撞,只允许它与广场碰撞。

基本上我想阻止圆圈与另一个圆圈相撞并让它与正方形碰撞。

任何帮助都会非常感谢你

game.engine.engine class

  //collision detection class
   iter = p_group.listIterator(); 
        Sprite spr = null;
        while (iter.hasNext()) {
            spr = (Sprite)iter.next();

            //remove from list if flagged
            if (!spr.getAlive()) {
                iter.remove();
                continue;
            }

            //is collision enabled for this sprite?
            if (spr.getCollidable()) {

                //has this sprite collided with anything?
                if (spr.getCollided()) {

                    //is the target a valid object?
                    if (spr.getOffender() != null) {

                        /*
                         * External func call: notify game of collision
                         * (with validated offender)
                         */
                        collision(spr);

                        //reset offender
                        spr.setOffender(null);
                    }

                    //reset collided state
                    spr.setCollided(false);

                }
            }
        }

这是我在游戏类中检查碰撞的地方,它扩展了game.engine.engine包。

游戏类

          // all the circles were are set up the same, theres four
          // circletopleft, circletopright, circlebottomleft
          // circle bottom left collides with circle top right
          // theres no diffence between the code of the circles they are all the same
         /*ball*/circleBottomLeft.setCollidable(true);
                circleBottomLeft.setIdentifier(CIRCLEID_3);
                    addToGroup(circleBottomLeft);


                    //init ball sprite
                circleBottomRight.position = new Float2(550, h-550);
                circleBottomRight.setVelocity(new Float2(10.0f, -9.0f));

                    //keep ball inside secreen boundary
                Point size4 = circleBottomRight.getSize();
                circleBottomRight.addAnimation(new ReboundBehavior(new RectF
                    (0 , 0 , w-size4.x, h-size4.y),size4,circleBottomRight.getVelocity()) );

        /*ball*/circleBottomRight.setCollidable(true);
                circleBottomRight.setIdentifier(CIRCLEID_4);
                    addToGroup(circleBottomRight);


}


       //collision class extended from game.engine.Engine
    public void collision(Sprite sprite) {
    // TODO Auto-generated method stub
    final int oneMin = 60;
    final int twoMin = 120;
    final int threeMin = 180;
    final int fourMin = 240;
    final int fiveMin = 300;
    Sprite other = sprite.getOffender();
    Float2 vel = sprite.getVelocity();

    switch (sprite.getIdentifier()) {
    case CIRCLEID_1:    

     vel = sprite.getVelocity();
     other.position.y += 1;
    case CIRCLEID_2:

         vel = sprite.getVelocity();
         other.position.y += 1;
    case CIRCLEID_3:    
         vel = sprite.getVelocity();
         other.position.y += 1;
    case CIRCLEID_4:    
         vel = sprite.getVelocity();
         other.position.y += 1;

        break;
    }

    switch (other.getIdentifier()) {

    case oneMin:
        if (this.getElapsedTime() == oneMin) {

            vel.y = -1;
            sprite.setVelocity(vel);
            sprite.position.y += vel.y*2;

        }
        break;
    case twoMin:
        if (this.getElapsedTime() == twoMin) {
            vel.y = -1;
            sprite.setVelocity(vel);
            sprite.position.y += vel.y*2;

        }
        break;
    case threeMin:
        if (this.getElapsedTime() == threeMin) {
            vel.y = -1;
            sprite.setVelocity(vel);
            sprite.position.y += vel.y*2;

        }
        break;
    case fourMin:
        if (this.getElapsedTime() == fourMin) {
            vel.y = -1;
            sprite.setVelocity(vel);
            sprite.position.y += vel.y*2;

        }
        break;
    case fiveMin:
        if (this.getElapsedTime() == fiveMin) {
            vel.y = -1;
            sprite.setVelocity(vel);
            sprite.position.y += vel.y*2;

        }
        break;  

        case SQUARE_ID:
            //make sure its not touched
            if (other.getIdentifier() == SQUARE_ID &&
                other.getIdentifier() == CIRCLEID_1 ||
            other.getIdentifier() == CIRCLEID_2 || 
            other.getIdentifier() == CIRCLEID_3 ||
            other.getIdentifier() == CIRCLEID_4) {
                stop();
                Log.d("Game", "Collided, I collided with something");
                if (p_paused) {
                    Message msg = handler.obtainMessage();
                    msg.arg1 = 1;
                    handler.sendMessage(msg);

它在两个圆顶碰撞时保持停止(游戏中有417秒发生碰撞,并且它没有碰到方块。当圆圈底部左侧与圆圈右上方碰撞时,它恰好发生

1 个答案:

答案 0 :(得分:0)

if ( other.getCollided()  == true   && circleTopLeft.getCollided()     == true ||
            other.getCollided()  == true   && circleTopRight.getCollided()    == true || 
            other.getCollided()  == true   && circleBottomRight.getCollided() == true ||
            other.getCollided()  == true   && circleBottomLeft.getCollided()  == true ) {

通过调用我能够正确检查碰撞。