Changeset 61c4fbc in galcon-client for src/com/example/helloandroid/Fleet.java
- Timestamp:
- Jun 5, 2010, 5:08:40 PM (15 years ago)
- Branches:
- master
- Children:
- 0986844, 95509e1
- Parents:
- 9ef6f68
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/com/example/helloandroid/Fleet.java
r9ef6f68 r61c4fbc 2 2 3 3 import java.util.ArrayList; 4 5 import android.graphics.Canvas; 6 import android.graphics.Paint; 7 import android.graphics.Path; 8 import android.util.Log; 4 9 5 10 public class Fleet { … … 14 19 private int faction; 15 20 private boolean isNextToAPlanet; 21 private boolean dirChanged; 16 22 17 23 /* Optimising: pre-calculate paths */ … … 21 27 //line formula 22 28 slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY()); 29 23 30 xIntercept = destination.getY() - (slope*destination.getX()); 24 31 … … 52 59 this.destination = destination; 53 60 this.isNextToAPlanet = false; 61 dirChanged = false; 54 62 } 55 63 … … 125 133 } 126 134 127 135 public void draw(Canvas canvas, Paint linePaint) { 136 //Log.i("Gencon", "direction: "+direction); 137 canvas.drawLine((float)x, (float)y, (float)(x+10*Math.cos(direction)), (float)(y+10*Math.sin(direction)), linePaint); 138 139 Path p = new Path(); 140 141 p.moveTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2))); 142 p.lineTo((float)(x+5*Math.cos(direction-Math.PI/2)), (float)(y+5*Math.sin(direction-Math.PI/2))); 143 p.lineTo((float)(x+10*Math.cos(direction)), (float)(y+10*Math.sin(direction))); 144 p.lineTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2))); 145 146 canvas.drawPath(p, linePaint); 147 } 128 148 129 149 public void update(ArrayList<Planet> planets) { 130 150 int speed = 1; //pixels per move 131 double distance, tangentDirection , angle;151 double distance, tangentDirection; 132 152 Planet temp = null; 133 153 //is the ship going around a planet already … … 144 164 } 145 165 //if temp planet is not picked move along the direction by #speed 146 if(temp == null ){166 if(temp == null || dirChanged) { 147 167 dblY += (Math.sin(direction)*speed); 148 168 dblX += (Math.cos(direction)*speed); … … 150 170 x = (int)dblX; 151 171 y = (int)dblY; 152 } else 153 //otherwise 154 { 172 }else { 173 double radAngle = Math.atan(getSlope(temp.getX(), temp.getY(), dblX, dblY)); 174 175 if(dblX < temp.getX()) 176 radAngle += Math.PI; 177 178 double angle = radAngle + Math.PI/2; 179 180 double diff = direction-angle; 181 182 if(Math.abs(diff)>Math.PI/2) 183 direction = angle-Math.PI; 184 else 185 direction = angle; 186 187 dirChanged = true; 188 155 189 //figure out which way to go clockwise or counter clockwise 156 tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2);190 /*tangentDirection = (Math.atan(getSlope(temp.getX(),temp.getY(),dblX,dblY))) + (Math.PI/2); 157 191 angle = Math.atan((Math.tan(tangentDirection) - Math.tan(direction))/(1 + Math.tan(tangentDirection)*Math.tan(direction))); 158 192 if (angle <= Math.PI/2) 159 angle = Math.PI - angle; 193 angle = Math.PI - angle;*/ 160 194 //get next point and the direction and set it 161 195 } … … 182 216 183 217 //helper functions 184 private double getDistanceBetween(double x1, double y1, double x2, double y2) 185 { 218 private double getDistanceBetween(double x1, double y1, double x2, double y2) { 186 219 return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2)); 187 220 } 188 221 189 private double getSlope(double x1, double y1, double x2, double y2) 190 { 191 return ((y1 - y1)/(x2 - x1)); 222 private double getSlope(double x1, double y1, double x2, double y2) { 223 return ((y2 - y1)/(double)(x2 - x1)); 192 224 } 193 225 }
Note:
See TracChangeset
for help on using the changeset viewer.