Hi, I have been struggling to get a couple of functions in my latest project to do what they're supposed to, and was wondering if anyone could offer any suggestions.
Basically what's supposed to happen is this. When you fire a gun, the game checks to see if it intersects each enemy, which, in code, is a circle with a radius of 2. I have a function that checks whether a line intersects a circle, which can be found here.
https://www.dropbox.com/s/59hreb10098wvu9/LCI.bgt?dl=1
This seems to work, but I'm having problems when I try to get the closest enemy with a function. When the function gets called, I'm getting a divide by 0 error in line 7 of the function in the db link. I'm not sure why this is though, as that can only happen if the distance between you and the enemy is 0.
Does anyone have any suggestions as to why this might be happening? The code for the function is below:
double iEX, iEY, i2EX, i2EY;
int rIndex = -1;
for (int index = 0; index < this.pl.level.enemies.length; index++) {
if (getCircleIntersection(this.pl.x+1, this.pl.y+1, newX, newY, this.pl.level.enemies[index].x, this.pl.level.enemies[index].y, this.pl.level.enemies[index].radius, iEX, iEY, i2EX, i2EY) == 1) {
if (square_root((this.pl.x-iEX)^2+(this.pl.y-iEY)^2) < distance) {
distance = square_root((this.pl.x-iEX)^2+(this.pl.y-iEY)^2);
rIndex = index;
}
else if (square_root((this.pl.x-i2EX)^2+(this.pl.y-i2EY)^2) < distance) {
distance = square_root((this.pl.x-i2EX)^2+(this.pl.y-i2EY)^2);
rIndex=index;
}
}
else if(getCircleIntersection(this.pl.x, this.pl.y, newX, newY, this.pl.level.enemies[index].x, this.pl.level.enemies[index].y, this.pl.level.enemies[index].radius, iEX, iEY, i2EX, i2EY) == 2) {
if (square_root((this.pl.x-iEX)^2+(this.pl.y-iEY)^2) < distance) {
distance = square_root((this.pl.x-iEX)^2+(this.pl.y-iEY)^2);
rIndex = index;
}
}
}
return rIndex;
}
I feel like I'm missing something obvious, but like I said. I really have absolutely no idea.
Pages 1