To demonstrate the problem, I have modified the "Detecting objects under cursor" tutorial. I made two changes to the tutorial code.
If you want to try it for yourself, then change the code in the Init method so it creates 10 boxes like this.
Code:
REPA(obj)
obj[i].create(Box(RandomF(0.1f, 0.5f),Random(Box(10, 1, 10)))).group(GROUP_OBJ);
Then add the following code to the end of the draw() method.
Code:
for(Flt x = -5; x < 5; x += 0.1f)
for(Flt z = -5; z < 5; z += 0.1f)
{
if(Physics.ray(Vec( x, 5.0f, z), Vec(x, -5.0f, z), &phys_hit, IndexToFlag(GROUP_OBJ)))
D.dot(RED, Vec(x, -1.5f, z));
}
Video 1 displays the first problem using the code above.
Video 1
As you can see, the grid of rays stop hiting the objects as they fall to the ground, even though the rays are passing right through the objects. Compare the ray start and end position with the object positions and sizes. What is the problem here? Why do the rays stop hitting the objects???
Now if we reverse the rays like this (note the start and end of the rays are reversed).
Code:
for(Flt x = -5; x < 5; x += 0.1f)
for(Flt z = -5; z < 5; z += 0.1f)
{
if(Physics.ray(Vec(x, -5.0f, z), Vec(x, 5.0f, z), &phys_hit, IndexToFlag(GROUP_OBJ)))
D.dot(RED, Vec(x, -1.5f, z));
}
Video 2 displays the second problem using the changed code above.
Video 2
As you can see, the objects are now being hit by the rays all the time. I don't understand why reversing the direction of the rays should make any difference. However, this better shows the second problem (which happens no matter which way the rays are directed). Notice how the red dots (which is where a ray has "hit" an object) do not line up with any of the boxes. What is going on here?
If I draw the actual hit position (i.e. phys_hit.plane.pos) then the dots appear in the correct places. Why doesn't phys_hit.plane.pos.xz() == Vec2(x,z)??? In fact phys_hit.plane.pos.xz is always exactly double Vec2(x,z). Why is this? For example, if you fire a ray from (2,5,4) to (2,-5,4) and it hits an object at 0.0f on the ground plane... the resulting phys_hit.plane.pos = (4,0,8)?!?!?! How can I fire a ray through the scene in one place and have the result return back a location exactly 2x the distance away from where the ray passed through the scene??? Is there a bug here?
P.S. add this to the update method if you need to spin around the scene to view it better.
Code:
CamHandle(0.1f, 10, CAMH_ZOOM|(Ms.b(1)?CAMH_MOVE:CAMH_ROT));