JavaFX 3D Tutorial #4 – 3D Object Transform (Rotation) with Mouse

In the previous chapter, we saw how to do the transforms with keyboard input. In this chapter, we take it a step further. Today, we will talk about JavaFX 3D Mouse Control implementation. Giving control using mouse is more reliable than keyboard. With mouse control, user can directly interact with the 3D application.


The basic idea is to track the mouse drag. Based on how much drag is made in x and y-axis, we apply rotation transformation.

The following method will do the trick.

 //Tracks drag starting point for x and y
 private double anchorX, anchorY;
 //Keep track of current angle for x and y
 private double anchorAngleX = 0;
 private double anchorAngleY = 0;
 //We will update these after drag. Using JavaFX property to bind with object
 private final DoubleProperty angleX = new SimpleDoubleProperty(0);
 private final DoubleProperty angleY = new SimpleDoubleProperty(0);

 private void initMouseControl(SmartGroup group, Scene scene) {
    Rotate xRotate;
    Rotate yRotate;
    group.getTransforms().addAll(
        xRotate = new Rotate(0, Rotate.X_AXIS),
        yRotate = new Rotate(0, Rotate.Y_AXIS)
    );
    xRotate.angleProperty().bind(angleX);
    yRotate.angleProperty().bind(angleY);

    scene.setOnMousePressed(event -> {
      anchorX = event.getSceneX();
      anchorY = event.getSceneY();
      anchorAngleX = angleX.get();
      anchorAngleY = angleY.get();
    });

    scene.setOnMouseDragged(event -> {
      angleX.set(anchorAngleX - (anchorY - event.getSceneY()));
      angleY.set(anchorAngleY + anchorX - event.getSceneX());
    });
  }

Let’s discuss the code parts.

Step 1: Prepare and apply initial rotation transformation
//Prepare X and Y axis rotation transformation obejcts
Rotate xRotate;
Rotate yRotate;
//Add both transformation to the container
group.getTransforms().addAll(
	xRotate = new Rotate(0, Rotate.X_AXIS),
	yRotate = new Rotate(0, Rotate.Y_AXIS)
);
/*Bind Double property angleX/angleY with corresponding transformation.
  When we update angleX / angleY, the transform will also be auto updated.*/
xRotate.angleProperty().bind(angleX);
yRotate.angleProperty().bind(angleY);
Step 2: Track starting of mouse drag.

It is necessary to track the drag start. Because, we will have to track how much the drag was happened in both axes.
A drag always start with a mouse press.

//Listen for mouse press -- Drag start with a click :-)
scene.setOnMousePressed(event -> {
  //Save start points
  anchorX = event.getSceneX();
  anchorY = event.getSceneY();
  //Save current rotation angle
  anchorAngleX = angleX.get();
  anchorAngleY = angleY.get();
});
Step 3: Calculate angle of rotation based on current drag point

When the user start dragging, after each pixel (or minor dragging), we will get a callback. We will check current (x,y) value and compare it with previous to calculate the rotation delta.

//Listen for drag
scene.setOnMouseDragged(event -> {
    /*event.getSceneY() gives current Y value. Find how much far away
      it is from saved anchorY point.
     */
    angleX.set(anchorAngleX - (anchorY - event.getSceneY()));
    angleY.set(anchorAngleY + anchorX - event.getSceneX());
});

It is important to note that, when the user drags in Y axis, we have to rotate based on X axis and vice versa (Try the opposite and see how much weird it is). The negative symbol in angleX is to set the direction of rotation. You can change the direction by changing the subtraction to addition.

Visit JavaFX 3D Course Index Page

Find Code in GitHub

Comments

52 responses to “JavaFX 3D Tutorial #4 – 3D Object Transform (Rotation) with Mouse”

  1. … [Trackback]

    […] Here you can find 95571 additional Info on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  2. … [Trackback]

    […] Read More on to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  3. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  4. … [Trackback]

    […] Find More here to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  5. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  6. … [Trackback]

    […] Info on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  7. … [Trackback]

    […] Info on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  8. … [Trackback]

    […] There you will find 73513 more Info to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  9. … [Trackback]

    […] Here you will find 59827 additional Info on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  10. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  11. … [Trackback]

    […] Information to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  12. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  13. … [Trackback]

    […] There you can find 62364 additional Info to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  14. … [Trackback]

    […] There you can find 8512 additional Info on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  15. … [Trackback]

    […] There you will find 63145 additional Information to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  16. … [Trackback]

    […] Find More on to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  17. … [Trackback]

    […] Information to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  18. … [Trackback]

    […] Info on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  19. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  20. … [Trackback]

    […] Read More here on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  21. … [Trackback]

    […] Find More here to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  22. … [Trackback]

    […] Read More here on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  23. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  24. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  25. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  26. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  27. … [Trackback]

    […] Here you can find 85607 more Information on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  28. … [Trackback]

    […] Read More Info here on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  29. … [Trackback]

    […] Find More on on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  30. … [Trackback]

    […] Read More here on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  31. … [Trackback]

    […] Info on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  32. … [Trackback]

    […] Information on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  33. … [Trackback]

    […] Read More on to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  34. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  35. … [Trackback]

    […] There you will find 84695 additional Info to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  36. … [Trackback]

    […] Here you can find 64381 more Information on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  37. … [Trackback]

    […] Here you will find 8397 more Info to that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  38. … [Trackback]

    […] Read More Info here on that Topic: genuinecoder.com/javafx-3d-tutorial-object-transform-rotation-with-mouse/ […]

  39. online order enclomiphene purchase online uk

    discount enclomiphene usa where to buy

  40. comment obtenir une prescription kamagra

    kamagra expédié contre remboursement le samedi

  41. buying androxal cheap new zealand

    ordering androxal cheap alternatives

  42. get flexeril cyclobenzaprine cost on prescription

    online order flexeril cyclobenzaprine generic pricing

  43. get dutasteride online mastercard accepted

    buy dutasteride cheap now

  44. cheap gabapentin cost australia

    discount gabapentin price for prescription

  45. buy cheap fildena price prescription

    Cheapest fildena online

  46. discount itraconazole generic ireland

    how to order itraconazole united kingdom

  47. purchase staxyn cheap next day delivery

    order staxyn canada price

  48. cheap avodart generic europe

    buy cheap generic avodart

  49. buy rifaximin generic buy online

    delivered rifaximin

  50. xifaxan overnight without prescription

    purchase xifaxan generic effectiveness

  51. na dobírku online recepty kamagra

    kamagra online

Leave a Reply