【问题标题】:Android Libgdx and collision detectionAndroid Libgdx 和碰撞检测
【发布时间】:2012-11-12 07:19:10
【问题描述】:

对于我的 android 游戏,我使用了 Libgdx,我检测到 Bob (Omino) 和 Plant (Pianta) 之间的碰撞,这段代码运行良好: Assets.class

pianta = new Animation(0.5f,new TextureRegion(items, 160, 384, 64, 96),
                              new TextureRegion(items, 224, 384, 64, 96));

Pianta.class

public class Pianta extends GameObject {
   public static final float PIANTA_WIDTH = 2;
   public static final float PIANTA_HEIGHT = 3;
   public static float stateTime;
   public Pianta(float x, float y) {
      super(x, y, PIANTA_WIDTH, PIANTA_HEIGHT);   
      stateTime = 0;
   }

    public void update(float deltaTime) {
           stateTime += deltaTime;
       }

}

World.class

Pianta pianta1_0 = new Pianta(x+10,2.2f);
piante.add(pianta1_0);

private void collisionPiante(){
      int len = piante.size();
      for(int i=0;i<len;i++){
         if(OverlapTester.overlapRectangles(piante.get(i).bounds,omino.bounds)){
            omino.ominoMorto();
         }
      }
   }

WorldRender.class

private void renderPiante() {
      TextureRegion keyFrame;
      int len = world.piante.size();
        for(int i = 0; i < len; i++) {
            Pianta pianta = world.piante.get(i);            
            keyFrame = Assets.pianta.getKeyFrame(Pianta.stateTime, Animation.ANIMATION_LOOPING);
            batcher.draw(keyFrame,pianta.position.x, pianta.position.y, 2, 3);
        }


   }

但是如果你看下面的图片 2,你可以看到 Bob 击中但没有与石头碰撞(Pietra)! 这是代码:

Assets.class

pietra1 = new TextureRegion(items,288,416,128,64);

Pietra.class

public class Pietra extends GameObject {
   public static float PIETRA_WIDTH = 4;
   public static float PIETRA_HEIGHT = 2;
   public Pietra(float x, float y) {
      super(x, y, PIETRA_WIDTH, PIETRA_HEIGHT);
   }

}

World.class

Pietra pietra1_0 = new Pietra(x+25,2.2f);
pietre.add(pietra1_0);
private void collisionPietre(){
      int len2 = pietre.size();
      for(int l=0;l<len2;l++){
         if(OverlapTester.overlapRectangles(pietre.get(l).bounds,omino.bounds)){
            omino.ominoMorto();
         }
      }   
   }

WorldRender.class

private void renderPietre() {
      int len = world.pietre.size();
        for(int i = 0; i < len; i++) {
            Pietra pietra = world.pietre.get(i);            
           batcher.draw(Assets.pietra1,pietra.position.x, pietra.position.y, 4, 2);
        }
   }

重叠测试器

public class OverlapTester {
    public static boolean overlapRectangles (Rectangle r1, Rectangle r2) {
        if (r1.x < r2.x + r2.width && r1.x + r1.width > r2.x && r1.y < r2.y + r2.height && r1.y + r1.height > r2.y)
            return true;
        else
            return false;
    }

有人可以告诉我为什么即使没有碰撞,与植物的碰撞也能正常工作,而石头却被 Bob 击中?如您所见,代码是相同的,唯一的区别是植物是动画对象,而石头不是。

【问题讨论】:

    标签: android collision-detection libgdx


    【解决方案1】:

    您可能希望将您的 OverlapTester 替换为 Rectangle 的辅助函数包含。例如:

    世界级

    if(OverlapTester.overlapRectangles(piante.get(i).bounds,omino.bounds)){
        omino.ominoMorto();
    }
    

    可以是:

    if (piante.get(i).bounds.contains(omino.bounds)) {
        omino.ominoMorto();
    }
    

    【讨论】:

      【解决方案2】:

      如果我理解正确 overlapRectangles 会检查 rectangle 是否完全在里面。这可能不是你想要的。

      LibGDX 具有特殊的碰撞检查功能。请检查http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Intersector.html

      【讨论】:

      • 那么为什么Libgdx的作者使用bounds和overlaptester类来检测碰撞? (overlaptteser 用于部分碰撞)
      • 我也尝试过交叉但也不起作用:-(问题也是一样的。我意识到工厂工作是因为它是大 2f 但如果它变得大 3f 她没有工作要么!!!
      • 是的。我重新检查了矩形碰撞。似乎很好...嗯...您使用 SuperJumper 示例中的 GameObject 类。正确的?你的物体会动吗?如果是,则检查边界是否正确更新,因为在我在 Internet (java2s.com/Open-Source/Android/Game/libgdx/com/badlogicgames/…) 上找到的代码中,边界仅在构造函数中初始化一次。如果您更改对象坐标边界保持不变。它会导致问题吗?
      • 绑定更新为Bob.classhttp://www.java2s.com/Open-Source/Android/Game/libgdx/com/badlogicgames/superjumper/Bob.java.htm 用于植物和石头它们是静态对象
      • 好的。如果它像这样工作,那很好。您是否尝试过打印矩形坐标或在调试器中检查它们?
      【解决方案3】:

      检查您的 OverlapTester。这就是 Libgdx 在 Rectangle.java 类中的做法:

      /** @param rectangle the other {@link Rectangle}
       * @return whether this rectangle overlaps the other rectangle. */
      public boolean overlaps (Rectangle rectangle) {
          return !(x > rectangle.x + rectangle.width || x + width < rectangle.x || y > rectangle.y + rectangle.height || y + height < rectangle.y);
      }
      

      【讨论】:

      • 好的,但我不明白为什么它适用于动画植物而不适用于石头......我从 Libgdx 的作者制作的项目 Superjumper 中学习了重叠测试类,所以它应该是正确的。 ...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多