Skip to content

Instantly share code, notes, and snippets.

@Orpheon
Created September 1, 2011 18:28
Show Gist options
  • Save Orpheon/1186870 to your computer and use it in GitHub Desktop.
Save Orpheon/1186870 to your computer and use it in GitHub Desktop.
def characterHitObstacle(character, wallmask):
# THIS IS THE NEW VERSION; STILL WITH x/y
newX = character.x
newY = character.y
hspeed = character.hspeed
vspeed = character.vspeed
length = lengthdir(hspeed, vspeed)
if length == 0:# You haven't moved; if this happens something went wrong
print "You haven't moved, yet managed to collide with something."
return False
# hs and vs is the normalized vector of hspeed and vspeed.
hs = character.hspeed/length
vs = character.vspeed/length
while True:
if not objectCheckCollision(character, wallmask):
break
character.x -= hs
character.y -= vs
print character.x, character.y, hs, vs
return True
# The character got pushed out, but now we need to let him move in the directions he's allowed to move.
character.x += sign(character.hspeed)
if not objectCheckCollision(character, wallmask):
# There's empty space on the left/right
# Kill all vertical movement
character.vspeed = 0
i = 0
while i <= hspeed:
character.x += sign(hspeed)
# If the new position has met a wall too:
if objectCheckCollision(character, wallmask):
character.x -= sign(hspeed)
break
i += 1
else:
character.x += sign(hspeed)
character.y += sign(vspeed)
if not objectCheckCollision(character, wallmask):
# There's empty space on the left/right
# Kill all vertical movement
character.hspeed = 0
i = 0
while i <= vspeed:
character.y += sign(vspeed)
# If the new position has met a wall too:
if objectCheckCollision(character, wallmask):
character.y -= sign(vspeed)
break
i += 1
character.y -= sign(character.vspeed)
# character.hspeed = 0
# character.vspeed = 0
character.hspeed = character.oldX-character.x
character.vspeed = character.oldY-character.y
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment