JavaFX 3D PerspectiveCamera affects drag position

2019-08-28 22:05发布

问题:

I have been having issues trying to move nodes in JavaFX within a 3D Scene. The issue is that I want the mouse to stay at the position within the node I've clicked, i.e., center. With a PerspectiveCamera it will alter the position. I've tried a couple of different methods and haven't had any luck. 2D Rectangles, and 3D Boxes(without a camera) work perfectly, but once a PerspectiveCamera is added, regardless of true/false parameter, I have issues.

I am wondering if this a bug that should be reported, or if there is some way to get another the perspective affecting the moving of nodes

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();

});
}     
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

}

I'm using Java 8 update 91 or 181 (it seems netbeans puts the default at 91, but I have 181 as well)

JavaFX Moving 3D objects with mouse on a virtual plane

I also found this post, and had tried the answer and it seemed like it also had some issues, but seemed much better(except it it was hard to test with the additional code of spinning the node when dragging, so if you drag off the sphere it rotates instead).

Thank you very much

EDIT: After trying to go back to how I originally dragged, I was able to get to a point where I could get the mouse cursor to stay in the middle, but I am trying to figure out how to get the exact position.

b.setLayoutX(b.getLayoutX() + (event.getX()));
b.setLayoutY(b.getLayoutY() + (event.getY()));

will give me the center of the node.

I originally used similar code to this for 2D, but was having issues with 3D, which I am assuming was due to the differences of 0,0 top-left corner, vs 0,0,0 in the center.

The code for 2D was something along the lines of

b.setLayoutX(b.getLayoutX() + (event.getX()-b.getMinX()));
b.setLayoutY(b.getLayoutY() + (event.getY()-b.getMinY()));

Essentially, from what I see when I set the original layout + the event position it just moves the center/top-left to the coordinates of the mouse event, so I would try to get the difference between the origin of the node and the mouseEvent, which is what event.getX() does, and try to figure out the difference to move, which is what the event.getSceneX() - x0 is for. I tried doing it without the Scene X/Y but it doesn't seem to work properly, but I'm not sure if using the SceneX/Y is what I should be doing.