Advertisement
tepples

Lerp-smoothed camera position

Jun 24th, 2015
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Calculation of new camera position with lerp smoothing,
  2. ; assuming 256-pixel screen width and 12.4 fixed-point math
  3. ; caution: not tested
  4. .proc calc_new_camera_pos
  5. camtarget_lo = $00
  6. camtarget_hi = $01
  7.  
  8.   ; Step 1: Find the position that the camera seeks.
  9.   lda player_x
  10.   sta camtarget_lo
  11.   lda player_xhi
  12.   ldy player_facing  ; 0 for right, 1 for left
  13.   sec
  14.   sbc camera_target_by_facing
  15.   bcs not_off_left_side
  16.     lda #0
  17.     sta camtargetlo
  18.     sec
  19.   not_off_left_side:
  20.   sta camtargethi
  21.  
  22.   ; Step 2: Find the distance that the camera must move
  23.   ; to reach this position.
  24.   lda camtarget_lo
  25.   sbc camera_lo
  26.   sta camtarget_lo
  27.   lda camtarget_hi
  28.   sbc camera_hi
  29.   ; and divide by 4 for smoothing
  30.   cmp #$80
  31.   ror a
  32.   ror camtarget_lo
  33.   cmp #$80
  34.   ror a
  35.   ror camtarget_lo
  36.   sta camtarget_hi
  37.  
  38.   ; Step 3: Add this to produce the camera's new position.
  39.   ; The bit shifted out of camtarget_lo into carry can be used
  40.   ; for free
  41.   lda camtarget_lo
  42.   adc camera_lo
  43.   sta new_camera_lo
  44.   lda camtarget_hi
  45.   adc camera_hi
  46.   sta new_camera_hi
  47.   rts
  48.  
  49. .pushseg
  50. .segment "RODATA"
  51. camera_target_by_facing:
  52.   .byte 6  ; When facing right, put 6 blocks to player's left
  53.   .byte 10  ; When facing left, put 10 blocks to player's left
  54. .popseg
  55. .endproc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement