Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.08 KB | None | 0 0
  1.     /**
  2.      * Assume an object (ball) moving from line.start() to line.end().
  3.      * If this object will not collide with any of the collidables.
  4.      * in this collection, return null. Else, return the information.
  5.      * about the closest collision that is going to occur.
  6.      *
  7.      * @param trajectory - the given object trejectory
  8.      * @return - the collision info (if there is) null otherwise.
  9.      */
  10.     public CollisionInfo getClosestCollision(Line trajectory) {
  11.  
  12.         // there aren't any collidables to collide with
  13.         if (this.listOfCollidables.isEmpty()) {
  14.             return null;
  15.         }
  16.  
  17.         /*
  18.          * Begins with setting the closest point to be the first one
  19.          * from the listOfCollidables which it will collide with
  20.          */
  21.         Collidable temp;
  22.         Point closest;
  23.         int j = 0;
  24.  
  25.         //getting the first collision in the list
  26.         do {
  27.             temp = (Collidable) listOfCollidables.get(j);
  28.             closest = trajectory.closestIntersectionToStartOfLine(temp.getCollisionRectangle());
  29.             j++;
  30.         } while ((closest == null) && (j < listOfCollidables.size()));
  31.  
  32.         // no collision found
  33.         if ((closest == null)) {
  34.             return null;
  35.         }
  36.  
  37.         // finding the next collision in list (if there is any other)
  38.         Collidable object = (Collidable) listOfCollidables.get(j - 1);
  39.         for (int i = j; i < this.listOfCollidables.size(); i++) {
  40.  
  41.             //Makes a temp point of next collision point
  42.             temp = (Collidable) listOfCollidables.get(i);
  43.             Point tempPoint = trajectory.closestIntersectionToStartOfLine(temp.getCollisionRectangle());
  44.  
  45.             // compares which is closer to the trajectory start point
  46.  
  47.             if ((tempPoint != null) && (this.checkDouble(trajectory.getStart().distanceTo(closest)
  48.                     , trajectory.getStart().distanceTo(tempPoint)) == 1)) {
  49.  
  50.                 closest = tempPoint;
  51.                 object = temp;
  52.             }
  53.         }
  54.  
  55.         // return the closest collisionInfo from the list
  56.         return new CollisionInfo(closest, object);
  57.     }
  58.  
  59.     /**
  60.      * checking the ratio between 2 double numbers .
  61.      * return 0 if equals (with 0.001 difference).
  62.      * return 1 if the first is bigger the thw second.
  63.      * return -1 if the second is bigger.
  64.      *
  65.      * @param first  - the first number.
  66.      * @param second - the second.
  67.      * @return - the ratio
  68.      */
  69.     public int checkDouble(double first, double second) {
  70.         if (Math.abs(first - second) < 0.001) {
  71.             return 0;
  72.         } else if (first > second) {
  73.             return 1;
  74.         }
  75.         return -1;
  76.     }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. public Velocity hit(Ball hitter, Point collisionPoint, Velocity currentVelocity) { //TODO where do i send hitter?
  92.  
  93.         // changing the number of hits left to the block.
  94.         if (this.hit > 0) {
  95.             this.hit--;
  96.         }
  97.  
  98.         this.coll = collisionPoint;
  99.  
  100.         // creating the block line's
  101.         Line topLine = new Line(this.getCollisionRectangle().getUpperLeft(),
  102.                 this.getCollisionRectangle().getUpperRight());
  103.         Line bottomLine = new Line(this.getCollisionRectangle().getBottomLeft(),
  104.                 this.getCollisionRectangle().getBottomRight());
  105.         Line leftLine = new Line(this.getCollisionRectangle().getUpperLeft(),
  106.                 this.getCollisionRectangle().getBottomLeft());
  107.         Line rightLine = new Line(this.getCollisionRectangle().getUpperRight(),
  108.                 this.getCollisionRectangle().getBottomRight());
  109.  
  110.         Velocity newVel = new Velocity(currentVelocity.getDx(), currentVelocity.getDy());
  111.  
  112.         // checking if the collision is on the corner
  113.         if (cornerCheck(collisionPoint, topLine.getStart())
  114.                 || cornerCheck(collisionPoint, topLine.getEnd())
  115.                 || cornerCheck(collisionPoint, bottomLine.getStart())
  116.                 || cornerCheck(collisionPoint, bottomLine.getEnd())) {
  117.             newVel.setDy(-1 * currentVelocity.getDy());
  118.             newVel.setDx(-1 * currentVelocity.getDx());
  119.             return newVel;
  120.         }
  121.  
  122.         //If ball is hitting from top
  123.         if (rangeX(collisionPoint, topLine.getStart(), topLine.getEnd())) {
  124.             newVel.setDy(-1 * currentVelocity.getDy());
  125.  
  126.             //If ball is hitting from bottom
  127.         } else if ((rangeX(collisionPoint, bottomLine.getStart(), bottomLine.getEnd()))) {
  128.             newVel.setDy(-1 * currentVelocity.getDy());
  129.         }
  130.  
  131.         //if ball is hitting from left
  132.         if (rangeY(collisionPoint, leftLine.getStart(), leftLine.getEnd())) {
  133.             newVel.setDx(-1 * currentVelocity.getDx());
  134.  
  135.             //If ball is hitting from right
  136.         } else if (rangeY(collisionPoint, rightLine.getStart(), rightLine.getEnd())) {
  137.             newVel.setDx(-1 * currentVelocity.getDx());
  138.         }
  139.         this.notifyHit(hitter);
  140.         return newVel;
  141.     }
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  /**
  148.      * the function that move's the ball on the screen.
  149.      * according to the ball's surface and velocity.
  150.      */
  151.     public void moveOneStep() {
  152.  
  153.         // getting the ball's trajectory and speed
  154.         Line trajectory = this.trajectory();
  155.         Velocity v = this.getVelocity();
  156.  
  157.         // checking if there is a collision in his trajectory
  158.         CollisionInfo collision = this.getEnvironment().getClosestCollision(trajectory);
  159.  
  160.         // if there is then get a new speed
  161.         if (collision != null) {
  162.  
  163.             if ((this.checkDouble(this.point.distanceTo(collision.getCollisionPoint()), this.getSize()) == 0)
  164.                     || (this.checkDouble(this.point.distanceTo(collision.getCollisionPoint()), this.getSize()) == -1)) {
  165.  
  166.                 v = collision.collisionObject().hit(this, collision.getCollisionPoint(), this.getVelocity());
  167.                 this.setVelocity(v.getDx(), v.getDy());
  168.             }
  169.  
  170.             // if there is'nt then move according to his speed
  171.         } else {
  172.  
  173.             this.point = this.getVelocity().applyToPoint(this.point);
  174.         }
  175.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement