import java.util.HashMap; /** * Ball Class * - Tracking ball as well as basic example of Voronoi Diagram * to work out goal shots and passing. Provides hashmap to hold all the * ball position data and also has methods to work out distance for * goals and passing * @author Akbar Sherwani * @date 27th November * @version 0.1 */ public class Ball { public double posx; // Position x public double posy; // Position y public double velx; // Velocity x public double vely; // Velocity y private HashMap touches; // Touches Hashmap for ball pos /** * Constructor * Intialises ball class */ public Ball() { // Initialise posx = 0.0; posy = 0.0; velx = 0.0; vely = 0.0; touches = new HashMap(); } /** * Set * Sets ball position and velocity * @param posx sets postion x * @param posy sets postion y * @param velx sets velocity x * @param vely sets velocity y */ public void set(double posx, double posy, double velx, double vely) { this.posx = posx; this.posy = posy; this.velx = velx; this.vely = vely; } /** * Ball kicked * Sets ball position and velocity into a position class * which is then added to the touches hashmap * @param player player number */ public void BallKicked(int player) { //get ball position and add to hashmap Position pos = new Position(posx, posy); touches.put(player, pos); } /** * Ball passed * Find pass distance * @param playera player who passed * @param playerb player who recieved ball * @return goal distance string */ public int BallPass(int playera, int playerb) { Position posa = (Position) touches.get(playera); Position posb = (Position) touches.get(playerb); //Get Distance between two positions double passl = Distance(posa, posb); int dis = (int) passl; return dis; } /** * Ball shot * Find shot distance * @param playera player who shot * @param playerb goalkeeper who saved * @return goal distance string */ public int BallShot(int playera, int playerb) { Position posa = (Position) touches.get(playera); Position posb = (Position) touches.get(playerb); //Get Distance between two positions double passl = Distance(posa, posb); int dis = (int) passl; return dis; } /** * Goal shot * Find shot distance to center of goal * @param playera player who shot * @return goal distance string */ public int BallGoal(int playera) { Position posa = (Position) touches.get(playera); Position posb; // Depends on which goal posts are you aiming for below // Or remove all negatives if(playera > 11) { posb = new Position(-53,0); } else { posb = new Position(53,0); } //Get Distance between two positions double passl = Distance(posa, posb); int dis = (int) passl; return dis; } /** * Goal Player Position * Find shot distance to center of goal * @param playera player who shot * @return goal distance string */ public Position BallGoalPlayer(int playera) { Position posa = (Position) touches.get(playera); return posa; } /** * Ball Distance * Find pass distance * @param a position of each * @param b position of each * @return distance between the two points */ public double Distance(Position a, Position b) { if(a != null || b != null) { double diffx = Math.abs(a.getPosx() - b.getPosx()); double diffy = Math.abs(a.getPosy() - b.getPosy()); //System.out.println(pythag(diffx, diffy)); return pythag(diffx, diffy); } else { return 0; } } /** * Pythag * Find pass distance * @param a position of each * @param b position of each * @return pythag value of the two points */ public double pythag(double a, double b) { double absa,absb; absa=Math.abs(a); absb=Math.abs(b); if(absa > absb) return absa*Math.sqrt(1.+Math.pow((absb/absa),2)); else if(absb==0.) return 0.; else return absb*Math.sqrt(1.+Math.pow((absa/absb),2)); } /** * Get method for posx * @return position x of the ball */ public double getPosx() { return posx; } /** * Get method for posy * @return position y of the ball */ public double getPosy() { return posy; } /** * Get final ball position * @return position x of the ball */ public Position getBallPos() { Position pos = new Position(posx, posy); return pos; } }