code snippet:

void checkcollision()
{
    float x1 = tanks[0].x;
    float y1 = tanks[0].y;
    float x2 = tanks[1].x;
    float y2 = tanks[1].y;
    float dist = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    float dr = tanks[0].rad+tanks[1].rad;
    if(dist<dr);
    {
        printf("checkcollision read true\n");
        printf("dist: %f, radius+radius: %f\n",dist,dr);
        goback(0); goback(1);
    }
}

output:

checkcollision read true
dist:363.159302, radius+radius:14.142136
goback executed for tank 1
goback executed for tank 2

I just need someone to break down why this might happen. I don't get it. Do conditional operands not work on float?

if(dist<dr);
remove semicolon ?
should be or it will do nothing if dist < dr and do nothing on else

    if(dist<dr)
    {
        printf("checkcollision read true\n");
        printf("dist: %f, radius+radius: %f\n",dist,dr);
        goback(0); goback(1);
    }

Edited 4 Years Ago by Sokurenko: more

thank you. Am I the only one who has trouble seeing small problems in walls of text that comprise code? This is depressing.

No, everyone has them, you need to be patient. Cheer up :)
Just always know that this is c language, there is no magic, if something does not work then you can find out why always. Add as much printf as you can to do that..
Like that

void checkcollision()
{
    float x1 = tanks[0].x;
    float y1 = tanks[0].y;
    float x2 = tanks[1].x;
    float y2 = tanks[1].y;
    float dist = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    float dr = tanks[0].rad+tanks[1].rad;
    printf("dist: %f, radius+radius: %f\n",dist, dr);
    if(1 < 2)
    {
       puts("yes");
    }
    else
    {
       puts("wtf");
    }
    if(dist < dr) ;
    {
        printf("checkcollision read true\n");
        printf("dist: %f, radius+radius: %f\n",dist,dr);
        goback(0); goback(1);
    }
}

thank you. Am I the only one who has trouble seeing small problems in walls of text that comprise code? This is depressing.

Ane we usually guard these with #ifdef DEBUG ... #endif /* DEBUG */ blocks so that you can turn the printf statements off when you are ready for production. Example:

void checkcollision()
{
    float x1 = tanks[0].x;
    float y1 = tanks[0].y;
    float x2 = tanks[1].x;
    float y2 = tanks[1].y;
    float dist = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
    float dr = tanks[0].rad+tanks[1].rad;
#ifdef DEBUG
    printf("dist: %f, radius+radius: %f\n",dist, dr);
    if(1 < 2)
    {
       puts("yes");
    }
    else
    {
       puts("wtf");
    }
#endif /* DEBUG */
    if(dist < dr) ;
    {
        printf("checkcollision read true\n");
        printf("dist: %f, radius+radius: %f\n",dist,dr);
        goback(0); goback(1);
    }
}

So, when you have a valid value, yet you don't get the output from inside the if (dist<dr) clause, then you know you need to look there. In any case, this sort of bug is common to beginners. Another one is to have single line contents of a conditional and not use curly-braces, so when you add code later that should be inside the conditional, it doesnt work correctly. Example:

        if(dist < dr) /* Note - no semicolon now */
            goback(0); goback(1);

This will be another bug since the second goback(1) will execute whether or not dist < dr. Rule: ALWAYS brace ALL conditional blocks of code, whether they are one liners or not. First, it is easier to read. Second, adding more code within the conditional will be less likely to result in inadvertant bugs.

One final thing. Do NOT, if at all possible, execute two separate statements on one line as you have above with goback(0); goback(1);. If they are on two lines, then you can tell more easily that you forgot to enclose them in braces, at least for my example. In your case, you DID enclose them - good, but put both statements on one line - bad.

Edited 4 Years Ago by rubberman

Thanks guys.

Rubberman, that's really smart using #ifdef DEBUG I'm gonna keep that in mind. I did know about the one line execution after if statement without curly braces, thats why I almost always use it.

This question has already been answered. Start a new discussion instead.