Changeset 0986844 in galcon-client


Ignore:
Timestamp:
Jun 5, 2010, 7:02:40 PM (14 years ago)
Author:
Zero Cool <devnull@…>
Branches:
master
Children:
17dfb52
Parents:
61c4fbc
Message:

Some more updates, almost done going round the planet

File:
1 edited

Legend:

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

    r61c4fbc r0986844  
    1919        private int faction;
    2020        private boolean isNextToAPlanet;
    21         private boolean dirChanged;
    22        
     21        private boolean dirChanged, isClockwise;
     22
    2323        /* Optimising: pre-calculate paths */
    2424        public Fleet(Planet source, Planet destination, int numShips, int faction) {
    2525                //Calculate initial coordinates and direction
    2626                if((destination.getX() - source.getX()) != 0){
    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                
     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
    4242                } else {
    4343                        if((destination.getY() - source.getY()) > 0 ){
     
    5050                                dblY = source.getY() - source.radius - 10;
    5151                        }
     52                        xIntercept = destination.getX();
    5253                }
    53                
     54
    5455                x = (int)dblX;
    5556                y = (int)dblY;
    56                
     57
    5758                this.numShips = numShips;
    5859                this.faction = faction;
     
    6162                dirChanged = false;
    6263        }
    63        
    64                
     64
     65
    6566        public int getX() {
    6667                return x;
     
    144145                p.lineTo((float)(x+5*Math.cos(direction+Math.PI/2)), (float)(y+5*Math.sin(direction+Math.PI/2)));
    145146               
     147               
    146148                canvas.drawPath(p, linePaint);
    147149        }
     
    149151        public void update(ArrayList<Planet> planets) {
    150152                int speed = 1; //pixels per move
    151                 double distance, tangentDirection;
     153                double distance, tangentDirection, angle;
    152154                Planet temp = null;
    153155                //is the ship going around a planet already
     
    167169                                dblY += (Math.sin(direction)*speed);
    168170                                dblX += (Math.cos(direction)*speed);
    169                                
     171
    170172                                x = (int)dblX;
    171173                                y = (int)dblY;
    172174                        }else {                         
    173175                                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;
    174181                               
    175182                                if(dblX < temp.getX())
    176183                                        radAngle += Math.PI;
    177184                               
    178                                 double angle = radAngle + Math.PI/2;
     185                                angle = radAngle + Math.PI/2;
    179186                               
    180187                                double diff = direction-angle;
    181188                               
     189                                if(diff > 0)
     190                                        isClockwise = false;
     191                                else
     192                                        isClockwise = true;
    182193                                if(Math.abs(diff)>Math.PI/2)
    183194                                        direction = angle-Math.PI;
    184195                                else
    185196                                        direction = angle;
    186                                
     197                                       
    187198                                dirChanged = true;
    188199                               
     
    198209                        //if so set isNextToAPlanet to false and move
    199210                        //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                        }
    201227                }
    202                
    203                
    204         }
    205        
     228        }
     229
    206230        // attack the destination planet
    207231        //after the method is called the fleet needs to removed
     
    214238                }
    215239        }
    216        
     240
    217241        //helper functions
    218242        private double getDistanceBetween(double x1, double y1, double x2, double y2) {
    219243                return Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
    220244        }
    221        
     245
    222246        private double getSlope(double x1, double y1, double x2, double y2) {
    223247                return ((y2 - y1)/(double)(x2 - x1));
Note: See TracChangeset for help on using the changeset viewer.