python - How's this for collision detection? -
i have write collide
method inside rectangle
class takes rectangle
object parameter , returns true
if collides rectangle performing method , false
if doesn't. solution use for
loop iterates through every value of x , y in 1 rectangle see if falls within other, suspect there might more efficient or elegant ways it. method (i think names pretty self explanatory, ask if isn't clear):
def collide(self,target): result = false x in range(self.x,self.x+self.width): if x in range(target.get_x(),target.get_x()+target.get_width()): result = true y in range(self.y,self.y+self.height): if y in range(target.get_y(),target.get_y()+target.get_height()): result = true return result
thanks in advance!
the problem of collision detection well-known one, thought rather speculate might search working algorithm using well-known search engine. turns out literature on rectangle overlap less easy come might think. before move on that, perhaps can comment on use of constructs like
if x in range(target.get_x(),target.get_x()+target.get_width()):
it python's credit such obvious expression of idea succeeds intended. may not realize (in python 2, anyway) each use of range() creates list (in python 3 creates generator , iterates on instead; if don't know means please accept it's little better in computational terms). suspect may have meant is
if target.get_x() <= x < target.get_x()+target.get_width():
(i using open interval testing reflect use of range()
)this has merit of replacing n equality comparisons 2 chained comparisons. relatively simple mathematical operation (subtracting target.get_x()
each term in comparison) transform into
if 0 <= x-target.get_x() < target.get_width():
do not overlook value of eliminating such redundant method calls, though it's simpler save evaluated expressions assignment future reference.
of course, after scrutiny have renewed vigor at
for x in range(self.x,self.x+self.width):
this sets lower , upper bound on x, , inequality wrote has false values of x. delving beyond code purpose of algorithm, however, worth doing. because lit creation inner test might have done duplicated many times on (by width of object, precise). take liberty of paraphrasing
for x in range(self.x,self.x+self.width): if x in range(target.get_x(),target.get_x()+target.get_width()): result = true
into pseudocode: "if x between self.x , self.x+self.width lies between target's x , target's x+width, objects colliding". in other words, whether 2 ranges overlap. sure doing lot of work find out.
also, because 2 objects collide in x dimension doesn't mean collide in space. in fact, if not collide in y dimension objects disjoint, otherwise assess these rectangles colliding:
+----+ | | | | +----+ +----+ | | | | +----+
so want know if collide in both dimensions, not one. ideally 1 define one-dimensional collision detection (which have ...) , apply in both dimensions. hope accessor functions can replaced simple attribute access, , code on going assume that's case.
having gone far, it's time take quick @ principles in this youtube video, makes geometry relatively clear doesn't express formula @ well. explains principles quite long using same coordinate system. 2 objects , b overlap horizontally if a's left side between b's left , right sides. overlap if b's right between a's left , right. both conditions might true, in python should think using keyword or
avoid unnecessary comparisons.
so let's define one-dimensional overlap function:
def oned_ol(aleft, aright, bleft, bright): return (aleft <= bright < aright) or (bleft <= aright < bright)
i'm going cheat , use both dimensions, since inside of function doesn't know dimension's data cam calling with. if correct, following formulation should do:
def rect_overlap(self, target): return oned_ol(self.x, self.x+self.width, target.x, target.x+target.width) \ , oned_ol(self.y, self.y+self.height, target.y, target.y+target.height
if insist on using accessor methods have re-cast code include them. i've done sketchy testing on 1-d overlap function, , none @ on rect_overlap, please let me know - caveat lector. 2 things emerge.
a superficial examination of code can lead "optimization" of hopelessly inefficient algorithm, it's better return first principles , more @ algorithm.
if use expressions arguments function available name inside function body without need make explicit assignment.
Comments
Post a Comment