Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Assume an object (ball) moving from line.start() to line.end().
- * If this object will not collide with any of the collidables.
- * in this collection, return null. Else, return the information.
- * about the closest collision that is going to occur.
- *
- * @param trajectory - the given object trejectory
- * @return - the collision info (if there is) null otherwise.
- */
- public CollisionInfo getClosestCollision(Line trajectory) {
- // there aren't any collidables to collide with
- if (this.listOfCollidables.isEmpty()) {
- return null;
- }
- /*
- * Begins with setting the closest point to be the first one
- * from the listOfCollidables which it will collide with
- */
- Collidable temp;
- Point closest;
- int j = 0;
- //getting the first collision in the list
- do {
- temp = (Collidable) listOfCollidables.get(j);
- closest = trajectory.closestIntersectionToStartOfLine(temp.getCollisionRectangle());
- j++;
- } while ((closest == null) && (j < listOfCollidables.size()));
- // no collision found
- if ((closest == null)) {
- return null;
- }
- // finding the next collision in list (if there is any other)
- Collidable object = (Collidable) listOfCollidables.get(j - 1);
- for (int i = j; i < this.listOfCollidables.size(); i++) {
- //Makes a temp point of next collision point
- temp = (Collidable) listOfCollidables.get(i);
- Point tempPoint = trajectory.closestIntersectionToStartOfLine(temp.getCollisionRectangle());
- // compares which is closer to the trajectory start point
- if ((tempPoint != null) && (this.checkDouble(trajectory.getStart().distanceTo(closest)
- , trajectory.getStart().distanceTo(tempPoint)) == 1)) {
- closest = tempPoint;
- object = temp;
- }
- }
- // return the closest collisionInfo from the list
- return new CollisionInfo(closest, object);
- }
- /**
- * checking the ratio between 2 double numbers .
- * return 0 if equals (with 0.001 difference).
- * return 1 if the first is bigger the thw second.
- * return -1 if the second is bigger.
- *
- * @param first - the first number.
- * @param second - the second.
- * @return - the ratio
- */
- public int checkDouble(double first, double second) {
- if (Math.abs(first - second) < 0.001) {
- return 0;
- } else if (first > second) {
- return 1;
- }
- return -1;
- }
- public Velocity hit(Ball hitter, Point collisionPoint, Velocity currentVelocity) { //TODO where do i send hitter?
- // changing the number of hits left to the block.
- if (this.hit > 0) {
- this.hit--;
- }
- this.coll = collisionPoint;
- // creating the block line's
- Line topLine = new Line(this.getCollisionRectangle().getUpperLeft(),
- this.getCollisionRectangle().getUpperRight());
- Line bottomLine = new Line(this.getCollisionRectangle().getBottomLeft(),
- this.getCollisionRectangle().getBottomRight());
- Line leftLine = new Line(this.getCollisionRectangle().getUpperLeft(),
- this.getCollisionRectangle().getBottomLeft());
- Line rightLine = new Line(this.getCollisionRectangle().getUpperRight(),
- this.getCollisionRectangle().getBottomRight());
- Velocity newVel = new Velocity(currentVelocity.getDx(), currentVelocity.getDy());
- // checking if the collision is on the corner
- if (cornerCheck(collisionPoint, topLine.getStart())
- || cornerCheck(collisionPoint, topLine.getEnd())
- || cornerCheck(collisionPoint, bottomLine.getStart())
- || cornerCheck(collisionPoint, bottomLine.getEnd())) {
- newVel.setDy(-1 * currentVelocity.getDy());
- newVel.setDx(-1 * currentVelocity.getDx());
- return newVel;
- }
- //If ball is hitting from top
- if (rangeX(collisionPoint, topLine.getStart(), topLine.getEnd())) {
- newVel.setDy(-1 * currentVelocity.getDy());
- //If ball is hitting from bottom
- } else if ((rangeX(collisionPoint, bottomLine.getStart(), bottomLine.getEnd()))) {
- newVel.setDy(-1 * currentVelocity.getDy());
- }
- //if ball is hitting from left
- if (rangeY(collisionPoint, leftLine.getStart(), leftLine.getEnd())) {
- newVel.setDx(-1 * currentVelocity.getDx());
- //If ball is hitting from right
- } else if (rangeY(collisionPoint, rightLine.getStart(), rightLine.getEnd())) {
- newVel.setDx(-1 * currentVelocity.getDx());
- }
- this.notifyHit(hitter);
- return newVel;
- }
- /**
- * the function that move's the ball on the screen.
- * according to the ball's surface and velocity.
- */
- public void moveOneStep() {
- // getting the ball's trajectory and speed
- Line trajectory = this.trajectory();
- Velocity v = this.getVelocity();
- // checking if there is a collision in his trajectory
- CollisionInfo collision = this.getEnvironment().getClosestCollision(trajectory);
- // if there is then get a new speed
- if (collision != null) {
- if ((this.checkDouble(this.point.distanceTo(collision.getCollisionPoint()), this.getSize()) == 0)
- || (this.checkDouble(this.point.distanceTo(collision.getCollisionPoint()), this.getSize()) == -1)) {
- v = collision.collisionObject().hit(this, collision.getCollisionPoint(), this.getVelocity());
- this.setVelocity(v.getDx(), v.getDy());
- }
- // if there is'nt then move according to his speed
- } else {
- this.point = this.getVelocity().applyToPoint(this.point);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement