Rotation according to the mouse position?!

Hey, I want to rotate an object according to the mouseposition, so that it “looks” in the direction of the mouse. I want to do this by using simple vector math but I have problems to get the mousecoordinates as a vector. I found this line on the forum:

[java]

cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f)[/java]



The return values i receive seem to be too little though. I tried this to test the values:

[java]

box.setLocalTranslation(cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.0f));[/java]



I expected the box to be on the mousecoordinates.

Those coordinates are the position of the cursor on the screen.

If you do the mouse picking tutorial, you’ll see how you can apply it to a 3d position.

You should be able to modify the “direction” vector according to your needs.

Hm, I forgot to say that I don’t need a 3D position because everything happens in a upper view and I actually don’t want to pick something. I want to click on a random position on the screen and the object should rotate in the clicked direction. So the mousecoordinates as a vector would be very helpful, but maybe there’s another way?!



greetings

“Upper view” as in top-down view?

In any case, the view port coords do not correlate to a 3d world position, so you can’t just translate it to affect an object in the 3d world without first knowing where the mouse is (in 3d space). So you have to shoot a ray straight down into the same plane the object is at, and get the relative vector3f from that, then rotate the box to face the point you clicked at.



I’d still say the solution is in Hello Mouse Picking.

Yes, top-down view is what I meant :slight_smile: . I’ll give it a try, but in what direction do I have to shoot the ray? I don’t want the camera to be rotatet, kind of like in a strategy game. (Age of Empires I + II for example)



EDIT: Is it possible to get the screen coordinates of my physics node? That would be very helpful!

Camera.getScreenCoordinates( worldCoords )

1 Like