# Python, measuring distance using ray_cast

I have one problem I need help with.

I'm trying to measure a distance between the origin point and hit point using ray_cast. Is it possible or do I have to use another tool?

ray_cast loc[2] always return 1.0 or -1.0

cb = bpy.data.objects['Cube']
src = bpy.data.objects['src']
dst = bpy.data.objects['dst']

mw = cb.matrix_world
mwi = mw.inverted()

origin = mwi * src.matrix_world.translation
dest = mwi * dst.matrix_world.translation
direction = (dest - origin).normalized()

hit, loc, norm, face = cb.ray_cast(origin, direction)


• Please add your code. Suspect the location is the location of hit. You want to subtract origin from it to get the distance – batFINGER Apr 5 '19 at 14:43

The location of hit loc is the local coordinate on the cube face that the ray hits.

Its local distance from origin

(origin - loc).length


its global distance from origin

(mw * loc - src.matrix_world.translation).length


With no rotation or scale applied all local coordinates on default cube are either 1 or -1. The top and bottom faces are the planes z = 1 and z = -1 respectively.

• It works but I need to measure the length that ray "flown" before it hit the object. I'm planning to use it to find the highest and lowest point if terrain mesh with objects on it. – Ján Hanes Apr 7 '19 at 21:40