Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/com/example/helloandroid/Fleet.java

    r95509e1 r0986844  
    44
    55import android.graphics.Canvas;
    6 import android.graphics.Color;
    76import android.graphics.Paint;
    87import android.graphics.Path;
     
    2019        private int faction;
    2120        private boolean isNextToAPlanet;
    22         private boolean dirChanged;
    23        
     21        private boolean dirChanged, isClockwise;
     22
    2423        /* Optimising: pre-calculate paths */
    2524        public Fleet(Planet source, Planet destination, int numShips, int faction) {
    26                 source.setNumShips(source.getNumShips()-numShips);
    27                
    2825                //Calculate initial coordinates and direction
    2926                if((destination.getX() - source.getX()) != 0){
    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                
     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
    4542                } else {
    4643                        if((destination.getY() - source.getY()) > 0 ){
     
    5350                                dblY = source.getY() - source.radius - 10;
    5451                        }
     52                        xIntercept = destination.getX();
    5553                }
    56                
     54
    5755                x = (int)dblX;
    5856                y = (int)dblY;
    59                
     57
    6058                this.numShips = numShips;
    6159                this.faction = faction;
     
    6462                dirChanged = false;
    6563        }
    66        
    67                
     64
     65
    6866        public int getX() {
    6967                return x;
     
    136134        }
    137135
    138         public void draw(Canvas canvas, Paint linePaint) {     
     136        public void draw(Canvas canvas, Paint linePaint) {
     137                //Log.i("Gencon", "direction: "+direction);
     138                canvas.drawLine((float)x, (float)y, (float)(x+10*Math.cos(direction)), (float)(y+10*Math.sin(direction)), linePaint);
     139               
    139140                Path p = new Path();
    140141               
     
    144145                p.lineTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2)));
    145146               
    146                 int c, prevC = linePaint.getColor();
    147                
    148                 switch(faction) {
    149                 case 0:
    150                         c = Color.argb(255, 100, 100, 100);
    151                         break;
    152                 case 1:
    153                         c = Color.argb(255, 255, 0, 0);
    154                         break;
    155                 case 2:
    156                         c = Color.argb(255, 0, 180, 0);
    157                         break;
    158                 case 3:
    159                         c = Color.argb(255, 0, 0, 255);
    160                         break;
    161                 case 4:
    162                         c = Color.argb(255, 150, 150, 0);
    163                         break;
    164                 default:
    165                         c = prevC;
    166                 }
    167                
    168                 linePaint.setColor(c);
    169147               
    170148                canvas.drawPath(p, linePaint);
    171                
    172                 linePaint.setColor(prevC);
    173149        }
    174150
    175151        public void update(ArrayList<Planet> planets) {
    176152                int speed = 1; //pixels per move
    177                 double distance, tangentDirection;
     153                double distance, tangentDirection, angle;
    178154                Planet temp = null;
    179155                //is the ship going around a planet already
     
    193169                                dblY += (Math.sin(direction)*speed);
    194170                                dblX += (Math.cos(direction)*speed);
    195                                
     171
    196172                                x = (int)dblX;
    197173                                y = (int)dblY;
    198174                        }else {                         
    199175                                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;
    200181                               
    201182                                if(dblX < temp.getX())
    202183                                        radAngle += Math.PI;
    203184                               
    204                                 double angle = radAngle + Math.PI/2;
     185                                angle = radAngle + Math.PI/2;
    205186                               
    206187                                double diff = direction-angle;
    207188                               
     189                                if(diff > 0)
     190                                        isClockwise = false;
     191                                else
     192                                        isClockwise = true;
    208193                                if(Math.abs(diff)>Math.PI/2)
    209194                                        direction = angle-Math.PI;
    210195                                else
    211196                                        direction = angle;
    212                                
     197                                       
    213198                                dirChanged = true;
    214199                               
     
    224209                        //if so set isNextToAPlanet to false and move
    225210                        //otherwise continue moving along the circumferenceds4
    226                
     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                        }
    227227                }
    228                
    229                
    230         }
    231        
     228        }
     229
    232230        // attack the destination planet
    233231        //after the method is called the fleet needs to removed
     
    240238                }
    241239        }
    242        
     240
    243241        //helper functions
    244242        private double getDistanceBetween(double x1, double y1, double x2, double y2) {
    245243                return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
    246244        }
    247        
     245
    248246        private double getSlope(double x1, double y1, double x2, double y2) {
    249247                return ((y2 - y1)/(double)(x2 - x1));
Note: See TracChangeset for help on using the changeset viewer.