Changeset 9ef6f68 in galcon-client for src/com/example/helloandroid/Fleet.java
- Timestamp:
- Jun 1, 2010, 2:12:31 AM (14 years ago)
- Branches:
- master
- Children:
- 61c4fbc
- Parents:
- c27abf4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/example/helloandroid/Fleet.java
rc27abf4 r9ef6f68 1 1 package com.example.helloandroid; 2 3 import java.util.ArrayList; 2 4 3 5 public class Fleet { … … 11 13 private int numShips; 12 14 private int faction; 15 private boolean isNextToAPlanet; 13 16 14 17 /* Optimising: pre-calculate paths */ … … 17 20 if((destination.getX() - source.getX()) != 0){ 18 21 //line formula 19 slope = ((source.getY() - destination.getY())/(source.getX() - destination.getX()));22 slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY()); 20 23 xIntercept = destination.getY() - (slope*destination.getX()); 21 24 22 25 //direction 23 direction = 1/Math.tan(slope);26 direction = Math.atan(slope); 24 27 25 28 //coordinates for all 4 coordinates … … 48 51 this.faction = faction; 49 52 this.destination = destination; 53 this.isNextToAPlanet = false; 50 54 } 51 55 … … 123 127 124 128 125 public void update( ) {129 public void update(ArrayList<Planet> planets) { 126 130 int speed = 1; //pixels per move 131 double distance, tangentDirection, angle; 132 Planet temp = null; 133 //is the ship going around a planet already 134 if(!isNextToAPlanet){ 135 /*looks through all the planets to figure out if 136 the ship's path is about to intersect a planet*/ 137 for(Planet p: planets){ 138 //if two point of intersection are found save planet 139 distance = getDistanceBetween(dblX,dblY,p.getX(),p.getY()); 140 if(distance <= p.radius){ 141 temp = p; 142 break; 143 } 144 } 145 //if temp planet is not picked move along the direction by #speed 146 if(temp == null){ 147 dblY += (Math.sin(direction)*speed); 148 dblX += (Math.cos(direction)*speed); 149 150 x = (int)dblX; 151 y = (int)dblY; 152 } else 153 //otherwise 154 { 155 //figure out which way to go clockwise or counter clockwise 156 tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2); 157 angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction))); 158 if (angle <= Math.PI/2) 159 angle = Math.PI - angle; 160 //get next point and the direction and set it 161 } 162 } else { 163 //can you reach the center of the planet by following this direction 164 //if so set isNextToAPlanet to false and move 165 //otherwise continue moving along the circumferenceds4 166 167 } 168 169 127 170 } 128 171 … … 137 180 } 138 181 } 182 183 //helper functions 184 private double getDistanceBetween(double x1, double y1, double x2, double y2) 185 { 186 return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2)); 187 } 188 189 private double getSlope(double x1, double y1, double x2, double y2) 190 { 191 return ((y1 - y1)/(x2 - x1)); 192 } 139 193 }
Note:
See TracChangeset
for help on using the changeset viewer.