边界框不计算Libgdx中的正确位置

时间:2016-03-24 06:50:16

标签: java libgdx

我正在尝试创建一个模型,我用libgdx中的一个框创建了检测碰撞 - 我有5个框并使用边界框来检查碰撞。

这是碰撞的3D场景: enter image description here

无碰撞

enter image description here

那一点,我期待发生碰撞,但没有检测到任何东西,因为边界框完全不同。当应该发生碰撞时,这是每个盒子和钩子的边界框的中心(图1):

03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BBHOOK CORNER000: (13.938202,963.2201,-2830.7458)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BBHOOK CORNER001: (13.938202,963.2201,-2788.6057)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BBHOOK CORNER010: (13.938202,1011.61804,-2830.7458)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BBHOOK CORNER011: (13.938202,1011.61804,-2788.6057)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BBHOOK CORNER100: (29.528505,963.2201,-2830.7458)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BBHOOK CORNER101: (29.528505,963.2201,-2788.6057)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BBHOOK CORNER110: (29.528505,1011.61804,-2830.7458)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BBHOOK CORNER111: (29.528505,1011.61804,-2788.6057)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 0: (0.0,15.0,-10.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 0:  CORNER000 (-10.0,5.0,-20.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 0:  CORNER001 (-10.0,5.0,0.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 0:  CORNER010 (-10.0,25.0,-20.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 0:  CORNER011 (-10.0,25.0,0.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 0:  CORNER100 (10.0,5.0,-20.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 0:  CORNER101 (10.0,5.0,0.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 0:  CORNER110 (10.0,25.0,-20.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 0:  CORNER111 (10.0,25.0,0.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 1: (0.0,15.0,-40.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 1:  CORNER000 (-10.0,5.0,-50.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 1:  CORNER001 (-10.0,5.0,-30.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 1:  CORNER010 (-10.0,25.0,-50.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 1:  CORNER011 (-10.0,25.0,-30.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 1:  CORNER100 (10.0,5.0,-50.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 1:  CORNER101 (10.0,5.0,-30.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 1:  CORNER110 (10.0,25.0,-50.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 1:  CORNER111 (10.0,25.0,-30.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 2: (0.0,15.0,-70.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 2:  CORNER000 (-10.0,5.0,-80.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 2:  CORNER001 (-10.0,5.0,-60.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 2:  CORNER010 (-10.0,25.0,-80.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 2:  CORNER011 (-10.0,25.0,-60.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 2:  CORNER100 (10.0,5.0,-80.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 2:  CORNER101 (10.0,5.0,-60.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 2:  CORNER110 (10.0,25.0,-80.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 2:  CORNER111 (10.0,25.0,-60.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 3: (0.0,15.0,-100.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 3:  CORNER000 (-10.0,5.0,-110.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 3:  CORNER001 (-10.0,5.0,-90.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 3:  CORNER010 (-10.0,25.0,-110.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 3:  CORNER011 (-10.0,25.0,-90.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 3:  CORNER100 (10.0,5.0,-110.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 3:  CORNER101 (10.0,5.0,-90.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 3:  CORNER110 (10.0,25.0,-110.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 3:  CORNER111 (10.0,25.0,-90.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 4: (0.0,15.0,-130.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 4:  CORNER000 (-10.0,5.0,-140.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 4:  CORNER001 (-10.0,5.0,-120.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 4:  CORNER010 (-10.0,25.0,-140.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 4:  CORNER011 (-10.0,25.0,-120.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 4:  CORNER100 (10.0,5.0,-140.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 4:  CORNER101 (10.0,5.0,-120.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 4:  CORNER110 (10.0,25.0,-140.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: BB BOX 4:  CORNER111 (10.0,25.0,-120.0)
03-24 01:25:23.634 17593-17607/com.gdx.crane I/System.out: Interesects = -1

这是我创建盒子和起重机模型的代码:

    public class Crane {
        public ModelInstance modelInstance;
        public Node cuerda,polea,brazo;
        ArrayList<ModelInstance> boxes;
        int pulleyMoves,maxPulleyMoves,minPulleyMoves;
        int armMoves,maxArmMoves,minArmMoves;
        int ropeMoves,maxRopeMoves,minRopeMoves;
    public Crane(){
        ModelBuilder modelBuilder = new ModelBuilder();
        UBJsonReader jsonReader = new UBJsonReader();
        G3dModelLoader modelLoader = new G3dModelLoader(jsonReader);
        Model model;
        model = modelLoader.loadModel(Gdx.files.getFileHandle("crane.g3db", Files.FileType.Internal));
        modelInstance = new ModelInstance(model);
        modelInstance.transform.scale(0.05f, 0.05f, 0.05f);
        modelInstance.transform.translate(0, 0, 500);
        modelInstance.calculateTransforms();
        cuerda = modelInstance.getNode("cuerda_mover");
        polea = modelInstance.getNode("polea");
        brazo = modelInstance.getNode("Cube.109");
        model = modelBuilder.createBox(20, 20, 20, new Material(ColorAttribute.createDiffuse(Color.BLUE)),
                VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal);
        boxes= new ArrayList<ModelInstance>();
        boxes.add(new ModelInstance(model));
        boxes.add(new ModelInstance(model));
        boxes.add(new ModelInstance(model));
        boxes.add(new ModelInstance(model));
        boxes.add(new ModelInstance(model));
        boxes.get(0).transform.translate(0, 15, -10);
        boxes.get(1).transform.translate(0, 15, -40);
        boxes.get(2).transform.translate(0, 15, -70);
        boxes.get(3).transform.translate(0, 15, -100);
        boxes.get(4).transform.translate(0, 15, -130);

        for(ModelInstance box: boxes){
            box.calculateTransforms();

        }



    }
public int intersects(){

    Node hook = this.modelInstance.getNode("gancho");

    BoundingBox bbCrane = hook.calculateBoundingBox(new BoundingBox());


    System.out.println("BB HOOK: "+bbCrane.getCenter(new Vector3(0, 0, 0)).toString());
    int i =0;
    for(ModelInstance box: this.boxes){

        BoundingBox bbBox = box.calculateBoundingBox(new BoundingBox());
        bbBox .mul(box.transform);
        System.out.println("BB BOX "+i+": "+bbBox.getCenter(new Vector3(0, 0, 0)).toString());
        if(bbCrane.intersects(bbBox )){
            System.out.println("------------------------------------------ COLISION !!!! -------------------------------------------");
            return i;
        }
        i++;
    }
    return -1;


}

    }

我不明白为什么钩子的边界盒子坐标与盒子相比如此之大。任何人都可以帮我解决这个问题吗?

编辑:

如果它有用,我上下移动绳子的方法是将钩子的父节点(即绳索)按z轴上的因子缩放。

0 个答案:

没有答案