I'm trying to figure out how pivot points work in 3D. I'm trying to make a 3D box rotate on the edge of the box instead of in the center. Any help would be appreciated. Thanks!
Asked
Active
Viewed 705 times
1 Answers
1
You can apply a Rotate
transform to your Box
, as it allows setting the angle, pivot point and axis of rotation. See javadoc.
This will apply a transform over the center of the box:
Rotate rotate = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS);
Box box = new Box(100, 100, 100);
box.getTransforms().add(rotate);
while this will apply over one of its edges:
Rotate rotate = new Rotate(0, -50, -50, 0, Rotate.Z_AXIS);
Box box = new Box(100, 100, 100);
box.getTransforms().add(rotate);
This is a quick test with an animation of both cases:
private long time;
private final IntegerProperty counter = new SimpleIntegerProperty();
@Override
public void start(Stage primaryStage) {
Rotate rotate1 = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS);
rotate1.angleProperty().bind(counter);
Box box1 = new Box(100, 100, 100);
box1.setMaterial(new PhongMaterial(Color.LIMEGREEN));
box1.getTransforms().add(rotate1);
Cylinder axis1 = new Cylinder(2, 200);
axis1.setRotationAxis(Rotate.X_AXIS);
axis1.setRotate(90);
axis1.setMaterial(new PhongMaterial(Color.RED));
Group group1 = new Group(box1, axis1);
group1.setTranslateX(-100);
Rotate rotate2 = new Rotate(0, -50, -50, 0, Rotate.Z_AXIS);
rotate2.angleProperty().bind(counter);
Box box2 = new Box(100, 100, 100);
box2.setMaterial(new PhongMaterial(Color.LAVENDER));
box2.getTransforms().add(rotate2);
Cylinder axis2 = new Cylinder(2, 200);
axis2.setRotationAxis(Rotate.X_AXIS);
axis2.setRotate(90);
axis2.setTranslateX(-50);
axis2.setTranslateY(-50);
axis2.setMaterial(new PhongMaterial(Color.RED));
Group group2 = new Group(box2, axis2);
group2.setTranslateX(200);
Group root = new Group(group1, group2);
SubScene subScene = new SubScene(root, 600, 400, true, SceneAntialiasing.BALANCED);
PerspectiveCamera camera = new PerspectiveCamera();
camera.setTranslateX(-200);
camera.setTranslateY(-200);
camera.setTranslateZ(-100);
camera.setRotationAxis(new Point3D(0, 0.5, 0.5));
camera.setRotate(10);
subScene.setCamera(camera);
Scene scene = new Scene(new StackPane(subScene), 600, 400, true, SceneAntialiasing.BALANCED);
primaryStage.setScene(scene);
primaryStage.show();
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
if (now - time > 30_000_000) {
counter.set((counter.get() + 1) % 360);
time = now;
}
}
};
timer.start();
}

José Pereda
- 44,311
- 7
- 104
- 132
-
dude thank you. Those red lines really helped make it a lot clearer. Appreciate it!! – saladsrock May 01 '17 at 16:24
-
Glad it helped you. Consider marking the answer as accepted so it can be helpful to others as well. – José Pereda May 01 '17 at 17:56