0

Hey guys, its been quite a while since ive posted here :-(

Im making a game in java (nope not for school, just for fun)

Anyway, ive created some logic for an enemySuicideBomber(spaceship game, prolly not what your thinking) to select the closest location. It works for the most part, but there are times when a destination other then the closest one is selected, and ive spent hours trying to figure out why. Below is my source code for the enemySuicideBomber, if you have any ideas id love to hear them, im pulling my hair out. Also if you need any other source code let me know, i just cant post it all, its thousands of lines :-).

```
package game;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
/**
*
* @author Fedhell
*/
public class EnemySuicideBomber
{
double x;
double y;
int angle;
double destinationx =20000;
double destinationy = 20000;
double deltaX;
double deltaY;
double speed =1;
boolean destroy = false;
int destinationHoroz;
int destinationVert;
public EnemySuicideBomber()
{
Random tempRandomGenerator = new Random();
int spawnRand = tempRandomGenerator.nextInt(4)+1;
System.out.println ("Creating an instance of SuicideBomber, which spawned at " + spawnRand);
if (spawnRand == 1)//left
{
x = 0;
y =300;
}
if (spawnRand == 2)//top
{
x = 400;
y =0;
}
if (spawnRand == 3)//right
{
x = 800;
y =300;
}
if (spawnRand == 4)//bottom
{
x = 400;
y =600;
}
}
void setDestination (Graphics g, GameObject arrayOfGameObjects[][])
{
//destinationx =0;
//destinationy =0;
double tempx = 10000;
double tempy = 10000;
double currentshortestdistance = 100000;
for (int xx =0; xx<32; xx++)
{
for (int yy =0; yy<24; yy++)
{
if (arrayOfGameObjects[xx][yy] != null)
{
//
double tempcalcx = tempx - arrayOfGameObjects[xx][yy].getX();
double tempcalcy = tempy - arrayOfGameObjects[xx][yy].getY();
tempcalcx = Math.pow(tempcalcx,2);
tempcalcy = Math.pow(tempcalcy,2);
double temptot = tempcalcx + tempcalcy;
double temproottot = Math.sqrt(temptot);
//System.out.println("Tracking Distance to object at grid horozontal: " + arrayOfGameObjects[xx][yy].getHorozontal()+ " vertical: "+ arrayOfGameObjects[xx][yy].getVertical() + " which is: " + temproottot);
if (temproottot < currentshortestdistance)
{
currentshortestdistance = temproottot;
tempx = arrayOfGameObjects[xx][yy].getX();
destinationHoroz = arrayOfGameObjects[xx][yy].getHorozontal();
destinationVert = arrayOfGameObjects[xx][yy].getVertical();
tempy = arrayOfGameObjects[xx][yy].getY();
}
}
}
}
//System.out.println ("Distination set to x: " + tempx + " y: " + tempy);
destinationx = tempx;
destinationy=tempy;
move(g);
//draw(g);
}
int getDestinationHoroz()
{
return destinationHoroz;
}
int getDestinationVert()
{
return destinationVert;
}
int getX()
{
return (int) x;
}
int getY()
{
return (int) y;
}
void draw (Graphics g)
{
}
boolean getDestroy()
{
if ((x < destinationx+5) && (x > destinationx -5) && (y < destinationy+5) && (y > destinationy -5))
{
return true;
}
return false;
}
void move(Graphics g)
{
deltaX = destinationx - x;
deltaY = destinationy - y;
double mag = Math.sqrt( deltaX*deltaX + deltaY*deltaY );
deltaX = deltaX / mag * speed;
deltaY = deltaY / mag * speed;
double templinex = deltaX *15;
double templiney = deltaY *15;
x = x+deltaX;
y=y+deltaY;
g.setColor(Color.WHITE);
double tempx = x;
double tempy = y;
double magtemp = mag;
magtemp += 0.5;
magtemp = Math.floor(magtemp);
tempx += 0.5;
tempx = Math.floor(tempx);
tempy += 0.5;
tempy = Math.floor(tempy);
g.drawRect ((int) tempx, (int) tempy, 10,10);
//g.drawLine((int)tempx,(int) tempy,(int) (tempx + templinex),(int) (tempy + templiney));
//g.drawLine((int)(tempx + templinex),(int) (tempy-templiney),(int) (tempx + templinex),(int) (tempy + templiney));
//g.drawLine((int)(tempx-templinex),(int) (tempy+templiney),(int) (tempx + templinex),(int) (tempy + templiney));
}
}
```

Thanks again!