Changeset 17dfb52 in galcon-client for src/com/example/helloandroid/Fleet.java
- Timestamp:
- Jun 5, 2010, 8:02:26 PM (14 years ago)
- Branches:
- master
- Children:
- 1bc5794, 6ebf60f
- Parents:
- c65ef39 (diff), 0986844 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/example/helloandroid/Fleet.java
rc65ef39 r17dfb52 20 20 private int faction; 21 21 private boolean isNextToAPlanet; 22 private boolean dirChanged ;23 22 private boolean dirChanged, isClockwise; 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 //line formula31 slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());32 33 xIntercept = destination.getY() - (slope*destination.getX());34 35 //direction36 direction = Math.atan(slope);37 38 //coordinates for all 4 coordinates39 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 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 } 56 55 xIntercept = destination.getX(); 56 } 57 57 58 x = (int)dblX; 58 59 y = (int)dblY; 59 60 60 61 this.numShips = numShips; 61 62 this.faction = faction; … … 64 65 dirChanged = false; 65 66 } 66 67 67 68 68 69 public int getX() { 69 70 return x; … … 175 176 public void update(ArrayList<Planet> planets) { 176 177 int speed = 1; //pixels per move 177 double distance, tangentDirection ;178 double distance, tangentDirection, angle; 178 179 Planet temp = null; 179 180 //is the ship going around a planet already … … 193 194 dblY += (Math.sin(direction)*speed); 194 195 dblX += (Math.cos(direction)*speed); 195 196 196 197 x = (int)dblX; 197 198 y = (int)dblY; 198 199 }else { 199 200 double radAngle = Math.atan(getSlope(temp.getX(), temp.getY(), dblX, dblY)); 201 //figure out which way to go clockwise or counter clockwise 202 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; 200 206 201 207 if(dblX < temp.getX()) 202 208 radAngle += Math.PI; 203 209 204 doubleangle = radAngle + Math.PI/2;210 angle = radAngle + Math.PI/2; 205 211 206 212 double diff = direction-angle; 207 213 214 if(diff > 0) 215 isClockwise = false; 216 else 217 isClockwise = true; 208 218 if(Math.abs(diff)>Math.PI/2) 209 219 direction = angle-Math.PI; 210 220 else 211 221 direction = angle; 212 222 213 223 dirChanged = true; 214 224 … … 224 234 //if so set isNextToAPlanet to false and move 225 235 //otherwise continue moving along the circumferenceds4 226 227 } 228 229 230 } 231 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 232 255 // attack the destination planet 233 256 //after the method is called the fleet needs to removed … … 240 263 } 241 264 } 242 265 243 266 //helper functions 244 267 private double getDistanceBetween(double x1, double y1, double x2, double y2) { 245 268 return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2)); 246 269 } 247 270 248 271 private double getSlope(double x1, double y1, double x2, double y2) { 249 272 return ((y2 - y1)/(double)(x2 - x1));
Note:
See TracChangeset
for help on using the changeset viewer.