problem with GeRayCollider [Solved]

On 17/06/2015 at 10:40, xxxxxxxx wrote:

Hi all,

I've problems getting the GeRayCollider work correctly in my scene:

As a simple test I create a sphere as target object and position the sphere in (0,0,0). First GeRayCollider gives me correct hits on this sphere. Its also working when I move my source object which is my starting point for the "Intersect" method: the collision coordinates are moving accordingly.

But when I move the target object I still get the collision hits on the place where the sphere was located before. Do I have to call anything after moving the target geometry in order to update GeRayCollider ?

regards,
Jens

On 17/06/2015 at 12:52, xxxxxxxx wrote:

Hi Jens,

the GeRayCollider works in local space of the target object.
Transferring the start position to local space and the hitpoints back to global space should work.
Please give this script a try.
Hope this helps!
Best wishes
Martin

  
import c4d  
from c4d import utils  
############################################################################  
#    sample implementation of GeRayCollider                                #  
#    You´ll need a linear spline object with two points (called "LINE")    #  
#    and an object to test the script                                      #  
###########################-by-monkeytack-##################################  
  
  
  
#############################################  
def Triangulate(op) :  
  #convert and triangulate  
  doc = op.GetDocument()  
  if op.GetType() !=c4d.Opolygon:  
      virtualop = utils.SendModelingCommand(command=c4d.MCOMMAND_CURRENTSTATETOOBJECT,  
                                                  list=[op.GetClone()],  
                                                  doc=doc)  
      if not virtualop: return        #it was not possible to convert the object         
  else:  
      virtualop = [op.GetClone()]  
            
  obj = utils.SendModelingCommand(command=c4d.MCOMMAND_TRIANGULATE,  
                                                  list=[virtualop[0]],  
                                                  doc=doc)  
  if not obj: return                  #it was not possible to triangulate the object  
  return virtualop[0]  
  
  
#############################################          
def Intersect_Rc(obj,p0,p1) :  
        
  ray = utils.GeRayCollider()            
  ray.Init(obj, True)         
    
  matr = obj.GetMg()     
    
    
  #transfer line points to local coordinates of the object  
  #as the ray collider works in local space of the object  
  pOlocal = p0 * ~matr  
  p1local = p1 * ~matr  
  ldir = p1local-pOlocal  
    
    
  direction = ldir.GetNormalized()  
  raylength = ldir.GetLength()  
    
  CollisionState = ray.Intersect(pOlocal, direction, raylength)  
  erg = []  
  count= ray.GetIntersectionCount()  
  print count  
  if count >0:  
      for i in xrange(count) :    
          result = ray.GetIntersection(i)["hitpos"]  
          #transfer hitpoints back to global coordinates  
          result = result * matr  
          erg.append(result)  
        
      return erg  
  else: return  
    
  
  
  
def main() :  
  
    
  #validate object to calculate  
  if not op:return  
  ##triangulate  
  obj = Triangulate(op)  
  if not obj:return  
  if not obj.IsInstanceOf(c4d.Opolygon) :return   #R16 else GetType()  
  #--------------------------------------------  
    
    
  #validate line/ray to check  
  LINE=doc.SearchObject("LINE")   
  if not LINE: return         
  p0l = LINE.GetPoint(0)  
  p1l =  LINE.GetPoint(1)  
  Lmatr = LINE.GetMg()     
  ##global points of the line  
  p0 = p0l*Lmatr  
  p1 = p1l*Lmatr  
  #--------------------------------------------      
    
  #call intersection function  
  erg =  Intersect_Rc(obj,p0,p1)  
  #--------------------------------------------        
    
  #insert test objects if collision appears  
  if erg:  
      for i in xrange(len(erg)) :  
          sphere = c4d.BaseObject(c4d.Osphere)  
          sphere[c4d.PRIM_SPHERE_RAD] = 10  
          doc.InsertObject(sphere)  
          sphere.SetAbsPos(erg[i])  
  #--------------------------------------------    
    
    
  c4d.EventAdd()  
  
if __name__=='__main__':  
  main()  
  

On 17/06/2015 at 15:58, xxxxxxxx wrote:

Hi Martin,

thanks a lot for the explanation and your very helpful code example !!

I should have read the manual a little more careful: **

parameters of "intersect" method:

** ray_p (Vector) – Start point of the ray in object coordinates
ray_dir (Vector) – Ray direction in object coordinates. **

**best regards,
Jens