GeRayCollider



  • On 29/10/2013 at 07:39, xxxxxxxx wrote:

    Hello,
    I used the function GeRayCollider() in a plugin. But I have a few problems.
    First I created a cube.
    Then I  tried to initialize the ray collider with this cube.
    But I got an error message, that the argument must be a polygon object.
    So I tried to convert the cube to a polygonobject.
    I used a call command for this and it works (I can see the polygon object in cinema 4d), but the error message is the same.
    Here ist the code:

      
    import c4d  
    import os, sys  
    import time  
    import math  
    from c4d import plugins  
    from c4d import utils  
    from config import PLUGIN_ID
     
    class CommandDataExecute(plugins.CommandData) :  
          
        dialog = None
        #plugin started by user  
        def Execute(self, doc) :  
             
            cube = c4d.BaseObject(c4d.Ocube)  
            doc.InsertObject(cube)  
            c4d.EventAdd()  
            cube.SetBit(c4d.BIT_ACTIVE)  
            print "Typ: ", cube.GetType()  
            c4d.CallCommand(12236)  
            c4d.EventAdd()  
            print "Typ: ", cube.GetType()      
            ray=c4d.utils.GeRayCollider()  
            if not ray.Init(cube) :  
                print "Initialisierung nicht erfolgreich"  
            else:  
                start = c4d.Vector(400,0,0)  
                end = c4d.Vector(-400,0,0)  
                direction = c4d.Vector(-1,0,0)  
                length = 800  
                print "globale matrix cube: ", cube.GetMg()  
                print "radius of the bounding box: ", cube.GetRad()  
                does_intersect = ray.Intersect(start, direction, length)  
                print "Intersection? ", does_intersect  
            return True  
            
    

    Now I have four questions:
    1. Is it necessary to convert the cube to a polygon object?
    2. Is there a better way to convert the cube than using call commands?
    3. Why doesnt the function "ray.Init(cube)" work?
    4. "does_intersect = ray.Intersect(start, direction, length)" should return true. Did I use the function correctly?
     
    Thanks for your comments.
    Conny



  • On 29/10/2013 at 10:10, xxxxxxxx wrote:

    1. yes
    2. yes two ways - a. grabbing the cache of the object (with is not an option in your scenario) and b. using the SMC method (SendModellingCommand). 
    3. The reason is most likely the CallCommand() if you want to use CallCommand() you will have to use the force (Cache) parameter of the method. And even then i wouldn't be so sure if it will work (the usual threading mess with c4ds main thread).
    4. depends on what you were trying to do, but basically yes.



  • On 12/11/2013 at 07:27, xxxxxxxx wrote:

    Thank you. The SMC method works.
    But now I have a problem with the intersection.
    In the following example I created two cubes and for each of them a GeRayCollider (ray1 and ray2).
    ray1 intersects cube 1 in one point. But I get two intersections in the same point.
    Although ray 2 doesn't intersect cube2 I get the same two intersections as with ray1.
    I expected that the function ray2.Intersect() only returns true, if there is an intersection with cube2. 
    Can someone explane me that?

      
    import c4d  
    import os, sys  
    import time  
    import math  
    from c4d import plugins  
    from c4d import documents  
    from c4d import utils  
    from config import PLUGIN_ID
      
    class CommandDataExecute(plugins.CommandData) :  
          
        dialog = None
        #plugin started by user  
        def Execute(self, doc) :  
            doc = documents.GetActiveDocument()  
             
                   
           #create two cubes and convert them to polygon objects  
             
            cube1 = c4d.BaseObject(c4d.Ocube)  
            cube2 = c4d.BaseObject(c4d.Ocube)  
            off = c4d.Vector(-300,0,0)  
            v1 = c4d.Vector(1,0,0)  
            v2 = c4d.Vector(0,1,0)  
            v3 = c4d.Vector(0,0,1)  
            matrix=c4d.Matrix(off, v1, v2, v3)  
              
            cube2.SetMg(matrix)  
            doc.InsertObject(cube1)  
            doc.InsertObject(cube2)  
            c4d.EventAdd()  
              
            cube1.SetBit(c4d.BIT_ACTIVE)  
            obj=doc.GetActiveObject()    
            convert=c4d.utils.SendModelingCommand(command=c4d.MCOMMAND_MAKEEDITABLE , list=[obj], mode= c4d.MODELINGCOMMANDMODE_ALL, bc= c4d.BaseContainer(), doc = doc)  
            doc.InsertObject(convert[0])  
            c4d.EventAdd()  
            cube1=convert[0]  
              
            cube2.SetBit(c4d.BIT_ACTIVE)  
            obj=doc.GetActiveObject()    
            convert=c4d.utils.SendModelingCommand(command=c4d.MCOMMAND_MAKEEDITABLE , list=[obj], mode= c4d.MODELINGCOMMANDMODE_ALL, bc= c4d.BaseContainer(), doc = doc)  
            doc.InsertObject(convert[0])  
            c4d.EventAdd()  
            cube2=convert[0]  
              
              
            #create GeRayCollider  
            ray1=c4d.utils.GeRayCollider()  
            ray2=c4d.utils.GeRayCollider()  
              
                           
            start = c4d.Vector(0,0,-700)  
            end = c4d.Vector(0,0,0)  
            dir = end - start  
            length = math.sqrt(dir.x*dir.x+dir.y*dir.y+dir.z*dir.z)  
            direction=dir.GetNormalized()  
            print "direction: ", direction  
            print "length: ", length  
            print "cube1: "  
            if not ray1.Init(cube1) :  
                print "Initialisierung nicht erfolgreich"  
            else:  
                  
                does_intersect1 = ray1.Intersect(start, direction, length)  
                print "Intersection? ", does_intersect1  
                j = ray1.GetIntersectionCount()  
                print "Number of Intersections: ", j  
                for k in range (0, j, 1) :  
                    print "Intersection ",k,": ", ray1.GetIntersection(k)  
                print "----------------------------------------------"  
                print "cube2: "  
            if not ray2.Init(cube2) :  
                print "Initialisierung nicht erfolgreich"  
            else:  
                  
                does_intersect2 = ray2.Intersect(start, direction, length)  
                print "Intersection? ", does_intersect2  
                j = ray2.GetIntersectionCount()  
                print "Number of Intersections: ", j  
                for k in range (0, j, 1) :  
                    print "Intersection ",k,": ", ray2.GetIntersection(k)  
                print "----------------------------------------------"  
           
             
            return True  
      
    

Log in to reply