[JME3] Retrieve mouse position when clicking --Solved

Hello,



I’m able to catch events from mouse clicks via the methods :

[java]

inputManager.addMapping(“LClick”, new MouseButtonTrigger(0)); // Left-button click

inputManager.addMapping(“RClick”, new MouseButtonTrigger(1)); // Right-button click

inputManager.addMapping(“CClick”, new MouseButtonTrigger(2)); // Center-button click

[/java]



The event triggers the function :

[java]

@Override

public void onAction( String mappedAction, boolean isClicking, float tpf )

{

if( ! initialized )

return;



if( mappedAction.equals( “LClick” ) )

manageLClickAction( mappedAction, isClicking, tpf );



else if( mappedAction.equals( “RClick” ) )

manageRClickAction( mappedAction, isClicking, tpf );



else if( mappedAction.equals( “CClick” ) )

manageCClickAction( mappedAction, isClicking, tpf );

}

[/java]



Until there, no problem.



Now, I would like to know where the player clicked on the screen. How do I do that?



There is a mouseInput field inside the SimpleApplication class, but I don’t know how to use it.



Thanks in advance.

[java] Vector3f location = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0);[/java]

Hello,

Thanks for the answer.



The Vector3f ‘location’ gives me the point where my mouse clicked ( just in front of the screen )



When I used the center of the screen for the cursor, I used this method to know where I really clicked :

[java]

Vector3f location = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0);

Vector3f direction = …

CollisionResults results = new CollisionResults();

Ray ray = new Ray( location , direction );



myNode.collideWith( ray, results );

if( results.size() == 0 )

return false;



// Create a little animation displaying were we clicked

CollisionResult collision = results.getClosestCollision();

Vector3f position = collision.getContactPoint();

[/java]



For doing the same thing with the new vector ‘location’, I suppose I need to calc a new direction :

[java]

CollisionResults results = new CollisionResults();

Ray ray = new Ray(cam.getLocation(), cam.getDirection());



myNode.collideWith( ray, results );

if( results.size() == 0 )

return false;



// Create a little animation displaying were we clicked

CollisionResult collision = results.getClosestCollision();

Vector3f position = collision.getContactPoint();

[/java]



Is there a simple way to calc this var ‘direction’ or do I need to retrieve my old Math Manuals ? :slight_smile:

See the TestMousePick test class

Huhu!



I saw this class but I thought it was the HelloPicking class form the wiki. :wink:



This part of code is exactly what I needed :

[java]

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

Vector3f direction = cam.getWorldCoordinates(inputManager.getCursorPosition(), 0.3f);

direction.subtractLocal(origin).normalizeLocal();

[/java]



Thanks!