def check_colisions(box): collided = False # 1 # # # # 2 # # # # # # # # # # 3 # # # # 4 x = box[0] * X y = box[1] * Y w = box[2] * X h = box[3] * Y center_x = int(x + (w / 2)) center_y = int(y + (h / 2)) pt1 = Vector(center_x - w, center_y - h) pt2 = Vector(center_x - w, center_y + h) pt3 = Vector(center_x + w, center_y - h) pt4 = Vector(center_x + w, center_y + h) box_poly = Poly(Vector(center_x, center_y), [pt1, pt2, pt3, pt4]) #print(box, box_poly, end="\n") if collide(box_poly, LEFT_LINE_POLY) or collide(box_poly, RIGHT_LINE_POLY): collided = True return collided
def __init__(self, shape_definitions: List[ShapeDefinition], collision_tag='genericCollision'): self.shapes = [] for s in shape_definitions: self.shapes.append( Poly(tuple2vector(s[0]), get_rel_points(s[0], s[1]))) self.event_tag = collision_tag
def _is_valid_proposal(o1_x, o1_y, object1_index, bounding_boxes, placements): o1_x += bounding_boxes[object1_index, 0, 0] o1_y += bounding_boxes[object1_index, 0, 1] # Check if object collides with any of already placed objects. We use half-sizes, # but collision uses full-sizes. That's why we multiply by 2x here. o1_w, o1_h, _ = bounding_boxes[object1_index, 1] object1 = Poly.from_box(Vector(o1_x, o1_y), o1_w * 2.0, o1_h * 2.0) for object2_index in range(len(placements)): # Don't care about z placement o2_x, o2_y, _ = placements[object2_index] o2_x += bounding_boxes[object2_index, 0, 0] o2_y += bounding_boxes[object2_index, 0, 1] # Don't care about object depth. o2_w, o2_h, _ = bounding_boxes[object2_index, 1] object2 = Poly.from_box(Vector(o2_x, o2_y), o2_w * 2.0, o2_h * 2.0) if collide(object1, object2): return False return True
def __init__(self, points: Tuple[Tuple[float]] = None, radius: float = None, *args, **kwargs): """ :param points: A tuple of tuples of floats (x,y) that describes the boundary using relative coordinates from the top left most point going clockwise around the perimeter. Object becomes a polygon or point. :param radius: A relative coordinate describing the radius of a circular boundary from the center of the object. Object becomes a circle. """ super(Collidable, self).__init__(*args, **kwargs) if radius and not points: self.collider = Circle(self.rel_vector, radius) elif points and len(points) != 1: self.collider = Poly(self.rel_vector, points) elif points: self.collider = None else: if (len(args) > 1 and args[1]) or 'window_width' in kwargs: width = args[1] if len( args) > 1 and args[1] else kwargs['window_width'] else: raise InvalidArguments if (len(args) > 2 and args[2]) or 'window_height' in kwargs: height = args[2] if len( args) > 2 and args[2] else kwargs['window_height'] else: raise InvalidArguments self.collider = Poly( self.rel_vector, (Vector(self.image.width / width * 16 + self.rel_x, self.image.height / height * 9 + self.rel_y), Vector(self.image.width / width * 16 + self.rel_x, -self.image.height / height * 9 + self.rel_y), Vector(-self.image.width / width * 16 + self.rel_x, -self.image.height / height * 9 + self.rel_y), Vector(-self.image.width / width * 16 + self.rel_x, self.image.height / height * 9 + self.rel_y)))
def from_mxCell(el, batch, windowSize, lineWidth=10): # Parse style style = parse_style(el.attrib['style']) # Get parent style['parent'] = el.attrib['parent'] # Get geometry geometry = el[0] x = float(geometry.attrib.get('x', '0')) y = float(geometry.attrib.get('y', '0')) width = float(geometry.attrib['width']) height = float(geometry.attrib['height']) # Create drawing (x, y) = translate_coordinates((x, y), windowSize, height) pos = Position(x=x, y=y, w=width, h=height, movable=False) rotate = 0 if style.get('rotation', '') != '': rotate = int(style['rotation']) if rotate < 0: rotate = 360 + rotate pos.angle = rotate draw = None col_points = None center = (pos.x + pos.w // 2, pos.y + pos.h // 2) if 'ellipse' in style: draw = primitives.Ellipse(center, width, height, style, rotate) col_points = draw._get_points() else: draw = primitives.Rectangle(x, y, width, height, style, rotate) col_points = pos._get_box() label = el.attrib.get('value', '') if label: label = pyglet.text.HTMLLabel(label, batch=batch, x=center[0], y=center[1], anchor_x='center', anchor_y='center') batch_draw = draw.add_to_batch(batch) col_points = list(map(lambda x: Vector(x[0] - center[0], x[1] - center[1]), col_points)) collision_box = Poly(Vector(center[0], center[1]), col_points) return [pos, Collidable(shape=collision_box)], style, batch_draw
def from_mxCell(el, batch, windowSize, lineWidth=10): # Parse style style = parse_style(el.attrib['style']) if style.get('shape') != 'mxgraph.floorplan.wall': raise Exception("Cannot create Wall from {}: shape is not mxgraph.floorplan.wall".format(el)) # Get parent parent_element = el.attrib['parent'] style['parent'] = parent_element # Get geometry geometry = el[0] x = float(geometry.attrib.get('x', '0')) y = float(geometry.attrib.get('y', '0')) width = float(geometry.attrib['width']) height = float(geometry.attrib['height']) # Create drawing (x, y) = translate_coordinates((x, y), windowSize, height) pos = Position(x=x, y=y, w=width, h=height, movable=False) rotate = 0 if 'rotation' in style: rotate = int(style['rotation']) if rotate < 0: rotate = 360 + rotate pos.angle = rotate label = el.attrib.get('value', '') if label: label = pyglet.text.HTMLLabel(label, batch=batch, x=center[0], y=center[1], anchor_x='center', anchor_y='center') # Create collision box col_points = pos._get_box() center = (pos.x + pos.w // 2, pos.y + pos.h // 2) col_points = list(map(lambda x: Vector(x[0] - center[0], x[1] - center[1]), col_points)) collision_box = Poly(Vector(center[0], center[1]), col_points) rectangle = primitives.Rectangle(x, y, width, height, style, rotate) rectangle.add_to_batch(batch) return ([pos, Collidable(shape=collision_box)], style)
def from_mxCell(el, batch, windowSize, lineWidth=10): # Parse style style = parse_style(el.attrib['style']) if style.get('shape', "") != 'mxgraph.floorplan.room': raise Exception( "Cannot create Wall from {}: shape is not mxgraph.floorplan.room". format(el)) # Get parent parent_element = el.attrib['parent'] # Get geometry geometry = el[0] x = float(geometry.attrib.get('x', '0')) y = float(geometry.attrib.get('y', '0')) width = float(geometry.attrib['width']) height = float(geometry.attrib['height']) # Create drawing (x, y) = translate_coordinates((x, y), windowSize, height) pos = Position(x=x, y=y, w=width, h=height, movable=False) center = (pos.x + pos.w // 2, pos.y + pos.h // 2) points = [(pos.x, pos.y), (pos.x, pos.y + pos.h), (pos.x - lineWidth // 2, pos.y + pos.h), (pos.x + pos.w + lineWidth // 2, pos.y + pos.h), (pos.x + pos.w, pos.y + pos.h), (pos.x + pos.w, pos.y), (pos.x + pos.w + lineWidth // 2, pos.y), (pos.x - lineWidth // 2, pos.y)] # Collision points # TODO: Verify corners boxes = [((pos.x + lineWidth // 2, pos.y + pos.h // 2), [(pos.x, pos.y), (pos.x, pos.y + pos.h), (pos.x + lineWidth, pos.y + pos.h), (pos.x + lineWidth, pos.y)]), ((pos.x + pos.w // 2, pos.y + pos.h + lineWidth // 2), [(pos.x, pos.y + pos.h), (pos.x, pos.y + pos.h + lineWidth), (pos.x + pos.w, pos.y + pos.h + lineWidth), (pos.x + pos.w, pos.y + pos.h)]), ((pos.x + pos.w + lineWidth // 2, pos.y + pos.h // 2), [(pos.x + pos.w, pos.y + pos.h), (pos.x + pos.w + lineWidth, pos.y + pos.h), (pos.x + pos.w + lineWidth, pos.y), (pos.x + pos.w, pos.y)]), ((pos.x + pos.w // 2, pos.y + lineWidth // 2), [(pos.x, pos.y), (pos.x, pos.y + lineWidth), (pos.x + pos.w, pos.y + lineWidth), (pos.x + pos.w, pos.y)])] boxes = list( map(lambda x: Poly(tuple2vector(x[0]), get_rel_points(x[0], x[1])), boxes)) if style.get('rotation', '') != '': rotate = int(style['rotation']) if rotate < 0: rotate = 360 - rotate for box in boxes: box.angle = rotate points = map( lambda x: rotate_around_point(x, math.radians(rotate), center), points) drawing = primitives.Line(list(points), style) drawing.add_to_batch(batch) label = el.attrib.get('value', '') if label: label = pyglet.text.HTMLLabel(label, batch=batch, x=center[0], y=center[1], anchor_x='center', anchor_y='center') return ([pos, Collidable(shape=boxes)], style)
def collision_from_points(shape: ShapeType, center: typing.Tuple[int, int]) -> Poly: points = shape._get_points() col_points = list(map(lambda x: Vector(x[0] - center[0], x[1] - center[1]), points)) return Poly(tuple2vector(center), col_points)
LEFT_LINE_X = CENTER_X - 110 LEFT_LINE_Y = 640 RIGHT_LINE_X = CENTER_X + 110 RIGHT_LINE_Y = 640 LEFT_MIDDLE = ((LEFT_LINE_X + TOP_X) / 2, (LEFT_LINE_Y + TOP_Y) / 2) LEFT_MIDDLE = (320 + 110, 260) RIGHT_MIDDLE = ((RIGHT_LINE_X + TOP_X) / 2, (RIGHT_LINE_Y + TOP_Y) / 2) #print(f"TOP X {CENTER_X} Y {TOP_Y} | LEFT X {LEFT_LINE_X} Y {LEFT_LINE_Y} \ # MID {LEFT_MIDDLE} | RIGHT X {RIGHT_LINE_X} Y {RIGHT_LINE_Y} MID \ # {RIGHT_MIDDLE}") LEFT_LINE_POLY = Poly(Vector(LEFT_MIDDLE[0], LEFT_MIDDLE[1]), [Vector(TOP_X, TOP_Y), Vector(LEFT_LINE_X, LEFT_LINE_Y)]) RIGHT_LINE_POLY = Poly( Vector(RIGHT_MIDDLE[0], RIGHT_MIDDLE[1]), [Vector(TOP_X, TOP_Y), Vector(RIGHT_LINE_X, RIGHT_LINE_Y)]) thickness = 1 isClosed = True # Blue color in BGR color = (255, 0, 0) # Create some points ppt = np.array( [TWO_METERS_LEFT, TWO_METERS_RIGTH, TREE_METERS_RIGTH, TREE_METERS_LEFT], np.int32)