Hi Mario,
Thanks for the suggestions but two comments on this:
1. Boundbox will not work for objects that have cavities for example. The boundbox will detect a collision while there actually is none.
2. distToShape also returns True when objects are just touching and not colliding.
I found a solution that covers (almost) all cases. It works as follows:
1. Perform distToShape
2. If distToShape is 0 take the closest points that distToShape returns and do isInside with last argument set to False to take on Face points as False.
It does not cover the case in which the closest point detected by distToShape lies just on the interesection while there is another point that lies inside the other object.
Code: Select all
def collision_detect(main_shape, other_shapes):
"""Checks if an object is colliding with another object. If they are touching 'False' is returned
Input:
main_shape: An OCC shape object
other_shapes: An OCC shape object or a list of OCC shape objects
Output:
A list of booleans indicating if the respective input shape in 'other_shapes' list is colliding with 'main_shape'
WARNING: Step 3. is not waterproof. If by a lot of coincidence the point that is found by distToShape is on the
intersection point than it will return False while it is actually True. Happens very rarely."""
#0. Make a list if other shapes is not a list yet
if type(other_shapes) is not list:
other_shapes = [other_shapes]
collision_bools = [] #List in which the answers will be gathered
for other_shape in other_shapes:
#1. Check if boundary boxes collide
if not main_shape.BoundBox.intersect(other_shape.BoundBox):
collision_bools.append(False) #bbox does not intersect --> no collision
continue
#2. Check shortest distance (if 0.0, check further)
dist, points, geom = main_shape.distToShape(other_shape)
if dist>0.0:
collision_bools.append(False) # min dist is larger than 0.0 --> no collision
continue
#3. Check if the points are inside and not only touching
if not main_shape.isInside(points[0][1],10**-6, False) and not other_shape.isInside(points[0][1],10**-6, False): #Last argument indicates if lying on face is considered inside
collision_bools.append(False)
continue
#Must be colliding
collision_bools.append(True)
return collision_bools