Python Collision Detection With X And Y Coordinates For Border
Solution 1:
Collision is easy! Before the nitty gritty, you need to understand how to obtain the distance between two points. If you have not done this before it is just pythag!
If you picture two points on a plane (red points on the picture), the shortest distance to travel between them, is directly from one point to another, without needing to make any turns, this is the distance between the points. In the picture above, let y be the vertical axis and x the horizontal axis. The horizontal distance between points d and e is represented by the value b. The vertical distance between points d and e is represented by the value a. As such...
a = d.y - e.y b = d.x - e.x
Although a and be might be negative, it doesn't matter, because we sqaure them in a the next step.
To get the value of c, we then have to get the square root of the sum of the squares of a and b. Might sound tricky at first, but very easy!
Python code To do this in python is simple.
c = ((a**2)+(b**2))**0.5# a**2 is a squared# anything to the power of 0.5 is square rooted, test it in console# 25**0.5 = 5.0# 5**2 = 25
We now have the distance between the two points d and e. Lets say d and e have the radius rd and re. We can then check if the circle d is colliding with circle e, by subtracting each radius from the distance between the centre of the circles. So c becomes...
c-= rd - re
If c is less than or equal to zero, then you have a collision between circles!
defcollision(d, e, rd, re):
a = d.y-e.y
b = d.x-e.x
c = ((a**2)+(b**2))**0.5if c > 0:
# no collisionreturnFalsereturnTrue
Rectangles Rectangles are a little easier, to check if a point is inside a rectangle all you need is some if statements. Let these variables represent the rectangle x = x location, y = y location, w = width, h = height. Suppose you want to check if point p is colliding with the rectangle.
defcheck_rect_collision(p, x, y, w, h):
if p.x >= x and p.x <= x+w and p.y >= y and p.y <= y+h:
# collision between p and rectanglereturnTruereturnFalse
Solution 2:
To test collision against a circle is trivial -- use the distance()
method which measures from the center of the cursor to a position or center of another turtle. Given a circle's center
position and its radius
:
defcircle_collision(the_turtle, center, radius):
return the_turtle.distance(center) <= radius
If you need to know if the turtle's nose has touched the circle, you could add half the turtle's size to the radius
, for a (possibly resized) cursor which would be very roughly:
def circle_collision(the_turtle, center, radius):
dx, dy, border = the_turtle.shapesize()
return the_turtle.distance(center) <= radius + 5 * (dx + dy) + border
I.e. half the default turtle size of 20 pixels times the average of dx
and dy
plus the width of the border around the turtle. Or some such approximation.
To detect rectangle collision is also reasonably simple:
def rectangle_collision(the_turtle, x, y, width, height):
tx, ty = the_turtle.position()
return x <= tx <= x + width and y <= ty <= y + height
Adjust to whatever rectangle measure you're using:
def rectangle_collision(the_turtle, llx, lly, urx, ury):
x, y = the_turtle.position()
return llx <= x <= urx and lly <= y <= ury
a la the coordinate arguments for setworldcoordinates()
.
Post a Comment for "Python Collision Detection With X And Y Coordinates For Border"