Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. package dev.colors.physics;
  2.  
  3. import java.util.ArrayList;
  4. import dev.colors.level.Level;
  5. import dev.colors.level.LevelObject;
  6. import dev.colors.level.tile.Tile;
  7.  
  8. public class Physics {
  9.  
  10. private final float gravity = 0.0015f;
  11.  
  12. private boolean checkCollision(LevelObject obj, Tile[][] mapTiles) {
  13.  
  14. ArrayList<Tile> tiles = obj.getBoundingShape().getTilesOccupying(mapTiles);
  15.  
  16. for (Tile t : tiles) {
  17. if (t.getBoundingShape() != null) {
  18. if ((t.getBoundingShape()).checkCollision(obj.getBoundingShape())) {
  19. return true;
  20. }
  21. }
  22. }
  23. return false;
  24. }
  25.  
  26. private boolean isOnGroud(LevelObject obj, Tile[][] mapTiles) {
  27.  
  28. ArrayList<Tile> tiles = obj.getBoundingShape().getGroundTiles(mapTiles);
  29.  
  30. obj.getBoundingShape().movePosition(0, 1);
  31.  
  32. for (Tile t : tiles) {
  33. if (t.getBoundingShape() != null) {
  34. if ((t.getBoundingShape()).checkCollision(obj.getBoundingShape())) {
  35. obj.getBoundingShape().movePosition(0, -1);
  36. return true;
  37. }
  38. }
  39. }
  40.  
  41. // and obviously we have to move the object back up if we don't hit the
  42. // ground
  43. obj.getBoundingShape().movePosition(0, -1);
  44.  
  45. return false;
  46. }
  47.  
  48. private void handleCharacters(Level level, int delta) {
  49. for (dev.colors.character.Character c : level.getCharacters()) {
  50.  
  51. // and now decelerate the character if he is not moving anymore
  52. if (!c.isMoving()) {
  53. c.decelerate(delta);
  54. }
  55.  
  56. handleGameObject(c, level, delta);
  57. }
  58. }
  59.  
  60. public void handlePhysics(Level level, int delta) {
  61. handleCharacters(level, delta);
  62. }
  63.  
  64. private void handleGameObject(LevelObject obj, Level level, int delta) {
  65.  
  66. // first update the onGround of the object
  67. obj.setOnGround(isOnGroud(obj, level.getTiles()));
  68.  
  69. // now apply gravitational force if we are not on the ground or when we
  70. // are about to jump
  71. if (!obj.isOnGround() || obj.getYVelocity() < 0)
  72. obj.applyGravity(gravity * delta);
  73. else
  74. obj.setYVelocity(0);
  75.  
  76. // calculate how much we actually have to move
  77. float x_movement = obj.getXVelocity() * delta;
  78. float y_movement = obj.getYVelocity() * delta;
  79.  
  80. // we have to calculate the step we have to take
  81. float step_y = 0;
  82. float step_x = 0;
  83.  
  84. if (x_movement != 0) {
  85. step_y = Math.abs(y_movement) / Math.abs(x_movement);
  86. if (y_movement < 0)
  87. step_y = -step_y;
  88.  
  89. if (x_movement > 0)
  90. step_x = 1;
  91. else
  92. step_x = -1;
  93.  
  94. if ((step_y > 1 || step_y < -1) && step_y != 0) {
  95. step_x = Math.abs(step_x) / Math.abs(step_y);
  96. if (x_movement < 0)
  97. step_x = -step_x;
  98. if (y_movement < 0)
  99. step_y = -1;
  100. else
  101. step_y = 1;
  102. }
  103. } else if (y_movement != 0) {
  104. // if we only have vertical movement, we can just use a step of 1
  105. if (y_movement > 0)
  106. step_y = 1;
  107. else
  108. step_y = -1;
  109. }
  110.  
  111. // and then do little steps until we are done moving
  112. while (x_movement != 0 || y_movement != 0) {
  113.  
  114. // we first move in the x direction
  115. if (x_movement != 0) {
  116. // when we do a step, we have to update the amount we have to
  117. // move after this
  118. if ((x_movement > 0 && x_movement < step_x)
  119. || (x_movement > step_x && x_movement < 0)) {
  120. step_x = x_movement;
  121. x_movement = 0;
  122. } else
  123. x_movement -= step_x;
  124.  
  125. // then we move the object one step
  126. obj.setX(obj.getX() + step_x);
  127.  
  128. // if we collide with any of the bounding shapes of the tiles we
  129. // have to revert to our original position
  130. if (checkCollision(obj, level.getTiles())) {
  131.  
  132. // undo our step, and set the velocity and amount we still
  133. // have to move to 0, because we can't move in that
  134. // direction
  135. obj.setX(obj.getX() - step_x);
  136. obj.setXVelocity(0);
  137. x_movement = 0;
  138. }
  139.  
  140. }
  141. // same thing for the vertical, or y movement
  142. if (y_movement != 0) {
  143. if ((y_movement > 0 && y_movement < step_y)
  144. || (y_movement > step_y && y_movement < 0)) {
  145. step_y = y_movement;
  146. y_movement = 0;
  147. } else
  148. y_movement -= step_y;
  149.  
  150. obj.setY(obj.getY() + step_y);
  151.  
  152. if (checkCollision(obj, level.getTiles())) {
  153. obj.setY(obj.getY() - step_y);
  154. obj.setYVelocity(0);
  155. y_movement = 0;
  156. break;
  157. }
  158. }
  159. }
  160. }
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement