# Picking Distance wrong?

I've implemented a function to find the point on an object after being clicked on by the mouse. I'm using TrianglePickResults and setCheckDistance(true).

Though, I'm a bit puzzled about the results.

The origin of the ray I used is: com.jme.math.Vector3f [X=0.0, Y=0.0, Z=25.0]

The Local Translation of the target is: com.jme.math.Vector3f [X=0.0, Y=0.0, Z=0.0] (and the object is a box with all extents below 1)

PickResult.getDistance() returns 49.05639 (about double of what I would expect).

Any idea why this happens?

is the origin of your ray the camera???

Check if the origin is the camera and go through the camera direction…

GuilhermeFranchi said:

is the origin of your ray the camera???
Check if the origin is the camera and go through the camera direction.... ;)

The origin is the camera, yes, but I don't want to go through the camera direction. After all, I pick with the mouse, not through a crosshair in the middle of the screen ;)

Try printing out the ray and the picked triangle as a sanity test.

I'll try that.

But the REAL point is that the distance between 0,0,25 and an object at 0,0,0 can't possibly be greater than 25, especially because the object is a very small box.

I'm sure the picking method works, because I did quite a few tests and it always returned the object I had clicked on. It's just the distance that seems to be wrong.

Right, so it would seem the data used to do the distance calc is wrong if something is indeed wrong.  I would put some printouts in TrianglePickData's getDistanceToTriangle method, specifically:

```        // Intersection test         Ray ray = getRay();         if ( ray.intersectWhere( worldTriangle[0], worldTriangle[1], worldTriangle[2], intersectionPoint ) ) {             System.err.println("ray's origin: "+ray.getOrigin()+"  intersects: "+intersectionPoint); // <--- ADDED LINE             return ray.getOrigin().distanceSquared( intersectionPoint );         }```

And see if the printed values make any sense.

I've changed the method to this:

```    private float getDistanceToTriangle( Vector3f[] triangle ) {         // Transform triangle to world space         for ( int i = 0; i < 3; i++ ) {             worldRotation.mult( triangle[i], worldTriangle[i] ).multLocal( worldScale ).addLocal( worldTranslation );         } System.out.println("triangle: " + triangle[0] + " " + triangle[1] + " " + triangle[2]);         // Intersection test         Ray ray = getRay();         if ( ray.intersectWhere( worldTriangle[0], worldTriangle[1], worldTriangle[2], intersectionPoint ) ) { System.out.println("ray's origin: "+ray.getOrigin()+"  intersects: "+intersectionPoint + " distanceSq: " + ray.getOrigin().distanceSquared( intersectionPoint ));             return ray.getOrigin().distanceSquared( intersectionPoint );         }         // Should not happen         return Float.MAX_VALUE;     }```

Which gives me the following output:

triangle: com.jme.math.Vector3f [X=3.0, Y=-0.1, Z=1.0] com.jme.math.Vector3f [X=-3.0, Y=-0.1, Z=1.0] com.jme.math.Vector3f [X=-3.0, Y=-0.1, Z=-1.0]
ray's origin: com.jme.math.Vector3f [X=0.0, Y=0.0, Z=25.0]  intersects: com.jme.math.Vector3f [X=-4.8185835, Y=-0.1661579, Z=-24.8474] distanceSq: 2508.0098
triangle: com.jme.math.Vector3f [X=-3.0, Y=0.1, Z=-1.0] com.jme.math.Vector3f [X=-3.0, Y=0.1, Z=1.0] com.jme.math.Vector3f [X=3.0, Y=0.1, Z=-1.0]
ray's origin: com.jme.math.Vector3f [X=0.0, Y=0.0, Z=25.0]  intersects: com.jme.math.Vector3f [X=-4.888929, Y=-0.1685836, Z=-25.575111] distanceSq: 2581.772
triangle: com.jme.math.Vector3f [X=3.0, Y=-0.1, Z=1.0] com.jme.math.Vector3f [X=-3.0, Y=-0.1, Z=1.0] com.jme.math.Vector3f [X=-3.0, Y=-0.1, Z=-1.0]
ray's origin: com.jme.math.Vector3f [X=0.0, Y=0.0, Z=25.0]  intersects: com.jme.math.Vector3f [X=-4.8185835, Y=-0.1661579, Z=-24.8474] distanceSq: 2508.0098
triangle: com.jme.math.Vector3f [X=-3.0, Y=0.1, Z=-1.0] com.jme.math.Vector3f [X=-3.0, Y=0.1, Z=1.0] com.jme.math.Vector3f [X=3.0, Y=0.1, Z=-1.0]
ray's origin: com.jme.math.Vector3f [X=0.0, Y=0.0, Z=25.0]  intersects: com.jme.math.Vector3f [X=-4.888929, Y=-0.1685836, Z=-25.575111] distanceSq: 2581.772

Now, looking at one pair of outputs...

triangle: com.jme.math.Vector3f [X=3.0, Y=-0.1, Z=1.0] com.jme.math.Vector3f [X=-3.0, Y=-0.1, Z=1.0] com.jme.math.Vector3f [X=-3.0, Y=-0.1, Z=-1.0]
ray's origin: com.jme.math.Vector3f [X=0.0, Y=0.0, Z=25.0]  intersects: com.jme.math.Vector3f [X=-4.8185835, Y=-0.1661579, Z=-24.8474] distanceSq: 2508.0098

How can the triangle (3/-0.1/1) (-3/-0.1/1) (-3/-0.1/-1) ever intersect a ray at (-4.8/-0.2/-24.8 )? If my mathematics knowledte isn't completely off, the intersection point isn't even in the triangle...

That's the ray's origin, but without the direction of the ray, it's impossible to tell you whether it's right or wrong

This is a simple suggestion but it cleared up an issue I had with picking. Make sure that you do an updateGeometricState call on the object your picking before testing for a pick. Don't know if this will help you, but it cleared up a bunch of warnings in the log file, and some strange behaviour that I had.

renanse:

Here's an output with source and direction of the ray:

triangle: com.jme.math.Vector3f [X=3.0, Y=-0.1, Z=-1.0] com.jme.math.Vector3f [X=3.0, Y=-0.1, Z=1.0] com.jme.math.Vector3f [X=-3.0, Y=-0.1, Z=-1.0]
ray: com.jme.math.Ray@19a8416  intersects: com.jme.math.Vector3f [X=5.636637, Y=-0.4206441, Z=-25.477325] distanceSq: 2579.909
triangle: com.jme.math.Vector3f [X=-3.0, Y=0.1, Z=1.0] com.jme.math.Vector3f [X=3.0, Y=0.1, Z=1.0] com.jme.math.Vector3f [X=3.0, Y=0.1, Z=-1.0]
ray: com.jme.math.Ray@19a8416  intersects: com.jme.math.Vector3f [X=5.7235165, Y=-0.42712763, Z=-26.255348] distanceSq: 2660.0518
triangle: com.jme.math.Vector3f [X=3.0, Y=-0.1, Z=-1.0] com.jme.math.Vector3f [X=3.0, Y=-0.1, Z=1.0] com.jme.math.Vector3f [X=-3.0, Y=-0.1, Z=-1.0]
ray: com.jme.math.Ray@155d3a3  intersects: com.jme.math.Vector3f [X=5.636637, Y=-0.4206441, Z=-25.477325] distanceSq: 2579.909
triangle: com.jme.math.Vector3f [X=-3.0, Y=0.1, Z=1.0] com.jme.math.Vector3f [X=3.0, Y=0.1, Z=1.0] com.jme.math.Vector3f [X=3.0, Y=0.1, Z=-1.0]
ray: com.jme.math.Ray@155d3a3  intersects: com.jme.math.Vector3f [X=5.7235165, Y=-0.42712763, Z=-26.255348] distanceSq: 2660.0518

Still, I think that the intersection point isn't anywhere near the triangle, mainly because the Z value of the intersection is too 'high' (in the negatives)

nymon:
I just tried calling updateGeometricState(0, true) on the scene, which didn't help. Should I update all objects in the scene, one by one, or should updating the scene be enough?

unfortunately you're only printing out "ray: com.jme.math.Ray@19a8416" for each ray.

I'm not sure exactly how the update propogates throught the scene. I was calling it directly on an element as I was ray casting downward to keep the character above ground. But I assume if you updated a parent node in the scene like you did, the children should be taken care of as well.

renanse said:
unfortunately you're only printing out "ray: com.jme.math.Ray@19a8416" for each ray.  :)

I'm just too stupid...
I noticed that in my first try, changed the code, and didn't recompile. Then, I never bothered checking wether the change did what I expected. DOH!

Now here's the correct output:
triangle: com.jme.math.Vector3f [X=3.0, Y=-0.1, Z=-1.0] com.jme.math.Vector3f [X=3.0, Y=-0.1, Z=1.0] com.jme.math.Vector3f [X=-3.0, Y=-0.1, Z=-1.0]
ray: (com.jme.math.Vector3f [X=0.0, Y=0.0, Z=25.0]/com.jme.math.Vector3f [X=0.068333335, Y=-0.0016666657, Z=-1.0])  intersects: com.jme.math.Vector3f [X=3.398199, Y=-0.08288285, Z=-24.72974] distanceSq: 2484.6016
triangle: com.jme.math.Vector3f [X=-3.0, Y=0.1, Z=1.0] com.jme.math.Vector3f [X=3.0, Y=0.1, Z=1.0] com.jme.math.Vector3f [X=3.0, Y=0.1, Z=-1.0]
ray: (com.jme.math.Vector3f [X=0.0, Y=0.0, Z=25.0]/com.jme.math.Vector3f [X=0.068333335, Y=-0.0016666657, Z=-1.0])  intersects: com.jme.math.Vector3f [X=3.4281867, Y=-0.08361425, Z=-25.168583] distanceSq: 2528.6462
triangle: com.jme.math.Vector3f [X=3.0, Y=-0.1, Z=-1.0] com.jme.math.Vector3f [X=3.0, Y=-0.1, Z=1.0] com.jme.math.Vector3f [X=-3.0, Y=-0.1, Z=-1.0]
ray: (com.jme.math.Vector3f [X=0.0, Y=0.0, Z=25.0]/com.jme.math.Vector3f [X=0.068333335, Y=-0.0016666657, Z=-1.0])  intersects: com.jme.math.Vector3f [X=3.398199, Y=-0.08288285, Z=-24.72974] distanceSq: 2484.6016
triangle: com.jme.math.Vector3f [X=-3.0, Y=0.1, Z=1.0] com.jme.math.Vector3f [X=3.0, Y=0.1, Z=1.0] com.jme.math.Vector3f [X=3.0, Y=0.1, Z=-1.0]
ray: (com.jme.math.Vector3f [X=0.0, Y=0.0, Z=25.0]/com.jme.math.Vector3f [X=0.068333335, Y=-0.0016666657, Z=-1.0])  intersects: com.jme.math.Vector3f [X=3.4281867, Y=-0.08361425, Z=-25.168583] distanceSq: 2528.6462

hmm, can you send me your test code?  (or post it here if you like)  This definitely looks fishy.

Sorry for the late answer, I had to be offline for 11 days because of technical difficulties.

I can send you my code if you supply me your mail address. I'd post it here, but it's split over quite some source (and data) files. Copy-n-paste would probably cause problems.

11 days…that's some serious technical difficulties.

my email is right

<---- here

darkfrog said:

11 days....that's some serious technical difficulties.