I am creating a JavaFx project and would like to be able to draw connecting lines in a 3D model. I see simple shapes offered like cylinder, sphere, and rectangles, but no 3D lines. Is there a simple way to add lines in three dimensions instead of two dimensions in JavaFX without using a 3rd party API?
I came across this answer while researching this issue and you don't need any additional 3rd party APIs. The object passed into the method contains two Point3D objects used for the start and end points of your line. This method creates cylinders and then rotates them to the correct orientation in 3D and is based on trigonometry:
static public Cylinder createCylinder(Segment segment) {
// x axis vector is <1,0,0>
// y axis vector is <0,1,0>
// z axis vector is <0,0,1>
// angle = arccos((P*Q)/(|P|*|Q|))
// define a point representing the Y axis
Point3D yAxis = new Point3D(0,1,0);
// define a point based on the difference of our end point from the start point of our segment
Point3D seg = segment.getEnd().subtract(segment.getStart());
// determine the length of our line or the height of our cylinder object
double height = seg.magnitude();
// get the midpoint of our line segment
Point3D midpoint = segment.getEnd().midpoint(segment.getStart());
// set up a translate transform to move to our cylinder to the midpoint
Translate moveToMidpoint = new Translate(midpoint.getX(), midpoint.getY(), midpoint.getZ());
// get the axis about which we want to rotate our object
Point3D axisOfRotation = seg.crossProduct(yAxis);
// get the angle we want to rotate our cylinder
double angle = Math.acos(seg.normalize().dotProduct(yAxis));
// create our rotating transform for our cylinder object
Rotate rotateAroundCenter = new Rotate(-Math.toDegrees(angle), axisOfRotation);
// create our cylinder object representing our line
Cylinder line = new Cylinder(1, height);
// add our two transfroms to our cylinder object
line.getTransforms().addAll(moveToMidpoint, rotateAroundCenter);
// return our cylinder for use
return line;
} // end of the createCylinder method