HiddenSpartan on DeviantArthttps://www.deviantart.com/hiddenspartan/art/Rectangle-Collision-Resolution-166511435HiddenSpartan

Deviation Actions

HiddenSpartan's avatar

Rectangle Collision Resolution

Published:
892 Views

Description


def rectangle_overlap(a, b):
if not rectangle_ovlbool(a, b): # if they do not intersect
return Vect() # return an empty (0, 0) vector
if a.x < b.x: # setting up which rectangle is to the left or right
l = a
r = b
else:
l = b
r = a
if a.y < b.y: # setting up which rectangle is above and below
v = a
e = b
else:
v = b
e = a

# calculate overlap
ovlx = a.w + b.w - (r.x - l.x)
ovly = a.h + b.h - (e.y - v.y)

# find shortest overlap to use
if ovlx < ovly:
ovly = 0
if l == b: # if b, not a as assumed, is on the left
ovlx *= -1 # flip x
else:
ovlx = 0
if v == b: # if b is above
ovly *= -1 # flip y
return Vect(ovlx, ovly)


Like that, but in HaXe. This sort of thing would work for situations where you don't need full polygon testing, just rects, like in a zelda game (most of them).
Image size
800x600px 10.36 KB
© 2010 - 2024 HiddenSpartan
Comments5
Join the community to add your comment. Already a deviant? Log In
ginkner's avatar
Please post in group?