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!

2
Contributors
4
Replies
5
Views
5 Years
Discussion Span
Last Post by NormR1

times when a destination other then the closest one is selected

Could you post some print outs that show the data when this happens? Print the location of the one that was selected and the location of the ones that are closer.

Can you post a small complete program that compiles, executes and shows the problem? Something that has the selection code and some objects to chose from. Without code that executes it is impossible to test.

Edited by NormR1

here is the output. it thought that 16 12 was closer, but if we look at my grid, this bomber spawned on the right side 22,13 was way closer, 22 was horozontal, it was 8 grids closer to the right :-(

Creating an instance of SuicideBomber, which spawned at 3
Tracking Distance to object at grid horozontal: 11 vertical: 12 which is distance: 13735.560600135694
Tracking Distance to object at grid horozontal: 16 vertical: 6 which is distance: 195.25624189766637
Tracking Distance to object at grid horozontal: 16 vertical: 12 which is distance: 150.0
Tracking Distance to object at grid horozontal: 22 vertical: 13 which is distance: 152.0690632574555
Distination set to x: 400.0 y: 300.0 horozontal: 16 vertical: 12
Tracking Distance to object at grid horozontal: 11 vertical: 12 which is distance: 13735.560600135694
Tracking Distance to object at grid horozontal: 16 vertical: 6 which is distance: 195.25624189766637
Tracking Distance to object at grid horozontal: 16 vertical: 12 which is distance: 150.0
Tracking Distance to object at grid horozontal: 22 vertical: 13 which is distance: 152.0690632574555
Distination set to x: 400.0 y: 300.0 horozontal: 16 vertical: 12
Tracking Distance to object at grid horozontal: 11 vertical: 12 which is distance: 13735.560600135694
Tracking Distance to object at grid horozontal: 16 vertical: 6 which is distance: 195.25624189766637
Tracking Distance to object at grid horozontal: 16 vertical: 12 which is distance: 150.0
Tracking Distance to object at grid horozontal: 22 vertical: 13 which is distance: 152.0690632574555
Distination set to x: 400.0 y: 300.0 horozontal: 16 vertical: 12

here is a pic of my game to help understand what im trying to do.

Can you post a small complete program that compiles, executes and shows the problem? Something that has the selection code and some objects to chose from. Without code that executes it is impossible to test.

I can but im not willing to do that yet, sorry i know your just trying to help, but for now i guess ill accept that my suicide bombers arnt that inteligent and sometimes go for the wrong target :-)

Thanks tho for trying, much appriciated.

-Fedhell

Edited by Fedhell

Attachments

Without code to test, there is nothing to suggest other than debugging by adding lots of println statements to show what the code is doing so you can see where the code is going wrong.

Can you write a simple program that has a list of points and a single point that uses your algorithm to find the closet point in the list to the single point?

Edited by NormR1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.