1

In JavaFX, I want to add a camera that orbits around an object like a satellite. I want the orbit to move along a horizontal and vertical line around the object. I also want the camera to always be pointed at the object in the center of the matrix.

At the moment i try to move the camera along the x and y axis by using the unit circle. The code looks like this at the moment:

<code>
int r = 10;
        Slider nxSlider = new Slider(0, 360, 0);
        nxSlider.valueProperty().addListener((observable, oldvalue, newvalue) ->
        {
            double i = newvalue.doubleValue();
            camera.setTranslateX(r * Math.cos(Math.toRadians(i)));
            camera.setTranslateY(r * Math.sin(Math.toRadians(i)));
            rotateX.setAngle(Math.toDegrees(Math.cos(Math.toRadians(i))));
        });
        rotateZ.setAngle(0);
        rotateY.setAngle(0);
        rotateX.setAngle(0);
        camera.setTranslateX(r);
        camera.setTranslateZ(0);
        camera.setTranslateY(0);
</code>

Where as rotateX, rotateY and rotateZ are Transform Rotates.

I think im pretty lost and I've had this problem for a long time. My code is probably a wrong and I would be extremly grateful if anyone could propose an idea of how I can continue.

Kalle H.
  • 11
  • 1
  • This is less a coding question and more about the mathematics of the translation. You might get a better response here: https://math.stackexchange.com/ – Matt Clark Mar 23 '20 at 19:57
  • Try to set the pivot of the rotation to the location of the object you rotate around. – user1803551 Apr 25 '20 at 14:35

1 Answers1

1

I don't get why you would bother with any of the math, since all that's been done in JavaFX already. Instead of using the Translate functionality, use Rotate. Here is a very simple little example of orbiting an object with the camera:

import javafx.application.Application;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

public class TestCamera extends Application {
    Group root = new Group();
    Box box = new Box(100, 20, 50);
    Camera camera = new PerspectiveCamera(true);
    Rotate rotation;

    @Override
    public void start(Stage primaryStage) throws Exception {

        box.setMaterial(new PhongMaterial(Color.BLUE));
        root.getChildren().add(box);

        Scene scene = new Scene(root, 800, 800, Color.BLACK);

        // this "backs you off" from the origin by 1000
        camera.setTranslateZ(-1000);

        // this is the crucial part here, where you set the pivot point
        // of your rotation, in this case 1000 "in front" of you
        // (these are "local coordinates" to the camera)
        // rotation = new Rotate(0, 0, 0, 1000, Rotate.Y_AXIS);
        // while the above works, if you want to rotate around two
        // axes at once, you can use do it like this
        rotation = new Rotate(0, 0, 0, 1000, new Point3D(1, 1, 0);
        camera.getTransforms().add(rotation);

        camera.setNearClip(0.01);
        camera.setFarClip(10000);
        scene.setCamera(camera);
        primaryStage.setScene(scene);
        primaryStage.show();

        scene.setOnKeyPressed(event -> {
            switch(event.getCode()) {
                case LEFT:
                    rotation.setAngle(rotation.getAngle() - 10);
                    break;
                case RIGHT:
                    rotation.setAngle(rotation.getAngle() + 10);
                    break;
            }
        });
    }

    public static void main(String[] args) {
        launch();
    }
}