Search in sources :

Example 1 with SphereSegment

use of org.fxyz.shapes.SphereSegment in project FXyzLib by Birdasaur.

the class SphereSegmentTest method start.

@Override
public void start(Stage primaryStage) throws Exception {
    Group sceneRoot = new Group();
    Scene scene = new Scene(sceneRoot, sceneWidth, sceneHeight);
    scene.setFill(Color.BLACK);
    camera = new PerspectiveCamera(true);
    camera.setNearClip(0.1);
    camera.setFarClip(10000.0);
    camera.setTranslateZ(-1000);
    scene.setCamera(camera);
    //Make a bunch of semi random sphere segments and stuff
    Group sphereGroup = new Group();
    for (int i = 0; i < 30; i++) {
        Random r = new Random();
        //A lot of magic numbers in here that just artificially constrain the math
        float randomRadius = (float) ((r.nextFloat() * 150) + 10);
        float randomThetaMax = (float) ((r.nextFloat() * 360) + 1);
        float randomThetaMin = (float) ((r.nextFloat()) + 1);
        if (randomThetaMin > randomThetaMax) {
            float swap = randomThetaMin;
            randomThetaMin = randomThetaMax;
            randomThetaMax = swap;
        }
        float randomPolarMax = (float) ((r.nextFloat() * 90) + 1);
        float randomPolarMin = (float) ((r.nextFloat()) + 1);
        if (randomPolarMin > randomPolarMax) {
            float swap = randomPolarMin;
            randomPolarMin = randomPolarMax;
            randomPolarMax = swap;
        }
        int randomSegments = (int) ((r.nextFloat() * 15) + 5);
        Color randomColor = new Color(r.nextDouble(), r.nextDouble(), r.nextDouble(), r.nextDouble());
        boolean ambientRandom = r.nextBoolean();
        boolean fillRandom = r.nextBoolean();
        SphereSegment sphereSegment = new SphereSegment(randomRadius, randomColor, Math.toRadians(0), Math.toRadians(360), Math.toRadians(randomPolarMin), Math.toRadians(randomPolarMax), randomSegments, ambientRandom, fillRandom);
        double translationX = Math.random() * sceneWidth / 2;
        if (Math.random() >= 0.5)
            translationX *= -1;
        double translationY = Math.random() * sceneWidth / 2;
        if (Math.random() >= 0.5)
            translationY *= -1;
        double translationZ = Math.random() * sceneWidth / 2;
        if (Math.random() >= 0.5)
            translationZ *= -1;
        Translate translate = new Translate(translationX, translationY, translationZ);
        Rotate rotateX = new Rotate(Math.random() * 360, Rotate.X_AXIS);
        Rotate rotateY = new Rotate(Math.random() * 360, Rotate.Y_AXIS);
        Rotate rotateZ = new Rotate(Math.random() * 360, Rotate.Z_AXIS);
        sphereSegment.getTransforms().addAll(translate, rotateX, rotateY, rotateZ);
        sphereSegment.getTransforms().add(translate);
        sphereGroup.getChildren().add(sphereSegment);
    }
    sceneRoot.getChildren().addAll(sphereGroup);
    scene.setOnKeyPressed(event -> {
        double change = 10.0;
        if (event.isShiftDown()) {
            change = 50.0;
        }
        KeyCode keycode = event.getCode();
        if (keycode == KeyCode.W) {
            camera.setTranslateZ(camera.getTranslateZ() + change);
        }
        if (keycode == KeyCode.S) {
            camera.setTranslateZ(camera.getTranslateZ() - change);
        }
        if (keycode == KeyCode.A) {
            camera.setTranslateX(camera.getTranslateX() - change);
        }
        if (keycode == KeyCode.D) {
            camera.setTranslateX(camera.getTranslateX() + change);
        }
    });
    //Add a Mouse Handler for Rotations
    Rotate xRotate = new Rotate(0, Rotate.X_AXIS);
    Rotate yRotate = new Rotate(0, Rotate.Y_AXIS);
    Rotate zRotate = new Rotate(0, Rotate.Z_AXIS);
    sphereGroup.getTransforms().addAll(xRotate, yRotate);
    //Use Binding so your rotation doesn't have to be recreated
    xRotate.angleProperty().bind(angleX);
    yRotate.angleProperty().bind(angleY);
    zRotate.angleProperty().bind(angleZ);
    //Start Tracking mouse movements only when a button is pressed
    scene.setOnMousePressed(event -> {
        scenex = event.getSceneX();
        sceney = event.getSceneY();
        fixedXAngle = angleX.get();
        fixedYAngle = angleY.get();
        if (event.isMiddleButtonDown()) {
            scenez = event.getSceneX();
            fixedZAngle = angleZ.get();
        }
    });
    //Angle calculation will only change when the button has been pressed
    scene.setOnMouseDragged(event -> {
        if (event.isMiddleButtonDown())
            angleZ.set(fixedZAngle - (scenez - event.getSceneY()));
        else
            angleX.set(fixedXAngle - (scenex - event.getSceneY()));
        angleY.set(fixedYAngle + sceney - event.getSceneX());
    });
    primaryStage.setTitle("F(X)yz SphereSegmentTest");
    primaryStage.setScene(scene);
    primaryStage.show();
}
Also used : Group(javafx.scene.Group) Rotate(javafx.scene.transform.Rotate) SphereSegment(org.fxyz.shapes.SphereSegment) Color(javafx.scene.paint.Color) PerspectiveCamera(javafx.scene.PerspectiveCamera) Scene(javafx.scene.Scene) Random(java.util.Random) KeyCode(javafx.scene.input.KeyCode) Translate(javafx.scene.transform.Translate)

Aggregations

Random (java.util.Random)1 Group (javafx.scene.Group)1 PerspectiveCamera (javafx.scene.PerspectiveCamera)1 Scene (javafx.scene.Scene)1 KeyCode (javafx.scene.input.KeyCode)1 Color (javafx.scene.paint.Color)1 Rotate (javafx.scene.transform.Rotate)1 Translate (javafx.scene.transform.Translate)1 SphereSegment (org.fxyz.shapes.SphereSegment)1