0

I have been having this issue for a long time, and no matter what answers on here I try I keep having an issue.

This is a previous question of mine that had no resolution to it JavaFX 3D PerspectiveCamera affects drag position of a node

(nor did the answers/links provided in the question)

Essentially I am trying to drag a node while keeping the mouse position at to the clicked position of the node while dragging.

The original thought was to do an event.getScreenX() or event.getSceneX() to get the initial position on mouseClicked, and then compare/update in the mouse dragged.

The issue is that when I zoom the camera in and out(camera.setTranslateZ()), for some reason the values will increase/decrease depending on the zoom, i.e., the node drags slower/stays with the mouse when the camera is zoomed out.

For what it's worth I also have scaled the main node by 10, which I think might have something to do with this as one of the examples did seem to break when the scale and/or camera were changed; however the example also doesn't work, with no scale.

Does anyone have any idea? It's extremely frustrating with how simple this task is, yet hard to actually accomplish. I would think that as the mouse would drag, regardless if it dragged 1 pixel with the mouse zoomed in, or 100 pixels with the mouse zoomed out that it wouldn't cause this issue, so I'm wondering if there is some sort of bug with this? Any thoughts are appreciated, thank you.

public class Move extends Application {

double x0,xDiff;
double y0,yDiff;
@Override
public void start(Stage primaryStage) {

    Box b = new Box(100,100,1);
    b.setLayoutX(0);
    b.setLayoutY(0);
//    b.setTranslateZ(20000);

    Pane root = new Pane();
    root.getChildren().add(b);

    PhongMaterial p = new PhongMaterial();
    p.setDiffuseColor(Color.RED);
    b.setMaterial(p);

    Scene scene = new Scene(root, 2000, 1250,true);
    PerspectiveCamera camera = new PerspectiveCamera(true);
    camera.setTranslateZ(-1000);
    camera.setFarClip(2000);
    scene.setCamera(camera);
    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();

        b.setOnMousePressed(event
     ->{
            x0 = event.getSceneX();
            y0 = event.getSceneY();
            event.consume();
        });

        b.setOnMouseDragged(event
    ->{



        xDiff = event.getSceneX() - x0;
        yDiff = event.getSceneY() - y0;
        b.setLayoutX(b.getLayoutX() + xDiff);
        b.setLayoutY(b.getLayoutY() + yDiff);

        x0 = event.getSceneX();
        y0 = event.getSceneY();

});



primaryStage.setOnScroll(event
     ->{
  if (event.getDeltaY() > 0)
                 {
                    camera.setTranslateZ(camera.getTranslateZ() + 45);

                 }

                  else 
                 {

                    camera.setTranslateZ(camera.getTranslateZ() - 45);
                 }  
 });
}
    /**
     * @param args the command line arguments
     */
public static void main(String[] args) {
    launch(args);
}

}
José Pereda
  • 44,311
  • 7
  • 104
  • 132
MXBuster
  • 43
  • 7
  • If you want to drag a 3D node in a `Scene`, this is the answer you are looking for: [JavaFX Moving 3D objects with mouse on a virtual plane ](https://stackoverflow.com/a/28746439/3956070). It doesn't work for `SubScene` though. Note that the problem is far from "how simple this task is". – José Pereda May 28 '19 at 11:55
  • @JoséPereda thank you, you seem to be the only person who has been helping wit this issue. I had tried that example and it didn't seem to work for my needs. I am using a SubScene, why does that not work the same as a scene? I would think that dragging would be very simple in 3D, but why is there an issue? Why is there an issue when I get the distance the mouse has moved from 1 point to another and then it not translate like that in the scene? I would think that would be simple. Is there a reason why the object doesn't move with the mouse by getting the move distance? – MXBuster May 28 '19 at 16:40
  • @JoséPereda I figured that the perspective camera is why this issue is occurring, do you have any idea? I was also thinking of using the camera distance in a formula in order to solve the issue with the camera going back and forth, but this does seem to be a very annoying issue, but it seems more like a bug than not. It still makes no sense why dragging is so much more difficult when dragging a 3D object on a 2D plane compared to 2D on 2D. Thank you very much for your time. I will look into that post again to see if I can find anything. If not any other thoughts you might have? Thank you – MXBuster May 28 '19 at 16:42
  • The scene works fine, and it is based on math calculations involving ray picking, the camera, the distance to the object, and the 2D coordinates of the mouse. This if far from trivial, because you want to move a 3D object in 3D coordinates, with just the 2D mouse reference. In the process there is a complex 2D -> 3D mapping. With a SubScene, things get worse, because you need also to transform between scene -> subScene. I've really spent quite some time on trying to figure out a solution for this, to no avail _yet_. – José Pereda May 28 '19 at 20:13
  • @JoséPereda I figured there would be heavy calculations involved behind the scenes, but I figured there would be some built in move for 3D objects. Technically if I just do Node.getX() + event.getX() and it will keep the mouse positioned at the center every time. Initially my thought was to calculate the distance from the center to the event.getX, but there's an odd change (mouse is center of first node) which is causing a lot more difficulty. I greatly appreciate your kindness in spending time trying to help me. The camera Z moving is also an issue (unless I use event.getX). Thanks again! – MXBuster May 28 '19 at 20:43
  • @JoséPereda I'm assuming that you weren't able to figure anything out with this? I appreciate your time spent on this, thank you. I did find a work-around that fits my needs, but it would be interesting to know the math behind it. – MXBuster Jun 12 '19 at 02:02
  • This is a long standing issue: https://bugs.openjdk.java.net/browse/JDK-8091210 – user1803551 Apr 25 '20 at 16:03

0 Answers0