Java的3D - 建立3D模型动态 [英] Java 3D - Build 3D models dynamically

查看:4074
本文介绍了Java的3D - 建立3D模型动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去建立一个三维模型,通过建立动态三维模型,并将其转化为在那里我需要他们。

即时通讯开始一个基本的模型,试图达到什么是对的画面波纹管。我想动态构建两个汽缸,并在X,Y,Z我的圆筒顶部的将是相同的,X,Y,Z上的第二缸的底部,如像波纹管的:

现在我有这个code:

 公共静态无效的主要(字串[] args)抛出FileNotFoundException异常,IOException异常{
    INT高= 10;
    INT半径= 1;
    INT角= 0;

    BranchGroup objRoot =新BranchGroup();
    汽缸的缸;
    Vector3f last_coordinates =新Vector3f(0F,0F,0F);
    的TransformGroup transf_group_cylinder = NULL;



    // ----工作OK ----- /
    //构造缸
    缸=新的钢瓶(半径,高度);
    transf_group_cylinder = createTransformGroup_Cylinder(新Vector3f(0F,0F,0F),角度);
    transf_group_cylinder.addChild(缸);
    objRoot.addChild(transf_group_cylinder);

    last_coordinates = calculateLastPoint(身高/ 2,角度);
    的System.out.println(last_coordinates);
    // ---------------------------- //



    //建立第二汽缸
    缸=新的钢瓶(半径,高度);
    transf_group_cylinder = createTransformGroup_Cylinder(last_coordinates,Math.PI / 2);
    transf_group_cylinder.addChild(缸);
    objRoot.addChild(transf_group_cylinder);

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

}

私有静态Vector3f calculateLastPoint(INT高度,诠释角度){
    浮动X =(浮点)(高* Math.sin(角度));
    浮动Y =(浮点)(高* Math.cos(角度));

    返回新Vector3f(0,X,Y);
}

私有静态的TransformGroup createTransformGroup_Cylinder(
        Vector3f last_coordinates,双角){

    的TransformGroup transf_group =新的TransformGroup();

    //模型的位置
    的Transform3D transform_origin =新的Transform3D();
    transform_origin.setTranslation(新Vector3f(0,0,0));

    在水平位置//集模型
    的Transform3D transf_horizo​​ntal =新的Transform3D();
    transf_horizo​​ntal.rotZ(Math.PI / 2);
    transform_origin.mul(transf_horizo​​ntal);

    //旋转对象
    的Transform3D angleRotation =新的Transform3D();
    angleRotation.rotX(角度);
    transform_origin.mul(angleRotation);

    的Transform3D transform_xyz =新的Transform3D();
    transform_xyz.s​​etTranslation(last_coordinates);
    transform_origin.mul(transform_xyz); //设置变换

    transf_group.setTransform(transform_origin);

    返回transf_group;
}
 

通过这个code IM实现这一目标:

我的第一个缸是好的,但我的第二个气缸不放在一个适当的位置。 我可以添加任何的大小和角度值的价值,所以我需要计算在动态的方式这两个值。

有人可以帮助解决这个翻译问题?

感谢你在前进。

解决方案

在这里的第一个步骤就是给每个气缸不同的颜色,让你看看哪个是哪个。

下一页:当你创建一个圆柱体,然后它的中心在原点。既然你想链他们在最后一点上,你需要相应地移动:首先,你需要约一半的高度移动的缸(或它的转换矩阵)几乎将缸原点到它的结束<。 / P>

下一步是,需要应用相同的变换矩阵到终点(此时加全汽缸高度),因此它线与实际的结束点。

不过,我会建议你创建一个辅助函数,可以在两个点之间创建一个圆柱体。这将允许你说:

  Point端点=缸(新点(-1,.5,0),新点(0,.5,0))
缸(端点,新点(0, -  5,0))
...
 

甚至可以创建一个辅助函数,它接受一个点的列表以及它们之间创建所有的油缸。

im trying to build a 3D Model, by building dynamically 3D models and translate them to where i need them.

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:

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.

这篇关于Java的3D - 建立3D模型动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆