Issue
My problem is that after moving my camera away from the cube(-z direction), with setFarClip(-1000), to the position -1010 it won't disappear.
Here is the code:
@Override
public void start(Stage stage) throws IOException {
Box box = new Box(100,100,100);
Camera cam = new PerspectiveCamera(true);
Group group = new Group();
group.getChildren().add(box);
Scene scene = new Scene(group,1920,1080);
scene.setCamera(cam);
cam.translateXProperty().set(0);
cam.translateYProperty().set(0);
cam.translateZProperty().set(-100);
cam.setNearClip(1);
cam.setFarClip(-1000);
scene.setFill(Color.SILVER);
System.out.println("CamX: "+scene.getCamera().getTranslateX()
+", CamY: "+scene.getCamera().getTranslateY()+", CamZ: "+scene.getCamera().getTranslateZ());
stage.addEventHandler(KeyEvent.KEY_PRESSED, event ->{
switch(event.getCode())
{
case W :
cam.translateZProperty().set(cam.getTranslateZ()+10);
System.out.println("CamX: "+scene.getCamera().getTranslateX()
+", CamY: "+scene.getCamera().getTranslateY()+", CamZ: "+scene.getCamera().getTranslateZ());
break;
case A:
cam.translateXProperty().set(cam.getTranslateX()-10);
System.out.println("CamX: "+scene.getCamera().getTranslateX()
+", CamY: "+scene.getCamera().getTranslateY()+", CamZ: "+scene.getCamera().getTranslateZ());
break;
case D:
cam.translateXProperty().set(cam.getTranslateX()+10);
System.out.println("CamX: "+scene.getCamera().getTranslateX()
+", CamY: "+scene.getCamera().getTranslateY()+", CamZ: "+scene.getCamera().getTranslateZ());
break;
case S:
cam.translateZProperty().set(cam.getTranslateZ()-10);
System.out.println("CamX: "+scene.getCamera().getTranslateX()
+", CamY: "+scene.getCamera().getTranslateY()+", CamZ: "+scene.getCamera().getTranslateZ());
break;
}
});
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
Instead of printing scence.getCamera().getTranslateX() out, I tried cam.getTranslateX(), which would always give 0.0. So either way it always registers the position as 0.0 and will never be beyond the farclip plane or something entirely else which I didn't grasp or consider. Thank you for every tip.
Solution
You have the sign wrong, instead of:
cam.setFarClip(-1000);
write:
cam.setFarClip(1000);
A negative value for farClip would mean to clip behind the camera, which makes no sense.
You may want to review the coordinate directions for axes:
In the JavaFX scene coordinate space, the default camera's projection plane is at Z=0 and camera coordinate system is as follows:
X-axis points to the right
Y-axis points down
Z-axis points away from the viewer or into the screen.
So a positive Z (which is the measurement for farClip in default camera orientation), is a location into the screen, and a negative Z would be a location behind the screen.
Answered By - jewelsea
Answer Checked By - David Marino (JavaFixing Volunteer)