# How are rays working?

Hi everybody,
i have some issues with raycasting. In my programm I shoot rays from different points in the 6 main directions (0,0,1), (0,0,-1), …, (-1, 0, 0). I set their limit to 1. Given an origin and an obviously normalized direction. I still get results where getDistance() gives values > 1. The way I would interpret setLimit is that it would ignore all results that are farther away then direction * limit. But it seems like it is not.

Can anyone tell why that is? Or how its actually working?

Thx
Greetz Xeratos

You’d have to show us the code. Limit should limit the results… it always does for me.

Hmm ok consider this mesh. A 3x3 grid with distance 1: Now I shoot the rays from each of these points in 6 directions as mentioned above:

[java]
public static Vector3f[] rDirections = new Vector3f[] { new Vector3f(0, 0, 1),
new Vector3f(0, 0, -1),
new Vector3f(1, 0, 0),
new Vector3f(-1, 0, 0),
new Vector3f(0, 1, 0),
new Vector3f(0, -1, 0) }

public float extractVoxelData2(Geometry refGeom, Vector3f voxelPos) {
float[] densities0, densities1;
CollisionResults tmpResults = new CollisionResults();
CollisionResult r;

``````    Ray ray = new Ray();
ray.setLimit(1);
for (Vector3f rDirection : rDirections) {
tmpResults.clear();

ray.setOrigin(voxelPos);
ray.setDirection(rDirection);
refGeom.collideWith(ray, tmpResults);

for (int i = 0; i &lt; tmpResults.size(); i++) {
r = tmpResults.getCollision(i);
// dbl = false;
// if (results.size() &gt; 0)
// if (r.getContactPoint()
// .equals(results.getCollision(results.size() - 1).getContactPoint())
// &amp;&amp; r.getDistance() == results.getCollision(results.size() - 1)
// .getDistance()) {
// dbl = true;
// }
// if (r.getDistance() &lt;= 1 &amp;&amp; !dbl) {
// } else {
// // results.d
// }
}
}

if (results.size() &gt; 0) {
System.out.println("Results for " + voxelPos);
densities0 = new float[results.size()];
densities1 = new float[results.size()];
for (int i = 0; i &lt; results.size(); i++) {
r = results.getCollision(i);

densities0 = r.getDistance();
densities1 = r.getDistance();

System.out.println("Result " + i + ": contact " + r.getContactPoint()
+ ",\n          normal " + r.getContactNormal()
+ ",\n          distance " + r.getDistance()
+ ", triIndex " + r.getTriangleIndex()
+ ", tri distance "
+ r.getTriangle(null).getCenter().distance(voxelPos));
System.out.println();
}
System.out.println("----------------------------------");
}

return .5f;
``````

}
[/java]

With this code I get 8 results for the 6 points outside the mesh and 24 for the one inside the mesh.
I get why its 4 cause it hits 4 triangles … but the others ofcourse also 4 but with distance 1.5f which would be fine if I wouldnt set a limit.