Here’s the relevant code I have:
public class OpenViewController implements Initializable {
@FXML
private SubScene mySubScene;
@FXML
private VBox mainVBox;
@FXML
private Text coordinateDisplay;
private final Translate translate = new Translate(0, 0, 0);
private final Translate translateZ = new Translate(0, 0, -1070);
private final Rotate rotateX = new Rotate(-120, 0, 0, 0, Rotate.X_AXIS);
private final Rotate rotateY = new Rotate(180, 0, 0, 0, Rotate.Y_AXIS);
private final Translate translateY = new Translate(0, 0, 0);
@Override
public void initialize(URL location, ResourceBundle resources) {
setupRiggedHands();
}
private void setupRiggedHands() {
// Some irrelevant code...
PerspectiveCamera perspectiveCamera = new PerspectiveCamera();
perspectiveCamera.setTranslateZ(-400);
perspectiveCamera.setNearClip(0.001);
perspectiveCamera.setFarClip(10000);
mySubScene.setCamera(perspectiveCamera);
mySubScene.widthProperty().bind(mainVBox.widthProperty());
mySubScene.heightProperty().bind(mainVBox.heightProperty().subtract(50));
Translate centerTranslate = new Translate();
centerTranslate.xProperty().bind(mySubScene.widthProperty().divide(2));
centerTranslate.yProperty().bind(mySubScene.heightProperty().divide(2));
mySubScene.getRoot().getTransforms().addAll(centerTranslate, translate, translateZ, rotateX, rotateY, translateY);
mySubScene.setPickOnBounds(true);
new DragSupport(mySubScene, null, MouseButton.SECONDARY, Orientation.VERTICAL, translateZ.zProperty(), -3);
new DragSupport(mySubScene, null, Orientation.HORIZONTAL, rotateY.angleProperty());
new DragSupport(mySubScene, null, Orientation.VERTICAL, rotateX.angleProperty());
new DragSupport(mySubScene, null, MouseButton.MIDDLE, Orientation.HORIZONTAL, translate.xProperty());
new DragSupport(mySubScene, null, MouseButton.MIDDLE, Orientation.VERTICAL, translate.yProperty());
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
// Get the bounds of the SubScene
Bounds bounds = mySubScene.getBoundsInParent();
double distanceX = Math.abs(translate.getX());
double distanceY = Math.abs(translateY.getY());
double distanceZ = Math.abs(translateZ.getZ() - perspectiveCamera.getTranslateZ());
// Update the coordinate display with the calculated distances
coordinateDisplay.setText(String.format("X: %.2f, Y: %.2f, Z: %.2f", distanceX, distanceY, distanceZ));
}
};
// Start the timer
timer.start();
}
}
I am developing a JavaFX application that displays two sets of hands, where the user can change the perspective by dragging the mouse and zoom in or out by right-clicking and dragging. However, I’m encountering an issue when trying to display the live coordinates (X, Y, Z) of the center of the hands relative to the PerspectiveCamera.
Here are the problems I’m facing:
- The X and Y coordinates are always zero and do not update as I move the scene. Only the Z coordinate works as expected.
- When I resize the window, the distance from the camera to the object (hands) changes unexpectedly, but the Z coordinate remains constant.
- I want to display the coordinates of the object’s center (i.e., the center between the two hands) in a way that adjusts correctly when resizing the scene or changing the camera’s perspective.
Here is an example of what it looks like in normal view
And here is what its like in full screen
As you can see the coordinate stays the same, and I have to zoom in to a completely different z coord to get a good view on full screen.
Also here is my DragSupport constructer I am not sure if the code here is relevant
public DragSupport(SubScene target, final KeyCode modifier, final MouseButton mouseButton, final Orientation orientation, final Property<Number> property, final double factor) {
// Code used for handling movement
}