2015-04-27 21:21:32

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.

Prier practice and preparation prevents piss poor performance!

2015-04-27 22:11:28

@Dranelement, I haven't had the time to go through your code to any extent, but one thing that strikes me as wrong as you are declaring your variables as int. You should never use integer variables for this type of calculation as you will lose precision and accuracy by doing so. You should be declaring them as floating point variables, float, in order to retain the accuracy you want/need for this type of calculation.

Sincerely,
Thomas Ward
USA Games Interactive
http://www.usagamesinteractive.com

2015-04-27 23:21:27

Hi, actualy the only things that are int are the values the function is returning. Some of the peramiters of the function are actually variables that get changed based on the input. I would have used pointers, but for some reason bgt only lets you use those in classes, so...

Prier practice and preparation prevents piss poor performance!

2015-04-28 14:41:08

yes you should use floats. it will allow for much more preciseness.
for example: in python
ex1. using integers
8 /3 = 2
obviously 2 is correct but its not the whole answer.
x2 using floats
8.0 / 3.0 = 2.666666...5
that is way more accurate and a big difference. i'm not sure if bgt handles this diffrently but i can only assume its about the same. just think if something came out like 5.915826384694628. the answer you'd get using integers would still be 5 but you'd be off by almost 1. which is a whole crap of a lot when talk in terms of shooting games.

I don’t believe in fighting unnecessarily.  But if something is worth fighting for, then its always a fight worth winning.
check me out on Twitter and on GitHub

2015-04-28 15:12:25

Hi, all the variables that store important numbers are doubles, which are very precise. The only things that are integers are the values returned by the functions, which are used to handle different cases.

Prier practice and preparation prevents piss poor performance!