addObject and rayCast Bug

So it seems that using rayCast() after an object has been added in the same frame will cause the ray to hit the added object regardless of its position. I’ve attached a .blend that demonstrates this.

I’ve tested it in 2.62 and 2.63. Any idea why this is happening?

RayTest.blend (461 KB)

Really? Doesn’t happen in my game.
Seems to me that you are adding the object “B” at “B”'s location. So a ray would indeed hit B.

sdfgeoff: If you look at the blend file, you’ll see that I limited the distance of the rays to 1 unit. A and B are 4 units apart, so both rays shouldn’t hit anything.

Well, so I sorta found out what was confusing me.

Firstly, there’s rayCast() and rayCastTo():
A.rayCastTo(B) casts a ray from A to B and will ignore A’s geometry. Simple enough.

rayCast() on the other hand, is a bit more complicated. A.rayCast(B, C) casts a ray from C to B and will ignore object A. This can cause some interesting behavior. A.rayCast(B, A) will hit B without a problem (assuming nothing else it between A and B). However, A.rayCast(A, B) will result in B being hit even though it was the source of the ray. C.rayCastTo(B, A) will also result in the ray fired from A hitting itself. The exception to this would be if the ray fired from A did not intersect its mesh (this might happen if A’s center was outside of the mesh or if there is a hole in it).

All of that aside, my original problem still stands since I was using rayCastTo(). As far as I can tell, scene.addObject(‘B’, B) adds a new object B to the scene, but it isn’t actually moved to B’s position until the next frame. Instead, the object is added on top of the object that made the call to addObject() (or possibly the origin), in this case object A. This causes the ray cast from A to hit the newly created object B regardless of where the ray is cast.

My suspicions were pretty much confirmed when I changed the distance to fire the ray from 1 to 0.99. This prevented the ray from hitting the new B as the distance from A’s center to its edge is 1 unit. I also tried casting the second ray from B to A. When the distance for this ray exceeded 4 units, the ray hit the new object B.

I hope I’m not making wild conjectures here. Please correct me if I’m wrong.

EDIT: Just did a little more testing and confirmed that scene.addObject(‘obj’, A) adds the new object at the origin (0,0,0) before moving it to A’s location. I guess this means I’ve solved my issue. Hope this will help people out.