How to check collision (mouse hover) with mouse on the static menu screen

I have a Node:

Node gearNode = new Node("GearNode") {{
    setLocalTranslation(0.965f, -0.525f, 0);
}};
rootNode.attachChild(gearNode);

And how I can check the mouse cursor on the screen is hover thar?.. -_-"

public void checkPointer(Vector2f mousePointer) {
        mouseDestination = new Vector3f(mousePointer.x, mousePointer.y, 0);
        float dist = gearNode.getWorldTranslation().distance(mouseDestination);
        log.info("Mouse moved: {}. Dist: {}", mousePointer, dist);
        isGearHovered = dist < ???; // ...
}

Mouse destination - is a coordinates from 0x0 to 1280x1080
But Gear coordinates on the screen is right bottom corner!

A node on its own doesn’t have any geometry or size… so it’s effectively not even there.

It has to actually have something.

…and then you should look at the HelloPicking tutorial.

Alternately, you can use Lemur’s picking which is more advanced than just raw picking and handles a bunch of things for you… but either way your scene will actually have to have stuff in it.

All right… I translate the gear to guiNode, now it works correct. I hope so.

I try to use Lemur, but got exception any time IndexOutOfBoundsException

05.10.24 13:43:51.704 [ERROR] th:jME3 Main c.j.a.LegacyApplication.handleError():666   Uncaught exception thrown in Thread[#58,jME3 Main,6,main]
java.lang.IndexOutOfBoundsException: null
	at java.base/java.nio.Buffer$1.apply(Buffer.java:757)
	at java.base/java.nio.Buffer$1.apply(Buffer.java:754)
	at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
	at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:106)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:302)
	at java.base/java.nio.Buffer.checkIndex(Buffer.java:768)
	at java.base/java.nio.DirectFloatBufferU.get(DirectFloatBufferU.java:342)
	at com.jme3.collision.bih.BIHTree.initTriList(BIHTree.java:82)
...

Any jMonkeyEngine picking will give you this problem because something in your scene has bad geometry that can’t have collision data.

…which I think I said in the other thread.

As mentioned there, you need to figure out which Geometry is failing and why it’s bad. Any JME picking will fail until you figure out which Geometry is failing.

Off-the-top-of-my-head pseudo code:

public static void dumpTree( String indent, Spatial spatial ) {
    System.out.println(indent + spatial);    
    if( spatial instanceof Node ) {
        for( Spatial child : ((Node)spatial).getChildren() ) {
            dumpTree("  " + indent, child);
        }
    } else if( spatial instanceof Geometry ) {
        ((Geometry)spatial).getMesh().createCollisionData();
    }
}

…then call that with whatever root is failing. Then when it crashes look at the console output to see what part of the tree failed.

3 Likes