JavaFX 3D Tutorial #3 – 3D Object Transform (Rotation) with Keyboard Input

In this chapter, we will talk about applying transformation on the 3D objects. JavaFX provides following transforms

  1. Affine
  2. Rotate
  3. Scale
  4. Shear
  5. Translate

For now, we make use of Rotate transformation. All the transformations can be applied in a similar manner.

Applying Transforms

All JavaFX nodes have the method getTransforms() that will return an observable list of all the transforms applied on that. You can simple add a new transform by

 Transform whatever = new Transform();
 yourObject.getTransforms().add(whatever);

When applying transform, it is important that you take care of existing transformation. Let’s say, your object is currently rotated 30 degrees, when you rotate it again by 10, you have to add with 30.

This can be achieved using createConcatenation() method that combines two transformations.

Transformable Container

When we work on 3D programming, we will have to do the transformation regularly. The following class helps to do this in the easy way.

class SmartGroup extends Group {
    Rotate r;
    Transform t = new Rotate();

    void rotateByX(int ang) {
      r = new Rotate(ang, Rotate.X_AXIS);
      t = t.createConcatenation(r);
      this.getTransforms().clear();
      this.getTransforms().addAll(t);
    }

    void rotateByY(int ang) {
      r = new Rotate(ang, Rotate.Y_AXIS);
      t = t.createConcatenation(r);
      this.getTransforms().clear();
      this.getTransforms().addAll(t);
    }
  }

With this SmartGroup, you can separate the rotation logic. It also remembers previous rotation values.

With this, you can rotate an object or a set of objects (the whole container) as follows.

    SmartGroup group = new SmartGroup();
    //Add all the components
    group.getChildren().addAll(box, sphere);

    //Rotate by X - 10 degrees
    group.rotateByX(10);
    //Make it 20
   group.rotateByX(10); 

Listen for Keyboard Inputs

You can register for keyboard press events using KeyEvent.KEY_PRESSED. We have to attach a keyboard listener to the stage itself so that all the key events can be tracked.

primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
   //Process according to the key pressed.
   switch (event.getCode()) {
   }
});

Working Example

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


/**
 * @author afsal villan
 * @version 1.0
 *
 * https://genuinecoder.com
 */
public class Rotation3D extends Application {

  private static final int WIDTH = 1400;
  private static final int HEIGHT = 800;

  @Override
  public void start(Stage primaryStage) {
    //Create box
    Box box = new Box(100, 20, 50);

    //Prepare transformable Group container
    SmartGroup group = new SmartGroup();
    group.getChildren().add(box);

    Camera camera = new PerspectiveCamera();
    Scene scene = new Scene(group, WIDTH, HEIGHT);
    scene.setFill(Color.SILVER);
    scene.setCamera(camera);

    //Move to center of the screen
    group.translateXProperty().set(WIDTH / 2);
    group.translateYProperty().set(HEIGHT / 2);
    group.translateZProperty().set(-1200);

    //Add keyboard control.
    primaryStage.addEventHandler(KeyEvent.KEY_PRESSED, event -> {
      switch (event.getCode()) {
        case W:
          group.translateZProperty().set(group.getTranslateZ() + 100);
          break;
        case S:
          group.translateZProperty().set(group.getTranslateZ() - 100);
          break;
        case Q:
          group.rotateByX(10);
          break;
        case E:
          group.rotateByX(-10);
          break;
        case NUMPAD6:
          group.rotateByY(10);
          break;
        case NUMPAD4:
          group.rotateByY(-10);
          break;
      }
    });

    primaryStage.setTitle("Genuine Coder");
    primaryStage.setScene(scene);
    primaryStage.show();
  }


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

  class SmartGroup extends Group {

    Rotate r;
    Transform t = new Rotate();

    void rotateByX(int ang) {
      r = new Rotate(ang, Rotate.X_AXIS);
      t = t.createConcatenation(r);
      this.getTransforms().clear();
      this.getTransforms().addAll(t);
    }

    void rotateByY(int ang) {
      r = new Rotate(ang, Rotate.Y_AXIS);
      t = t.createConcatenation(r);
      this.getTransforms().clear();
      this.getTransforms().addAll(t);
    }
  }
}

 

Visit JavaFX 3D Course Index Page

Comments

48 responses to “JavaFX 3D Tutorial #3 – 3D Object Transform (Rotation) with Keyboard Input”

  1. … [Trackback]

    […] Read More on to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  2. … [Trackback]

    […] Here you can find 62640 more Information on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  3. … [Trackback]

    […] There you will find 7244 additional Information to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  4. … [Trackback]

    […] Find More on to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  5. … [Trackback]

    […] Read More Information here to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  6. … [Trackback]

    […] Here you will find 88659 additional Information to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  7. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  8. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  9. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  10. … [Trackback]

    […] There you can find 13817 additional Information to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  11. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  12. … [Trackback]

    […] Here you can find 57495 more Information on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  13. … [Trackback]

    […] Info on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  14. … [Trackback]

    […] Read More on on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  15. … [Trackback]

    […] Find More Info here on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  16. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  17. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  18. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  19. … [Trackback]

    […] Here you will find 27402 more Information on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  20. … [Trackback]

    […] Find More Info here on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  21. … [Trackback]

    […] Read More on on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  22. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  23. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  24. … [Trackback]

    […] Find More here to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  25. … [Trackback]

    […] Read More here on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  26. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  27. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  28. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  29. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  30. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  31. … [Trackback]

    […] Find More to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  32. … [Trackback]

    […] Here you can find 72194 additional Information to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  33. … [Trackback]

    […] Read More on to that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  34. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  35. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/javafx-3d-transform-objects-with-keyboard-input/ […]

  36. buy enclomiphene cod next day fed ex

    enclomiphene canadian healthcare pharmacy

  37. kamagra sans rx

    kamagra sans ordonnance canada

  38. androxal no dr

    buy androxal generic online canada

  39. ordering flexeril cyclobenzaprine generic canada

    order flexeril cyclobenzaprine cheap from usa

  40. cheapest buy dutasteride canada shipping

    how to buy dutasteride buy singapore

  41. online order gabapentin generic new zealand

    gabapentin non perscription

  42. buy fildena generic dosage

    cheapest buy fildena online mastercard accepted

  43. ordering itraconazole generic in canada

    how to order itraconazole without prescriptions canada

  44. how to buy staxyn generic canadian

    cheap staxyn generic alternative

  45. order avodart buy in australia

    order avodart no prescription mastercard

  46. where to buy xifaxan no prescription no fees

    ordering xifaxan us prices

  47. purchase rifaximin generic drug india

    cheap rifaximin canada with no prescription

  48. renomované kanadské lékárny pro kamagra

    kamagra žádný skript

Leave a Reply