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.

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.

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.

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