Ray_cast() not working at blocked location

I am using ray_cast() to detect blocked points in 3D, but the function fails at some points. Here are three images of an object in three poses.

Image 1: This is the mapping of 3D points onto the object. (Each 3D point is assigned a unique RGB value.) In this view, all points should be shown as in Image 2 but ray_cast() finds some being blocked.
pose_0

Image 2: I got this image by modifying dist as in hit = obj.ray_cast(cam_local, direction, distance=dist) in code below to dist-0.1.
pose_0_dist

Image 3: When pose is transformed (in this image, the bed is rotated 90 degrees along x-axis), some points (such as yellow and red ones) should be found blocked (i.e. hit[0] == True), but they remain in the scene.
pose_1

Here is my code using ray_cast():

dist = np.linalg.norm(point_3d - cam_in_model_space)
direction = ((point_3d - cam_in_model_space) / np.linalg.norm(point_3d - cam_in_model_space))
hit = obj.ray_cast(cam_local, direction, distance=dist)

If hit[0] == True, the point will not be shown in the scene.

Can anyone please give some clue to fix this?

I’m curious about this as well, I’ve done some raycasting and found it to be somewhat unreliable as well though I always chalked it up to clipping plane weirdness. I dig the experiment you have set up, makes visualizing the problem really easy. care to share the script and .blend so more people can poke around with it and see if there’s some specific cause?

Sure. I have created a GitHub repo and you can pull part of my test from link.

There is a bug in matrix_world which should be changed with model rotation. I debugged the transformation error and changed dist to dist-0.01 and now it works ideally. Sorry for bothering.

1 Like

Hello Paragon

I’m fighting a similar bug while using bvh raycasting in Blender. You note that you solved the problem by making a correction to a bug in matrix_world. Would you mind sharing how you solved the problem? and/or a code sample?

Hi there. This is a bit old and I hope I remember it correct. My issue was very specific that I multiplies matrix_world with new rotation matrix. It seems that the model space was changed relative to world matrix, so in my case, a rotation is applied to both mesh and matrix.

thanks for the quick reply! If you recall, did you post the corrected version of the code at the github repo above? I wasn’t sure, looking at the notes here and the commit history on the github.

If so, I can just inspect there and see if I can adapt to my situation.

Thx
Z

No, it was a toy case. As far as I can recall, if applying another rotation to object and origin points as I mentioned does not solve your issue, you may have a different issue. So double check the locations of origins and direction of rays relative to rotation of object, or you can visualize the scene after each transformation as I did when debugging.

OK, thanks for all the tips! Your colored dots visualization method is very clever!

No problem. Good luck :slight_smile: