Changeset 9ef6f68 in galcon-client


Ignore:
Timestamp:
Jun 1, 2010, 2:12:31 AM (14 years ago)
Author:
Zero Cool <devnull@…>
Branches:
master
Children:
61c4fbc
Parents:
c27abf4
Message:

Chugging along with path finding

File:
1 edited

Legend:

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

    rc27abf4 r9ef6f68  
    11package com.example.helloandroid;
     2
     3import java.util.ArrayList;
    24
    35public class Fleet {
     
    1113        private int numShips;
    1214        private int faction;
     15        private boolean isNextToAPlanet;
    1316       
    1417        /* Optimising: pre-calculate paths */
     
    1720                if((destination.getX() - source.getX()) != 0){
    1821                //line formula
    19                 slope = ((source.getY() - destination.getY())/(source.getX() - destination.getX()));
     22                slope = getSlope(source.getX(),source.getY(),destination.getX(),destination.getY());
    2023                xIntercept = destination.getY() - (slope*destination.getX());
    2124               
    2225                //direction
    23                 direction = 1/Math.tan(slope);
     26                direction = Math.atan(slope);
    2427               
    2528                //coordinates for all 4 coordinates
     
    4851                this.faction = faction;
    4952                this.destination = destination;
     53                this.isNextToAPlanet = false;
    5054        }
    5155       
     
    123127
    124128
    125         public void update() {
     129        public void update(ArrayList<Planet> planets) {
    126130                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               
    127170        }
    128171       
     
    137180                }
    138181        }
     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        }
    139193}
Note: See TracChangeset for help on using the changeset viewer.