Potential bug with Ray

I started to add some code to my game to determine if an NPC has line of sight to the player. I’m using raycasting against my scene node to see if any walls obstruct the view.

[java]CollisionResults results = new CollisionResults();
Vector3f loc = spatial.getWorldTranslation().add(0, 1.8f, 0);
Vector3f testLoc = testSpatial.getWorldTranslation().add(0, 1.25f, 0);
Vector3f dir = testLoc.subtract(loc).normalizeLocal();
Ray ray = new Ray(loc, dir);
ray.setLimit(loc.distance(testLoc));
world.scene().getSceneNode().collideWith(ray, results);[/java]

i however was getting collison results when there was very clearly nothing in the way, however after adding this check to my collison results checking…

[java]
if (collision.getDistance() < ray.getLimit()) {
// … this collision is between the npc and player
}
[/java]

then it behaved as i expected it should. (i suspect the ray was still going to infinity, and hitting walls behind the player)

looking inside of the Ray class I dont see the limit field really used anywhere besides set/getRayLimit. It looks like someone started to add this code then forgot they were working on it…

The skybox may be in the way, it already happened before.
The skybox is a sphere in the middle of your scene, it’s just rendered far away with a trick, but it’s still there in the scene graph. Either you move it away, or you scale it a lot and it should fix your issue.

my “scene node” doesnt have a skybox, its JUST the scene node (eg, the node loaded from loading MyScene.blend). the only thing in “scene node” to collide with is the ground, and a couple of simple cubes that represent walls and houses.

next time i get some free time this week I can put together an example case using my scene and a simple setup to illustrate whats going on a little better (unless someone comments about the Ray first)

@icamefromspace said: I started to add some code to my game to determine if an NPC has line of sight to the player. I'm using raycasting against my scene node to see if any walls obstruct the view.

[java]CollisionResults results = new CollisionResults();
Vector3f loc = spatial.getWorldTranslation().add(0, 1.8f, 0);
Vector3f testLoc = testSpatial.getWorldTranslation().add(0, 1.25f, 0);
Vector3f dir = testLoc.subtract(loc).normalizeLocal();
Ray ray = new Ray(loc, dir);
ray.setLimit(loc.distance(testLoc));
world.scene().getSceneNode().collideWith(ray, results);[/java]

i however was getting collison results when there was very clearly nothing in the way, however after adding this check to my collison results checking…

[java]
if (collision.getDistance() < ray.getLimit()) {
// … this collision is between the npc and player
}
[/java]

then it behaved as i expected it should. (i suspect the ray was still going to infinity, and hitting walls behind the player)

looking inside of the Ray class I dont see the limit field really used anywhere besides set/getRayLimit. It looks like someone started to add this code then forgot they were working on it…

Ray doesn’t use the limit but the collision code does.
http://code.google.com/p/jmonkeyengine/source/browse/trunk/engine/src/core/com/jme3/collision/bih/BIHTree.java#417

Print out which object you are colliding with incorrectly and the other intersection data … that may clarify some things.

i made a simple test case here: http://www.mediafire.com/download/9t2q03yyy8g9xbv/RayTest.zip

the “bug” seems to not be what i thought it was. I expected for one of the perimeter walls to always be in the collision results (under my assumption that the ray was ignoring its limit).

there are however still weird results, mainly when standing right near the edge of a wall, you’ll get two collisions, one that is inside of the ray’s limit and one on the outside of the ray’s limit. I understand getting two collisions with the same object if the ray goes through it. but when standing right on the side of a wall/house (where youd think thered be only one collision if any) you get 2 collisions and one of them is beyond the ray’s limit.

though this doesnt explain the issue im having in my game, as theres always atleast one collision thats inside of the ray’s limit.

either way, is this sort of output supposed to happen? just the fact that any results show up beyond the rays limit suggests to me its not working as it should and there could be other issues involving the ray’s limit. I plan to investigate this again in my actual game later tonight. but this test is here for now…

i made a video of this bug in my actual game. the above test case i made is using the same raycasting code as my game, just the scene its in is slightly different. i just added the debug output text to my game to show what the ray is getting results against.

what youre seeing in that video:

the purple “outer walls” are named maze walls, the inner purple house thing is “maze shelter”. In the top left of the screen i print the same information as from my test case (all the collision results, and the distance at which the collision occured). everything else in the scene outside the maze is also all named appropriate things but dont come in to play.

the orange lines around sinbad are his “viewcone” raytesting is only done if youre inside the viewcone, the black line connecting sinbad and the ninja means a raytest was done on that frame.

sinbad’s head is labeled if he can see the player or not (will either display “i can see you” or “…”)

the only difference i can see between my test scene and my game scene is that the maze in the game scene are one big mesh/geom. which is perhaps invoking this weird “show invalid raytest results when inside a geoms bounding box”

If i change my code back to ignoring ray collison results that are beyond the ray’s limit, everything behaves as it should.

also, apparently Xeratos ran in to this issue a while ago:

http://hub.jmonkeyengine.org/forum/topic/how-are-rays-working/

I’d like to help fix the bug but im not sure where to start with the actual collision testing code, or what this might impact since it seems to have behaved like this for a while.