Question

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: What i want to achieve

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: enter image description here

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top