1
\$\begingroup\$

I am working on a side-scroller shooter right now. Everything is working fine along with the collision detection however, my problem is that the collision triggers my health deduction code multiple times per bullet. Here is the code where the bullet hits a turret. Supposedly doing 5.0 damage but instead it randomly does a lot more damage since the collision runs multiple times with just one bullet. How could I prevent this?

void TurretCollision()
{
 int BulletNo = 0; 
        int MissileNo = 0;

for (BulletNo = 0; BulletNo < MAX_BULLETS; BulletNo++)
{
    if (Turret[1] == true)
    {
        //cout << TurretH[1];
        if ((BulletX[BulletNo] >= 103 && BulletX[BulletNo] <= 103.09) && (BulletY[BulletNo] <= -38 && BulletY[BulletNo] >= -45))
        {
            BulletActive[BulletNo] = false;
            TurretTrial = TurretTrial - 5.0;

            if (TurretTrial <= 0.0)
            {
                Turret[1] = false;
            }
        }
    }

}

}

\$\endgroup\$
  • \$\begingroup\$ This has nothing to do with OpenGL; removed the tags and edited accordingly. \$\endgroup\$ – user1430 Dec 10 '14 at 17:52
2
\$\begingroup\$

Add a BulletCheck[BulletNo] == true condition.

if ((BulletX[BulletNo] >= 103 && BulletX[BulletNo] <= 103.09) 
&& (BulletY[BulletNo] <= -38 && BulletY[BulletNo] >= -45 && BulletActive[BulletNo]) == true)
    {
        BulletActive[BulletNo] = false;
        TurretTrial = TurretTrial - 5.0;

        if (TurretTrial <= 0.0)
        {
            Turret[1] = false;
        }
    }
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ It worked! Thanks. Haven't thought of that to be honest, kinda rushing the game since it's due in 10 hours and it's 1AM here and no sleep for a few days already. \$\endgroup\$ – DarkRider Dec 10 '14 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.