在Java3D中将较小的盒子装入较大的盒子中

时间:2016-02-26 16:00:58

标签: java java-3d

因此我无法将较小的盒子放入较大的盒子(容器)中。我试图从一个盒子中获取一个顶点的坐标,然后将另一个放在那个位置。这是我试过的代码:

 Shape3D backShape = ((Box) buildShape(x, y, z)).getShape(Box.RIGHT);

    GeometryArray geo=(GeometryArray)backShape.getGeometry();
    float[] position=new float[3];
    geo.getCoordinate(0, position);
    System.out.println(position[0]+" "+position[1]+" "+position[2]);
    Vector3f pp = new Vector3f (position[0],position[1], position[2]);

    Color3f c1=new Color3f(0.7f, .15f, .15f);
    Color3f c2=new Color3f(0.7f, .15f, .15f);
    Color3f[] c=(Color3f[]) new Color3f[20] ; 

        c[0] =new Color3f(Color.getHSBColor(20.f, 30f, 2.f));
        c[1] =new Color3f(Color.BLUE);
        c[2] =new Color3f(Color.YELLOW);
        c[3] =new Color3f(Color.GREEN);
        c[4] =new Color3f(Color.WHITE);
        c[5] =new Color3f(Color.CYAN);
        c[6] =new Color3f(Color.LIGHT_GRAY);
        c[7] =new Color3f(Color.PINK);
        c[8] =new Color3f(Color.RED);
        c[9] =new Color3f(Color.DARK_GRAY);
        c[10] =new Color3f(Color.MAGENTA);
        c[11] =new Color3f(Color.getHSBColor(0.2f, 0.15f, 2.f));
        c[11] =new Color3f(Color.getHSBColor(0.8f, 0.1f, 15.f));
        c[13] =new Color3f(Color.ORANGE);
        c[11] =new Color3f(Color.getHSBColor(1.f, 0.8f, 2.f));

    for (int i=0; i<boxes.size(); i++) {

            Transform3D transform = new Transform3D();
            transform.setTranslation(pp);
            Box ProductBox = addProductBox(boxes.get(i).dimension.get("length"),boxes.get(i).dimension.get("breadth")
                    ,boxes.get(i).dimension.get("height"),c1, c2,c[i],c[i]);

            Transform3D rotateCube = new Transform3D();
            rotateCube.set(new AxisAngle4d(1.0, 1.0, 0.0, Math.PI / 4.0));
            TransformGroup rotationGroup = new TransformGroup(rotateCube);
            TransformGroup TranslateGroup = new TransformGroup(transform);
            TranslateGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
            rotationGroup.addChild(ProductBox);
            TranslateGroup.addChild(rotationGroup);
            nodeRoot.addChild(TranslateGroup);
            Shape3D shape = ProductBox.getShape(Box.FRONT);
          GeometryArray g=(GeometryArray)shape.getGeometry();
            position[0]-= boxes.get(i).dimension.get("length")+0.15f;
           System.out.println(position[0]+" "+position[1]+" "+position[2]);

           Vector3f pp2 = new Vector3f (position[0],position[1],position[2]);
           pp=pp2;
           c[i]=c[i+1];
    }

我通过在每次迭代中减去位置[0]的长度,成功地将所有框放在容器中。但盒子没有排成一列。 我附上了一张图片,显示了我到目前为止所做的工作: here

1 个答案:

答案 0 :(得分:0)

我有这个简单的程序。假设你有一个Shape3D形状:

picture_src

这样我就可以得到形状的坐标。你必须很好地定位到3D空间才能理解。你必须通过定位你的对象进行实验(一开始就只有一个)。