Qbasicnews.com

Full Version: What's wrong??? (NPC1->Tile collision) // C++
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here's my collision detection routine (collision between NPCs and nonwalkable tiles). It doesn't work. But I can't see the problem. So, could anyone show me at least the bug in that function??

Code:
short int _engine::npc_tile_collide(_npc npc1) {

    int x1, y1, x2, y2;

    for (int nx = -1; nx < 2; nx++)
        for (int ny = -1; ny < 2; ny++)
            if (!map_data[npc1.x_pos + nx][npc1.y_pos + ny]) {

                x1 = sprite_x_wide * npc1.x_pos + npc1.x_pix;
                y1 = sprite_y_wide * npc1.y_pos + npc1.y_pix;
                x2 = sprite_x_wide * (npc1.x_pos + nx);
                y2 = sprite_y_wide * (npc1.y_pos + ny);

                if (!((x1 > x2 + sprite_x_wide) || (x2 > x1 + npc_sprite_x_wide)))
                    if (!((y1 > y2 + sprite_y_wide) || (y2 > y1 + npc_sprite_y_wide)))
                        return 1;

            }
        
    return 0;
    
}
Could you explain what is NPC, nonwalkable tiles.

Quote:if (!((x1 > x2 + sprite_x_wide) || (x2 > x1 + npc_sprite_x_wide)))
if (!((y1 > y2 + sprite_y_wide) || (y2 > y1 + npc_sprite_y_wide)))

Why arent you evaluating this condition in one line?
The problem is solved.

But anyway.:
NPC is the variable of a class for NonPlayerCharacters
Nonwalkable tiles are tiles you can't walk over, like walls and so on
I didn't do those two lines in one line, because this way I did it it looks better, I think
Good that you got it solved. Now could you enlighten me as to what the bug was?
That's the current code:
Code:
short int _engine::npc_tile_collide(_npc npc1) {



    int x1, y1, x2, y2;



    for (int nx = -1; nx < 2; nx++)

        for (int ny = -1; ny < 2; ny++)

            if (!map_walk_able[npc1.x_pos + nx][npc1.y_pos + ny]) {



                x1 = sprite_x_wide * npc1.x_pos + npc1.x_pix + (npc_sprite_x_wide - sprite_x_wide) / 2;

                y1 = sprite_y_wide * (npc1.y_pos - 1) + npc1.y_pix + npc_sprite_y_wide;

                x2 = sprite_x_wide * (npc1.x_pos + nx);

                y2 = sprite_y_wide * (npc1.y_pos + ny);



                if (!((x1 > x2 + sprite_x_wide) || (x2 > x1 + sprite_x_wide)))

                    return 1;

                if (!((y1 > y2 + sprite_y_wide) || (y2 > y1 + sprite_y_wide)))

                    return 1;

            

            }



    return 0;

    

}

It was just me dumbass...a typo...map_data and map_walk_able
Now, that was impossible for anyone to tell as we wouldnt have had known where you are storing the data Wink.
Yeah, that's true. I just can say sorry