Skip to content

Instantly share code, notes, and snippets.

@talentless
Created May 11, 2012 01:40
Show Gist options
  • Save talentless/2656971 to your computer and use it in GitHub Desktop.
Save talentless/2656971 to your computer and use it in GitHub Desktop.
op dpad psuedocode
# magic numbers
scalePad = 1.5
dragAfterDistance = 60
recenterDelay = 1.2
velocityMultiplier = 9
cornerBuffer = 50
# scheduled method
def step(deltaTime):
timeSinceRecenter = timeSinceRecenter + deltaTime
def onTouchBegan:
if touch in lower left quadrant:
# leave the dpad in place when the finger comes off briefly
if timeSinceRecenter > recenterDelay:
centerPoint = keepAwayFromEdges(touchLocation)
timeSinceRecenter = 0
def onTouchMoved:
if touch in lower left quadrant:
timeSinceRecenter = 0
delta = location - centerPoint
if iPad:
delta = delta * scalePad
player.velocity = delta * velocityMultiplier
# if the touch drifts, move the dpad
distance_from_center = distance(location, centerPoint)
if distance_from_center > dragAfterDistance:
centerPoint = location + ((centerPoint-location) * (dragAfterDistance/distance_from_center))
# don't start with the dpad in the corner
def keepAwayFromEdges(touchLocation):
return {x:MAX(cornerBuffer, touchLocation.x), y:MAX(cornerBuffer, touchLocation.y)}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment