My problem today is this:
I have many civilians going around, they are classes stored by an arraylist.
The idea is when they see another civilian panic, they'll start to panic and it will spread.
First I call each classes Step()
function by looping through an iterator. Then in the Step()
function it goes through another civillian iterator
As it goes through, it tries to detect whether it can see the other civillian in the iterator, this is where performance time goes from 0 to 50 Milliseconds for having 100 Civillians.
This is the problem I need to fix, I tried to make an easy way to detect whether any objects are in the way of point a to point b.
Here's the code for the line of sight:
public static Object LOS(int x, int y, int x2, int y2, String Scan, Object Me, Object You) {
DirectionX = (x-x2)/Quality;
DirectionY = (y-y2)/Quality;
CurrentX = x;
CurrentY = y;
String[] ScanArray = Scan.split(":");
for(int I=0;I<=Quality;I++) {
for(String Type: ScanArray) {
if(Type.equals("Boxs")) {
Iterator it=Level.Boxs.iterator();
while(it.hasNext()) {
Box Box = (Box)it.next();
if(Me!=Box&&You!=Box) {
//Collision = Tools.Collision((int)(CurrentX-(Width/2)), (int)(CurrentY-(Width/2)), Width, Width, Box.GetX(), Box.GetY(), Box.GetWidth(), Box.GetHeight(), 1);
boolean Col = Tools.BasicCollision((int)(CurrentX-(Width/2)), (int)(CurrentY-(Width/2)), Width, Width, Box.GetX(), Box.GetY(), Box.GetWidth(), Box.GetHeight());
}
}
}
}
CurrentX-=DirectionX;
CurrentY-=DirectionY;
}
return null;
}
If you have a headache the fundamentals are:
It figures out 10 points in between and detects whether it is inside, using BasicCollision
:
public static boolean BasicCollision(int x, int y, int width, int height, int x2, int y2, int width2, int height2) {
if(x<x2+width&&x+width>x2&&y<y2+height&&y+height>y2) {
return true;
} else {
return false;
}
}
My question is: Is there an easier way to detect this Line of Sight that doesn't severely impact my performance in large numbers? Any feedback?
LOS.txt
2. We don't want to see all of your code. Provide an SSCCE. \$\endgroup\$