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()