简体   繁体   English

Java 3D - 动态构建3D模型

[英]Java 3D - Build 3D models dynamically

im trying to build a 3D Model, by building dynamically 3D models and translate them to where i need them. 我试图通过构建动态3D模型并将它们转换到我需要的地方来构建3D模型。

Im starting with a basic model, trying to to achieve what is on the picture bellow. 我从一个基本模型开始,试图实现下面的图片。 I want to dynamically build two cylinders, and the X,Y,Z of the TOP of my cylinder will be the same X,Y,Z of the BOTTOM of the second Cylinder, like the picture bellow: 我想动态构建两个圆柱体,我的圆柱体TOP的X,Y,Z将与第二个圆柱体的BOTTOM相同,X,Y,Z,如下图所示: 我想要实现的目标

For now i have this code: 现在我有这个代码:

public static void main(String[] args) throws FileNotFoundException, IOException {
    int height = 10;
    int radius = 1;
    int angle = 0;

    BranchGroup objRoot = new BranchGroup();
    Cylinder cylinder;
    Vector3f last_coordinates = new Vector3f(0f,0f,0f);
    TransformGroup transf_group_cylinder = null;



    //---- Working ok -----/
    //build cylinder
    cylinder = new Cylinder(radius, height);
    transf_group_cylinder = createTransformGroup_Cylinder(new Vector3f(0f,0f,0f),angle);    
    transf_group_cylinder.addChild(cylinder);
    objRoot.addChild(transf_group_cylinder);

    last_coordinates = calculateLastPoint(height/2, angle);
    System.out.println(last_coordinates);
    //----------------------------//



    //build 2nd cylinder
    cylinder = new Cylinder(radius, height);
    transf_group_cylinder = createTransformGroup_Cylinder(last_coordinates, Math.PI/2); 
    transf_group_cylinder.addChild(cylinder);
    objRoot.addChild(transf_group_cylinder);

    OBJWriter objWriter = new OBJWriter("myObj.obj");
    objWriter.writeNode(objRoot);
    objWriter.close();

}

private static Vector3f calculateLastPoint(int height, int angle) {
    float x = (float) (height * Math.sin(angle));
    float y = (float) (height * Math.cos(angle));

    return new Vector3f(0, x, y);
}

private static TransformGroup createTransformGroup_Cylinder(
        Vector3f last_coordinates, double angle) {

    TransformGroup transf_group = new TransformGroup();

    //position the model
    Transform3D transform_origin = new Transform3D();
    transform_origin.setTranslation(new Vector3f(0, 0, 0)); 

    // set model in horizontal position
    Transform3D transf_horizontal = new Transform3D();
    transf_horizontal.rotZ(Math.PI / 2);
    transform_origin.mul(transf_horizontal);

    // rotate object
    Transform3D angleRotation = new Transform3D();
    angleRotation.rotX(angle);
    transform_origin.mul(angleRotation);

    Transform3D transform_xyz = new Transform3D();
    transform_xyz.setTranslation(last_coordinates);
    transform_origin.mul(transform_xyz); // set Transform for

    transf_group.setTransform(transform_origin);

    return transf_group;
}

With this code im achieving this: 使用此代码实现此目的: 在此输入图像描述

My first cylinder is ok, but my 2nd cylinder is not placed in a proper place. 我的第一个气缸没问题,但我的第二个气缸没有放在合适的位置。 I can add any value for the size and angle values , so i need to calculate this two values in a dynamically way. 我可以为大小和角度值添加任何值 ,所以我需要动态地计算这两个值。

Can someone help solving this translation problem? 有人可以帮助解决这个翻译问题吗?

Thank you in advance. 先感谢您。

The first step here is to give each cylinder a different color to allow you to see which one is which. 这里的第一步是给每个圆柱体一个不同的颜色,让你看看哪个是哪个。

Next: When you create a cylinder, then it's centered at the origin. 下一步:创建圆柱体时,它以原点为中心。 Since you want to chain them at the end point, you need to move them accordingly: First, you need to move the cylinder (or its transformation matrix) by -half its height to virtually move the "cylinder origin" to it's end. 由于您希望在终点处链接它们,因此需要相应地移动它们:首先,您需要将圆柱体(或其变换矩阵)移动半个高度,以便将“圆柱体原点”移动到它的末端。

The next step is that you need to apply the same transformation matrix to the end point (this time plus a full cylinder height), so it lines up with the actual end point. 下一步是您需要将相同的变换矩阵应用于终点(此时加上一个完整的柱面高度),因此它与实际终点对齐。

That said, I would suggest you create a helper function that can create a cylinder between two points. 也就是说,我建议你创建一个辅助函数,可以在两点之间创建一个圆柱体。 This would allow you to say: 这可以让你说:

Point endPoint = cylinder(new Point(-1,.5,0), new Point(0,.5,0))
cylinder(endPoint, new Point(0,-.5,0))
...

or even create a helper function which accepts a list of points and creates all the cylinders between them. 甚至创建一个辅助函数,它接受一个点列表并创建它们之间的所有柱面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM