Advertisement
NatThePorcupine

Horizontal Camera Panning Routine

Sep 3rd, 2020 (edited)
4,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sonic_PanCamera:
  2.         move.w  #159+16,d0          ; NTP: Sets the right-most edge of the camera deadzone (center screen x coordinate + rounded-up deadzone radius; actual deadzone width is 31px)
  3.         btst.b  #0,status(a0)           ; NTP: Check the direction the player is facing
  4.         beq.s   .isRight            ; NTP: If facing right, branch
  5.         addq.w  #1,d0               ; NTP: If facing left, we adjust the right-most edge of the deadzone, since the deadzone width is an odd number (175 -> 176)
  6.                             ; NTP: What this means is the camera will pan one less pixel ahead if the player happens to be facing opposite the direction that they're moving
  7.  
  8. .isRight:
  9.         tst.b   obj_control(a0)         ; NTP: Check if the player is under control of another object (CPZ Tubes, CNZ Flippers, etc.)
  10.         bne.s   .centerTarget           ; NTP: If so, branch ahead & use the deadzone edge as the target coordinate
  11.         btst.b  #1,status(a0)           ; NTP: Check if the player is in the air
  12.         beq.s   .onGround           ; NTP: If not, branch to the "on ground" code
  13.  
  14. .inAir:
  15.         move.w  x_vel(a0),d1            ; NTP: Load the player's horizontal veloctiy
  16.         beq.s   .centerTarget           ; NTP: If it's 0, branch ahead & use the deadzone edge as the target coordinate; we don't need to do math on that
  17.         bra.s   .setTarget          ; NTP: Otherwise, we skip ahead to the capping code
  18.  
  19. .onGround:
  20.         move.w  inertia(a0),d1          ; NTP: Load the player's ground veloctiy
  21.         beq.s   .centerTarget           ; NTP: If it's 0, branch ahead & use the deadzone edge as the target coordinate; we don't need to do math on that
  22.  
  23. .setTarget:
  24.         cmpi.w  #$1000,d1           ; NTP: Is the loaded velocity value higher than 16px/f?
  25.         ble.s   .noPosCap           ; NTP: If not, branch
  26.         move.w  #$1000,d1           ; NTP: If so, cap it at a max of 16px/f
  27.         bra.s   .noNegCap           ; NTP: And skip ahead to the target value calculation
  28.  
  29. .noPosCap:
  30.         cmpi.w  #-$1000,d1          ; NTP: Is the loaded velocity value lower than -16px/f?
  31.         bge.s   .noNegCap           ; NTP: If not, branch
  32.         move.w  #-$1000,d1          ; NTP: If so, cap it at a min of -16px/f
  33.  
  34. .noNegCap:
  35.         asr.w   #6,d1               ; NTP: Divide the velocity value by 64 (sort of; due to how shifting works, negative number don't always produce accurate results)
  36.         sub.w   d1,d0               ; NTP: Subtract the velocity factor to get our target value
  37.  
  38. .centerTarget:
  39.         move.w  (Horiz_scroll_pan).w,d1     ; NTP: Load the previous horizontal pan value
  40.  
  41.         add.w   d1,d0               ; NTP: All of the following add/shift mess is, for the most part, identical to this equation, sans rounding differences: (d0 + 31(d1)) / 32
  42.         add.w   d1,d0               ; NTP: What this accomplishes is the calculation an intermediate values that is heavily biased towards the pan value on the previous frame...
  43.         add.w   d1,d0               ; NTP: ...That the pan value will be set to, allowing us to create a nice, smooth, dynamic camera pan that isn't too jarring or jerky.
  44.         asr.w   #2,d0
  45.         add.w   d1,d0               ; NTP: Now the lot of you are probably wondering what causes the "deadzone" at the center of the screen, where slowing down doesn't scroll...
  46.         add.w   d1,d0               ; NTP: ...the camera back towards the center of the screen until you begin moving in the opposite direction entirely, or why we need one at...
  47.         add.w   d1,d0               ; NTP: ...all instead of just panning back to the center of the screen entirely. Well, truth be told... I have no ide what causes the deadzone!
  48.         asr.w   #2,d0               ; NTP: It's true; it was a quirk in the code that popped up while I was writting this camera system that I loved so much that I fully embraced...
  49.         add.w   d1,d0               ; NTP: ...it instead of investigating what had caused it to begin with. I personally think it's useful to still be able to see a slight bit...
  50.         asr.w   #1,d0               ; NTP: ...ahead of you even after coming to a complete stop, but I could not even begin to tell you what caused it. I'll leave that to you all! :P
  51.  
  52.         move.w  d0,(Horiz_scroll_pan).w     ; NTP: Oh yeah, and uh, this line just sets the new pan value once we're done running all the calculations & such
  53.         rts
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement