0

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.

  • 1
    The answer [Restricting a 3D object mouse drag movement to a plane in JavaFX](https://stackoverflow.com/questions/29034080/restricting-a-3d-object-mouse-drag-movement-to-a-plane-in-javafx/29036299#29036299) has a better approach to do dragging on a given plane. – José Pereda Mar 05 '19 at 09:11
  • @JoséPereda thank you, but that example changes the mouse position in the box when it's first dragged. I don't have a restriction of planes. I'm looking to keep the mouse positioned in the same spot I clicked, when dragging a node, I am trying a different approach now. –  Mar 06 '19 at 18:54
  • Try setting the camera clip like: `camera.setNearClip(0.1); camera.setFarClip(100000.0);`. And use the latest Java 8 as possible. – José Pereda Mar 06 '19 at 18:59
  • @JoséPereda Far clip was 2000, I tried the near clip with no luck... I'll update to the latest update, but I dont' think that will help. I did get to the point of having the mouse reset to the cent of the node, which is better than nothing, but would like to get it exact. I believe that with 2D I had it working with something along the lines of LayoutX = getLayoutX() + (event.getX()-getMinX()); but that didn't work in 3D. My actualy code doesn't like the changes I made to the example which I wil update above.I'm assuming it's because I have multiple group of boxes grouped together to be moved. –  Mar 06 '19 at 19:25

0 Answers0