2

I am trying to build a 2D map in python graphics module by Zelle. I created the map boundaries using the Polygon class object. If I wanted to check if a cirlce object is touching the map boundary to detect a collision, what should I do?

This is an example of what I mean:

poly  = Polygon(Point(x1,y1), Point(x2,y2), Point(x3,y3)) .draw(win)  # a triangle shape
circ = Circle (Point(x4,y4), radius) .draw(win)      # drawn in the middle of the triangle map

I can get the circ postion by using circ.getCenter() but I don't know what would be the best way to check if the two objects ever cross. Maybe something like this

def collision(circ,poly,x,y):

    if position of circle passes the position of the line of the poly at x,y:
        detect collision

   else:
       pass
will-hart
  • 3,742
  • 2
  • 38
  • 48
jean
  • 973
  • 1
  • 11
  • 23
  • possible duplicate of [Circle line collision detection](http://stackoverflow.com/questions/1073336/circle-line-collision-detection) – martineau Jan 18 '14 at 03:17
  • 1
    Note also you may be able improve the accepted answer to the "Circle line collision detection" question by doing some high-level checking -- for example, if the distance between the center of an imaginary circle enclosing all the points of the polygon is greater than the sum of the radii of the two circles. – martineau Jan 18 '14 at 03:25

1 Answers1

1

I figured out a collision detection program in python, here it is.

if circle_x < rect_x + circle_width and circle_x + rect_width > rect_x and circle_y < rect_y + circle_height and circle_height + circle_y > rect_height :

However, this senses if the edge of a circle touches a rectangle, so in order to get it to sense if the circle touches the map you will need to replace all of the rect_x with 0 and all of the rect_y with 0, then replace the rect_width with the screen width and the rect_height with the screen height, like this:

if circle_x < 0 + circle_width and circle_x + screen_width > 0 and circle_y < 0 + circle_height and circle_height + circle_y > screen_height :

now it senses if the circle is touching the map in general, in order to sense if the circle is touching the edge of the screen you will need to put nothing in the if statement and make an else statement where you put what you want, like this:

if circle_x < 0 + circle_width and circle_x + screen_width > 0 and circle_y < 0 + circle_height and circle_height + circle_y > screen_height :
    #put nothing here
else:
    #put the code you need here
pi guy
  • 106
  • 1
  • 3
  • 13