Skip to content

Instantly share code, notes, and snippets.

Created January 19, 2011 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/786255 to your computer and use it in GitHub Desktop.
Save anonymous/786255 to your computer and use it in GitHub Desktop.
for maryrosecook and pistol slut
def DetectCollision(grenade, obj):
hgw = grenade.width / 2 # half grenade width
hgh = grenade.height / 2
aug_obj = Rectangle(obj.x1 - hgw, obj.y1 - hgh,
obj.x2 + hgw, obj.y2 + hgh)
if aug_obj.contains(grenade.center):
BounceGrenade(grenade, aug_obj)
def BounceGrenade(grenade, aug_obj):
vsegs = [Segment(aug_obj.x1, aug_obj.y1, aug_obj.x1, aug_obj.y2),
Segment(aug_obj.x2, aug_obj.y1, aug_obj.x2, aug_obj.y2)]
hsegs = [Segment(aug_obj.x1, aug_obj.y1, aug_obj.x2, aug_obj.y1),
Segment(aug_obj.x1, aug_obj.y2, aug_obj.x2, aug_obj.y2)]
trajectory = Segment(grenade.last_center, grenade.center)
for seg in vsegs:
if trajectory.intersect(seg):
distance_into_obj = grenade.center - seg.x1
grenade.center -= Vector(2 * distance_into_obj, 0)
return
for seg in hsegs:
if trajectory.intersect(seg):
distance_into_obj = grenade.center - seg.y1
grenade.center -= Vector(0, 2 * distance_into_obj)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment