Skip to content

Instantly share code, notes, and snippets.

@fwilleke80
Last active August 19, 2019 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwilleke80/7991c1c4e708359c0a7200a214b5ef92 to your computer and use it in GitHub Desktop.
Save fwilleke80/7991c1c4e708359c0a7200a214b5ef92 to your computer and use it in GitHub Desktop.
[C4D] A function that uses GeRayCollider to determine whether a given position is inside a polygon object's volume or not. Only works reliably with closed polygon objects. Absolutely not optimised for speed (you would probably want to do the collider initialisation and position transformation elsewhere)
def isInside(pos, polyOp):
"""Check whether a given position is inside a (closed!) polygon object.
:param pos: A position to test for, in global space
:param polyOp: A PolygonObject
:return: True if pos is inside polyOp, otherwise False
"""
# Init GeRayCollider
collider = c4d.utils.GeRayCollider()
collider.Init(polyOp)
# Any direction is fine
rayDirection = c4d.Vector(0.0, 1.0, 0.0)
# Calculate requires ray length
polyOpRad = polyOp.GetRad()
rayLength = (polyOpRad.x + polyOpRad.y + polyOpRad.z) * 2.0 + (pos - polyOp.GetMg().off).GetLength()
# Transform ray position to collider object's local space
rayPos = pos * ~polyOp.GetMg()
# Intersect
collider.Intersect(rayPos, rayDirection, rayLength)
numIntersections = collider.GetIntersectionCount()
# Return result
# An odd number of found intersections tells us we're inside the polygon object
return numIntersections % 2 != 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment