Advertisement
runewalsh

Rays 1

Apr 17th, 2020
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. public RayCollision playerRayCast(double angle, int maxRange) {
  2.     angle += player.getRotation().getAngle();
  3.     angle = Rotation.normalizeAngle(angle);
  4.  
  5.     // distances to the horizontal/vertical walls
  6.     RayCollision h = new RayCollision(), v = new RayCollision();
  7.     final Double2 coords = player.getCoords();
  8.  
  9.     // intersections with "horizontal" walls
  10.     if (angle != 0 && angle != Math.PI) {
  11.         // up and down are shuffled because of the coordinates system
  12.         boolean downwards = angle > 0 && angle < Math.PI;
  13.  
  14.         // coordinates of the first possible collision
  15.         double y = downwards ? Math.ceil(coords.y) : Math.floor(coords.y);
  16.         double x = coords.x + (y - coords.y) / Math.tan(angle);
  17.         Double2 current = new Double2(x, y);
  18.         int tilesDistance = 0;
  19.  
  20.         // every next collisions are placed evenly (intercept theorem)
  21.         Double2 step = new Double2((downwards ? 1 : -1) / Math.tan(angle), downwards ? 1 : -1);
  22.         while (tilesDistance <= maxRange) {
  23.             Int2 tileCoords = current.add(0, downwards ? 0.1 : -0.1).toInt();
  24.             if (!tileField.isEmpty(tileCoords)) {
  25.                 h.tile = tileCoords;
  26.                     h.hitPoint = downwards ? current.x % 1 : 1 - current.x % 1;
  27.                     break;
  28.             }
  29.             current = current.add(step);
  30.             tilesDistance++;
  31.         }
  32.         if (tilesDistance <= maxRange) {
  33.             h.d = Math.abs((current.y - coords.y) / Math.sin(angle));
  34.         }
  35.     }
  36.  
  37.     // intersections with "vertical" walls
  38.     if (angle != -Math.PI / 2 && angle != Math.PI / 2) {
  39.         boolean right = angle > -Math.PI / 2 && angle < Math.PI / 2;
  40.  
  41.         // first collision
  42.         double x = right ? Math.ceil(coords.x) : Math.floor(coords.x);
  43.         double y = coords.y + (x - coords.x) * Math.tan(angle);
  44.         Double2 current = new Double2(x, y);
  45.         int tilesDistance = 0;
  46.  
  47.         // next collisions
  48.         Double2 step = new Double2(right ? 1 : -1, (right ? 1 : -1) * Math.tan(angle));
  49.         while (tilesDistance <= maxRange) {
  50.             Int2 tileCoords = current.add(right ? 0.1 : -0.1, 0).toInt();
  51.             if (!tileField.isEmpty(tileCoords)) {
  52.                 v.tile = tileCoords;
  53.                 v.hitPoint = right ? 1 - current.y % 1 : current.y % 1;
  54.                 break;
  55.             }
  56.             current = current.add(step);
  57.             tilesDistance++;
  58.         }
  59.         if (tilesDistance <= maxRange) {
  60.             v.d = Math.abs((current.x - coords.x) / Math.cos(angle));
  61.         }
  62.     }
  63.     if (h.d < v.d) {
  64.         return h;
  65.     } else {
  66.         return v;
  67.     }
  68. }
  69.  
  70. for (int i = 0; i < stripsCount; i++) {
  71.     // multiplying by cosine scales the distances an removes the fish eye effect
  72.     WalkingGameplay.RayCollision rayCollision = gameplay.playerRayCast(angle, 10);
  73.     angle += FOV / stripsCount;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement