I'm using JavaFX to move 3D cubes around by mouse drag. The cube should stay on the plane spanned by x and z axis. My solution works fairly well, however if I move the cube too fast with my mouse or when it encounters an object with a certain depth (y-Axis), it is assumed, that the mouse is moving on the y-Axis and the cube starts jumping forward or backwards. Is there a way to restrict the mouse to the xz-plane? A more complicated solution would be projecting the y length back to the xz-plane, but I got no clue how. I looked at JavaFX Moving 3D Objects, but couldn't adapt it to my case.
My code so far:
private volatile double relMousePosX;
private volatile double relMousePosZ;
public void enableDragMove(PaneBox paneBox) {
Group paneBoxGroup = paneBox.get();
paneBoxGroup.setOnMousePressed((MouseEvent me) -> {
if(isSelected(paneBox) && MouseButton.PRIMARY.equals(me.getButton())) {
relMousePosX = me.getX();
relMousePosZ = me.getZ();
}
});
paneBoxGroup.setOnMouseDragged((MouseEvent me) -> {
if(paneBoxGroup.focusedProperty().get() && MouseButton.PRIMARY.equals(me.getButton())) {
setDragInProgress(paneBox, true);
System.out.println(me.getY()); // should stay small value, but jumps to higher values at times, creating the problem.
paneBoxGroup.setCursor(Cursor.MOVE);
paneBox.setTranslateX(paneBox.getTranslateX() + (me.getX() - relMousePosX));
paneBox.setTranslateZ(paneBox.getTranslateZ() + (me.getZ() - relMousePosZ));
}
});
paneBoxGroup.setOnMouseReleased((MouseEvent me) -> {
if(paneBoxGroup.focusedProperty().get() && MouseButton.PRIMARY.equals(me.getButton())) {
setDragInProgress(paneBox, false);
paneBoxGroup.setCursor(Cursor.DEFAULT);
}
});
}