I’m using the ray_cast() function form the Object class from the bpy library (thus not the game engine).
But it seems it does not work as expected.
See the attached pictures (note: they do not show up in the order I attached them).
pict1 shows a Plane at the origin and a ray_cast() call casting a beam from above onto the middle of the pane.
This works fine and the hit point is at the origin, as you can see in the terminal window. A second ray misses the Plane and thus gives no hit.
pict2 shows the same ray casts but the Plane is now moved to a location where the second ray cast should hit. But it doesn’t. The first ray cast detects a hit at the origin. But there is no Plane there!
pict3 shows the same situation as pict2 but now the rotation and translate of the Plane are ‘Applied’ . Thus the current location becomes it origin. Now the first does not hit and the second cast hits the Plane!
I assume this is not how it is intended to work.
How can I work around this? I do not want to apply the translation and rotation etc because I need to update the position with the script repeatedly and then the position will not be correct anymore (I do not want to do that incrementally).
It works the same as the ray_cast() form the object class.
But now in world coordinates in stead of local co-ordinates.
I convert the (world) parameters for the ray_cast() function to local coordinates and after the ray_cast() call I convert the result back to world coordinates. Thats all.
It is a generic function. Not ment for something specific.
You give the ray_cast function a ray defined by a start and end point. Then it will give you the point at which it hits the object.