I've narrowed it down to a for loop in my update function that checks a whether a line segment(a bullet's previous position to its current position) intersects a circle(a person). Both the bullets and people are stored in a vector. The code is as follows:
Code: Select all
for(unsigned int k=0; k<mBullets.size(); k++) {
Vector2D p1(mBullets[k]->pX,mBullets[k]->pY);
Vector2D p2(mBullets[k]->mX,mBullets[k]->mY);
Vector2D O = p1;
Vector2D D = p2-p1;
for(unsigned int j=0; j<mPeople.size(); j++) {
Vector2D C(mPeople[j]->GetX(),mPeople[j]->GetY());
float t = D.Dot(C-O)/D.Dot(D);
if (t < 0) {
t = 0;
}
else if (t > 1) {
t = 1;
}
Vector2D closest = O+t*D;
Vector2D d = C - closest;
float ll = d.Dot(d);
int r = 16;
if (ll < r * r) {
mPeople[j]->mHealth -= 20;
mPeople[j]->SetSpeed(mPeople[j]->GetSpeed()*0.5f);
mBullets.erase(mBullets.begin()+k);
k--; //makes up for the erase to prevent skipping bullets
}
}
}
Thanks
btw, I'm using dr_watson's JGE++ Engine