Smooth Turns and Corners in Impact.js
Algorithm to allow smooth turns and corner turns in impact.js games.
handleMovementTrace: (res) ->
@parent(res)
@_handleCornerSmoothing(res, 'x')
@_handleCornerSmoothing(res, 'y')
_handleCornerSmoothing: (res, collisionAxies) ->
axies = if collisionAxies is 'x' then 'y' else 'x'
@_handleCollision(res, axies) if res.collision[collisionAxies]
_handleCollision: (res, axies)->
size = @size[axies]
mod = res.pos[axies] % size
belowCenter = mod < size/2
diff = if belowCenter then mod else size - mod
if Math.abs(diff) < (size/3) # smoothing criterion
if belowCenter then @pos[axies] -= diff else @pos[axies] += diff
Written by Nickolay Schwarz
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Game development
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#