Advertisement
dermetfan

final collision detection

Oct 13th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. public void update(float delta) {
  2.     // apply gravity
  3.     velocity.y -= gravity * delta;
  4.  
  5.     // clamp velocity
  6.     if(velocity.y > speed)
  7.         velocity.y = speed;
  8.     else if(velocity.y < -speed)
  9.         velocity.y = -speed;
  10.  
  11.     // save old position
  12.     float oldX = getX(), oldY = getY();
  13.     boolean collisionX = false, collisionY = false;
  14.  
  15.     // move on x
  16.     setX(getX() + velocity.x * delta);
  17.  
  18.     // calculate the increment for step in #collidesLeft() and #collidesRight()
  19.     increment = collisionLayer.getTileWidth();
  20.     increment = getWidth() < increment ? getWidth() / 2 : increment / 2;
  21.  
  22.     if(velocity.x < 0) // going left
  23.         collisionX = collidesLeft();
  24.     else if(velocity.x > 0) // going right
  25.         collisionX = collidesRight();
  26.  
  27.     // react to x collision
  28.     if(collisionX) {
  29.         setX(oldX);
  30.         velocity.x = 0;
  31.     }
  32.  
  33.     // move on y
  34.     setY(getY() + velocity.y * delta * 5f);
  35.  
  36.     // calculate the increment for step in #collidesBottom() and #collidesTop()
  37.     increment = collisionLayer.getTileHeight();
  38.     increment = getHeight() < increment ? getHeight() / 2 : increment / 2;
  39.  
  40.     if(velocity.y < 0) // going down
  41.         canJump = collisionY = collidesBottom();
  42.     else if(velocity.y > 0) // going up
  43.         collisionY = collidesTop();
  44.  
  45.     // react to y collision
  46.     if(collisionY) {
  47.         setY(oldY);
  48.         velocity.y = 0;
  49.     }
  50.  
  51.     // update animation
  52.     animationTime += delta;
  53.     setRegion(velocity.x < 0 ? left.getKeyFrame(animationTime) : velocity.x > 0 ? right.getKeyFrame(animationTime) : still.getKeyFrame(animationTime));
  54. }
  55.  
  56. private boolean isCellBlocked(float x, float y) {
  57.     Cell cell = collisionLayer.getCell((int) (x / collisionLayer.getTileWidth()), (int) (y / collisionLayer.getTileHeight()));
  58.     return cell != null && cell.getTile() != null && cell.getTile().getProperties().containsKey(blockedKey);
  59. }
  60.  
  61. public boolean collidesRight() {
  62.     for(float step = 0; step <= getHeight(); step += increment)
  63.         if(isCellBlocked(getX() + getWidth(), getY() + step))
  64.             return true;
  65.     return false;
  66. }
  67.  
  68. public boolean collidesLeft() {
  69.     for(float step = 0; step <= getHeight(); step += increment)
  70.         if(isCellBlocked(getX(), getY() + step))
  71.             return true;
  72.     return false;
  73. }
  74.  
  75. public boolean collidesTop() {
  76.     for(float step = 0; step <= getWidth(); step += increment)
  77.         if(isCellBlocked(getX() + step, getY() + getHeight()))
  78.             return true;
  79.     return false;
  80. }
  81.  
  82. public boolean collidesBottom() {
  83.     for(float step = 0; step <= getWidth(); step += increment)
  84.         if(isCellBlocked(getX() + step, getY()))
  85.             return true;
  86.     return false;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement