On 28/01/2015 at 04:44, xxxxxxxx wrote:
Hi
I'm in the middle of writing some code to calculate the ray reflections around tight corners inside a geometric object. Sometimes the reflected ray bounces outside the object because it misses a close adjoining corner. Anyway, do do this I need to manually test along the path of the reflected ray to see if it meets a new collision. The code I'm using below is working toward this - but there's a general inaccuracy of the final position that ReflectRay calculates and the same position I calculate by taking the collision point and extrapolating along it.
I'm pretty sure my math and code are correct, but my value (mypos) and ReflectRay's value (pos) are very slightly different and it's bugging me - for example,
Vector(-95.939, -75.666, -19.324) Vector(-97.445, -75.666, -19.324)
global pos, vel
predictedPos = pos + vel
sourceobj = doc.SearchObject("Null") # The object the ray will start from
targetobj = doc.SearchObject("Cube") # The object the ray collides with
ray = GeRayCollider() # Create a new GeRayCollider object
ray.Init(targetobj, True) # Assign the object to a variable
start = pos
direction = vel.GetNormalized() #The direction the ray points.
raylength = 1000
CollisionState = ray.Intersect(start, direction, raylength)
if CollisionState:
hitpos = ray.GetIntersection(0)["hitpos"]
collDist = c4d.Vector(hitpos - pos).GetLength()
norm = ray.GetIntersection(0)["f_normal"]
distance = ray.GetIntersection(0)["distance"]
predDist = c4d.Vector(predictedPos - pos).GetLength()
if collDist < predDist:
norm.Normalize()
reflect = ReflectRay(vel,norm)
mypos = hitpos + ( reflect.GetNormalized() * (vel.GetLength() - distance) )
vel = reflect
pos = pos + vel
print pos, mypos
else:
pos = pos + vel
else:
pos = pos + vel