Advertisement
NovaYoshi

Player physics psuedocode

May 12th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. X player coordinate is the middle of the player horizontally
  2. Y player coordinate is one pixel under the bottom of the player
  3.  
  4. Constants:
  5. BlockSize : Width/Height of a map block
  6.  
  7. Variables:
  8. PlayerRunning : Player is currently running
  9. PlayerOnGround : Player is on the ground this frame
  10. PlayerWasOnGround : Player was on the ground last frame
  11. MaxSpeed : Maximum allowed speed for walking/running
  12. PlayerPX : Player X position
  13. PlayerPY : Player Y position
  14. PlayerVX : Player X speed
  15. PlayerVY : Player Y speed
  16.  
  17. GetSlopeHeight(X, Y):
  18. BlockID = Level[X / BlockSize][Y / BlockSize]
  19. return the Y position corresponding to the highest point on the block, using X to select a column within the block
  20. (If the column is the maximum height, check if the block above is a slope, and use that block's highest point if so)
  21.  
  22. Player update():
  23. PlayerWasOnGround = PlayerOnGround
  24. PlayerOnGround = 0
  25.  
  26. // Get the maximum horizontal speed
  27. If PlayerRunning:
  28. MaxSpeed = Run speed
  29. Else:
  30. MaxSpeed = Walk speed
  31.  
  32. // Speed up if pressing left/right
  33. If pressing left:
  34. If PlayerVX > -MaxSpeed:
  35. PlayerVX -= Acceleration speed
  36.  
  37. If pressing right:
  38. If PlayerVX < MaxSpeed:
  39. PlayerVX += Acceleration speed
  40.  
  41. // Slow down if not pressing left/right
  42. Temp = Deceleration speed
  43. If pressing left:
  44. If PlayerVX < 0:
  45. goto SkipDeceleration
  46. Temp /= 2
  47. If pressing right:
  48. If PlayerVX > 0:
  49. goto SkipDeceleration
  50. Temp /= 2
  51. If PlayerVX > 0:
  52. PlayerVX -= Temp
  53. If PlayerVX < 0:
  54. PlayerVX = 0
  55. Else If PlayerVX < 0:
  56. PlayerVX += Temp
  57. If PlayerVX > 0:
  58. PlayerVX = 0
  59. SkipDeceleration:
  60.  
  61. PlayerPX += PlayerVX
  62.  
  63. // Going downhill requires extra help
  64. If PlayerWasOnGround && !JumpButtonPressed && PlayerVX != 0:
  65. If Level[PlayerPX / BlockSize][PlayerPY / BlockSize] is slope:
  66. PlayerPY = GetSlopeHeight(PlayerPX, PlayerPY)
  67. PlayerOnGround = True
  68. PlayerVY = 0
  69. Else If Level[PlayerPX / BlockSize][(PlayerPY+BlockSize) / BlockSize] is slope:
  70. PlayerPY = GetSlopeHeight(PlayerPX, PlayerPY + BlockSize)
  71. PlayerOnGround = True
  72. PlayerVY = 0
  73.  
  74. // Interact with walls
  75. If PlayerVX >= 0:
  76. SideXPos = PlayerPX + 3 pixels
  77. If PlayerVX < 0:
  78. SideXPos = PlayerPX - 3 pixels
  79.  
  80. TrySideInteraction(SideXPos, PlayerPY - 16 pixels)
  81. TrySideInteraction(SideXPos, PlayerPY - 23 pixels)
  82.  
  83. // Vertical movement
  84. If PlayerVY < MaxGravity:
  85. PlayerVY += Gravity
  86. PlayerPY += PlayerVY
  87.  
  88. // Original code allows canceling a jump here, for variable height jumps
  89.  
  90. // Slope interaction
  91. If Level[PlayerPX / BlockSize][PlayerPY / BlockSize] is slope:
  92. Temp = GetSlopeHeight(PlayerPX, PlayerPY)
  93. If PlayerPY >= Temp:
  94. PlayerPY = Temp
  95. PlayerVY = 0
  96. PlayerOnGround = 1
  97. goto SkipGroundCheck
  98.  
  99. // Collide with blocks on the bottom
  100. If PlayerVY >= 0:
  101. TryAboveInteraction(PlayerPX - 4 pixels, PlayerPY)
  102. TryAboveInteraction(PlayerPX + 3 pixels, PlayerPY)
  103. SkipGroundCheck:
  104.  
  105. // Collide with blocks above the player
  106. TryBelowInteraction(PlayerPX, PlayerPY - 30 pixels)
  107.  
  108. If PlayerOnGround:
  109. If JumpButtonPressed:
  110. PlayerVY = Jump velocity
  111. Update PlayerRunning for the current run button status
  112.  
  113. TryBelowInteraction(X, Y):
  114. If Level[X / BlockSize][Y / BlockSize] is solid on the bottom:
  115. PlayerVY = 0
  116. Snap PlayerPY to the bottom of the block
  117.  
  118. TrySideInteraction(X, Y):
  119. If Level[X / BlockSize][Y / BlockSize] is solid on the side:
  120. PlayerVX = 0
  121. Snap PlayerPX to the side of the block
  122.  
  123. TryAboveInteraction(X, Y):
  124. If Level[X / BlockSize][Y / BlockSize] is solid on the top:
  125. PlayerVY = 0
  126. Snap PlayerPY to the top of the block
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement