How would one find a block that was clicked on?

I suppose you shoot a ray from the cam to somewhere and find the collision?

for jme3 (I assume), this example does it:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_picking

1 Like

Ugh how stupid of me… I forgot that the cam has a Direction property. On a different note, does the mouse have a location?

something along these lines:

[java]

//mouse position on screen (bottom left is 0,0, increases to top right)

Vector2f screenPos = inputManager.getCursorPosition();

//above mouse cursor’s position in 3D

Vector3f origin = cam.getWorldCoordinates(screenPos, 0.0f);

//A point deeper into the screen (positive zPos):

Vector3f pointOnNormal= cam.getWorldCoordinates(screenPos, 0.3f);

//the direction:

Vector3f direction=pointOnNormal.subtractLocal(origin).normalizeLocal();

//the Ray from those:

Ray ray = new Ray(origin, direction);[/java]

mostly when

[java]inputManager.setCursorVisible(true);[/java]

(by default is false)



EDIT: I wrongly named “pointOnNormal” like that, it’s not a point on a normal because that (Ray really) is not perpendicular on anything, though it can be when in the middle of the screen only

1 Like

Here is the method I use to pick with the mouse: http://hub.jmonkeyengine.org/groups/general-2/forum/topic/picking-with-the-mouse/



[java]Vector2f mouseCoords = new Vector2f(inputManager.getCursorPosition());

Ray ray = new Ray(cam.getWorldCoordinates(mouseCoords, 0),

cam.getWorldCoordinates(mouseCoords, 1).subtractLocal(

cam.getWorldCoordinates(mouseCoords, 0)).normalizeLocal());

CollisionResults results = new CollisionResults();

rootNode.collideWith(ray, results);

if(results.size()!=0){

//you picked something

}[/java]

I suggest putting the objects you want to pick in a separate node. If you have a skybox in rootNode it will always be in results in this example.

1 Like

Thanks that helps me alot.

1 Like