CollisionResult.getDistance() Behaviour problem

It appears that when I use the CollisionResult.getDistance() function, strange values may return in some cases.



The scenario is:

I have 2 nodes containing different objects:

-One got Targetable objects that I want to select

-One got the static map



Now I check if I got a hit with an object in the first node, if so, I check if anything on the second node is in the way (as the second node may grow way bigger, I prefer to save computations this way). My code looks like this:



[java]

public Targetable getMouseTarget(){

// Get mouse collisions

CollisionResults targetableResults = new CollisionResults();

Ray mouseRay = getMouseRay();

//TODO: Update for animated targetables

getTargetableNode().collideWith(mouseRay, targetableResults);



// Mouse clicked on a targetable?

if (targetableResults.size() > 0) {

CollisionResult closestTarget = targetableResults.getClosestCollision();



// Check for obstructing map geometry

CollisionResults mapResults = new CollisionResults();

getMapNode().collideWith(mouseRay, mapResults);



// Stupid distance calculation used instead of getDistance()

// function mentioned seemed to make mistakes, positions turned out perfect

if (mapResults.size() == 0 ||

mapResults.getClosestCollision().getContactPoint().distance(Main.app.getCamera().getLocation()) >= closestTarget.getDistance()){



// When using models, targatable has a node with the model which has the model itself, crappy solution

return (Targetable)closestTarget.getGeometry().getParent().getParent();

}

}



return null;

[/java]



the line of interest is this:

[java]mapResults.getClosestCollision().getContactPoint().distance(Main.app.getCamera().getLocation()) >= closestTarget.getDistance()[/java]

which should be:

[java]mapResults.getClosestCollision().getDistance() >= closestTarget.getDistance()[/java]



I can only guess about the reason for this but I can’t think of anything sensible. The position of the collision seems to be perfect, somehow the distance for the mapResult often came out as either a very high value (300+) or very small (0 to 1), while it was supposed to be about 20 to 30. The staticMap contains a heigthMap generated terrain, I guess it may have to do something with that but as the position of the collision is 100% right, why is the distance wrong? Using the code above I found a okay-ish workaround but it feels wrong.



Also, as you may read in the above comment, I need to update that snippet a bit to support animated models. Any tips for that? I couldn’t find a tutorial of the combination of picking and animation, I’ve implemented both in my project but I have not found an obvious solution for that yet. I suppose I need to do some kind of apply on the models and then shoot the ray, or am I totally off?



Kind regards!