Changes in / [17dfb52:c65ef39] in galcon-client
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/example/helloandroid/Fleet.java
r17dfb52 rc65ef39 20 20 private int faction; 21 21 private boolean isNextToAPlanet; 22 private boolean dirChanged , isClockwise;23 22 private boolean dirChanged; 23 24 24 /* Optimising: pre-calculate paths */ 25 25 public Fleet(Planet source, Planet destination, int numShips, int faction) { … … 28 28 //Calculate initial coordinates and direction 29 29 if((destination.getX() - source.getX()) != 0){ 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 30 //line formula 31 slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY()); 32 33 xIntercept = destination.getY() - (slope*destination.getX()); 34 35 //direction 36 direction = Math.atan(slope); 37 38 //coordinates for all 4 coordinates 39 if((destination.getX() - source.getX()) < 0 ) 40 direction += Math.PI; 41 42 dblX = ((Math.cos(direction)*(source.radius + 10) + source.getX())); 43 dblY = ((Math.sin(direction)*(source.radius + 10) + source.getY())); 44 45 45 } else { 46 46 if((destination.getY() - source.getY()) > 0 ){ … … 53 53 dblY = source.getY() - source.radius - 10; 54 54 } 55 xIntercept = destination.getX(); 56 } 57 55 } 56 58 57 x = (int)dblX; 59 58 y = (int)dblY; 60 59 61 60 this.numShips = numShips; 62 61 this.faction = faction; … … 65 64 dirChanged = false; 66 65 } 67 68 66 67 69 68 public int getX() { 70 69 return x; … … 176 175 public void update(ArrayList<Planet> planets) { 177 176 int speed = 1; //pixels per move 178 double distance, tangentDirection , angle;177 double distance, tangentDirection; 179 178 Planet temp = null; 180 179 //is the ship going around a planet already … … 194 193 dblY += (Math.sin(direction)*speed); 195 194 dblX += (Math.cos(direction)*speed); 196 195 197 196 x = (int)dblX; 198 197 y = (int)dblY; 199 198 }else { 200 199 double radAngle = Math.atan(getSlope(temp.getX(), temp.getY(), dblX, dblY)); 201 //figure out which way to go clockwise or counter clockwise202 tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);203 angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction)));204 if (angle <= Math.PI/2)205 angle = Math.PI - angle;206 200 207 201 if(dblX < temp.getX()) 208 202 radAngle += Math.PI; 209 203 210 angle = radAngle + Math.PI/2;204 double angle = radAngle + Math.PI/2; 211 205 212 206 double diff = direction-angle; 213 207 214 if(diff > 0)215 isClockwise = false;216 else217 isClockwise = true;218 208 if(Math.abs(diff)>Math.PI/2) 219 209 direction = angle-Math.PI; 220 210 else 221 211 direction = angle; 222 212 223 213 dirChanged = true; 224 214 … … 234 224 //if so set isNextToAPlanet to false and move 235 225 //otherwise continue moving along the circumferenceds4 236 237 238 if(true){ 239 240 } else { 241 angle = speed/temp.radius; 242 if(isClockwise){ 243 dblX = ((Math.cos(direction + (Math.PI/2) - angle)*(temp.radius) + temp.getX())); 244 dblY = ((Math.sin(direction + (Math.PI/2) - angle)*(temp.radius) + temp.getY())); 245 direction = direction - (Math.PI/2); 246 } else { 247 dblX = ((Math.cos(direction - (Math.PI/2) + angle)*(temp.radius) + temp.getX())); 248 dblY = ((Math.sin(direction - (Math.PI/2) + angle)*(temp.radius) + temp.getY())); 249 direction = direction + (Math.PI/2); 250 } 251 } 252 } 253 } 254 226 227 } 228 229 230 } 231 255 232 // attack the destination planet 256 233 //after the method is called the fleet needs to removed … … 263 240 } 264 241 } 265 242 266 243 //helper functions 267 244 private double getDistanceBetween(double x1, double y1, double x2, double y2) { 268 245 return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2)); 269 246 } 270 247 271 248 private double getSlope(double x1, double y1, double x2, double y2) { 272 249 return ((y2 - y1)/(double)(x2 - x1));
Note:
See TracChangeset
for help on using the changeset viewer.