Changeset 0986844 in galcon-client for src/com/example/helloandroid/Fleet.java
- Timestamp:
- Jun 5, 2010, 7:02:40 PM (14 years ago)
- Branches:
- master
- Children:
- 17dfb52
- Parents:
- 61c4fbc
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/example/helloandroid/Fleet.java
r61c4fbc r0986844 19 19 private int faction; 20 20 private boolean isNextToAPlanet; 21 private boolean dirChanged ;22 21 private boolean dirChanged, isClockwise; 22 23 23 /* Optimising: pre-calculate paths */ 24 24 public Fleet(Planet source, Planet destination, int numShips, int faction) { 25 25 //Calculate initial coordinates and direction 26 26 if((destination.getX() - source.getX()) != 0){ 27 //line formula28 slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());29 30 xIntercept = destination.getY() - (slope*destination.getX());31 32 //direction33 direction = Math.atan(slope);34 35 //coordinates for all 4 coordinates36 if((destination.getX() - source.getX()) < 0 )37 direction += Math.PI;38 39 dblX = ((Math.cos(direction)*(source.radius + 10) + source.getX()));40 dblY = ((Math.sin(direction)*(source.radius + 10) + source.getY()));41 27 //line formula 28 slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY()); 29 30 xIntercept = destination.getY() - (slope*destination.getX()); 31 32 //direction 33 direction = Math.atan(slope); 34 35 //coordinates for all 4 coordinates 36 if((destination.getX() - source.getX()) < 0 ) 37 direction += Math.PI; 38 39 dblX = ((Math.cos(direction)*(source.radius + 10) + source.getX())); 40 dblY = ((Math.sin(direction)*(source.radius + 10) + source.getY())); 41 42 42 } else { 43 43 if((destination.getY() - source.getY()) > 0 ){ … … 50 50 dblY = source.getY() - source.radius - 10; 51 51 } 52 xIntercept = destination.getX(); 52 53 } 53 54 54 55 x = (int)dblX; 55 56 y = (int)dblY; 56 57 57 58 this.numShips = numShips; 58 59 this.faction = faction; … … 61 62 dirChanged = false; 62 63 } 63 64 64 65 65 66 public int getX() { 66 67 return x; … … 144 145 p.lineTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2))); 145 146 147 146 148 canvas.drawPath(p, linePaint); 147 149 } … … 149 151 public void update(ArrayList<Planet> planets) { 150 152 int speed = 1; //pixels per move 151 double distance, tangentDirection ;153 double distance, tangentDirection, angle; 152 154 Planet temp = null; 153 155 //is the ship going around a planet already … … 167 169 dblY += (Math.sin(direction)*speed); 168 170 dblX += (Math.cos(direction)*speed); 169 171 170 172 x = (int)dblX; 171 173 y = (int)dblY; 172 174 }else { 173 175 double radAngle = Math.atan(getSlope(temp.getX(), temp.getY(), dblX, dblY)); 176 //figure out which way to go clockwise or counter clockwise 177 tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2); 178 angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction))); 179 if (angle <= Math.PI/2) 180 angle = Math.PI - angle; 174 181 175 182 if(dblX < temp.getX()) 176 183 radAngle += Math.PI; 177 184 178 doubleangle = radAngle + Math.PI/2;185 angle = radAngle + Math.PI/2; 179 186 180 187 double diff = direction-angle; 181 188 189 if(diff > 0) 190 isClockwise = false; 191 else 192 isClockwise = true; 182 193 if(Math.abs(diff)>Math.PI/2) 183 194 direction = angle-Math.PI; 184 195 else 185 196 direction = angle; 186 197 187 198 dirChanged = true; 188 199 … … 198 209 //if so set isNextToAPlanet to false and move 199 210 //otherwise continue moving along the circumferenceds4 200 211 212 213 if(true){ 214 215 } else { 216 angle = speed/temp.radius; 217 if(isClockwise){ 218 dblX = ((Math.cos(direction + (Math.PI/2) - angle)*(temp.radius) + temp.getX())); 219 dblY = ((Math.sin(direction + (Math.PI/2) - angle)*(temp.radius) + temp.getY())); 220 direction = direction - (Math.PI/2); 221 } else { 222 dblX = ((Math.cos(direction - (Math.PI/2) + angle)*(temp.radius) + temp.getX())); 223 dblY = ((Math.sin(direction - (Math.PI/2) + angle)*(temp.radius) + temp.getY())); 224 direction = direction + (Math.PI/2); 225 } 226 } 201 227 } 202 203 204 } 205 228 } 229 206 230 // attack the destination planet 207 231 //after the method is called the fleet needs to removed … … 214 238 } 215 239 } 216 240 217 241 //helper functions 218 242 private double getDistanceBetween(double x1, double y1, double x2, double y2) { 219 243 return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2)); 220 244 } 221 245 222 246 private double getSlope(double x1, double y1, double x2, double y2) { 223 247 return ((y2 - y1)/(double)(x2 - x1));
Note:
See TracChangeset
for help on using the changeset viewer.