Last Updated: February 25, 2016
·
1.759K
· tjbladez

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

Picture