Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's wrong??? (NPC1->Tile collision) // C++
#1
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;
    
}
B 4 EVER
Reply
#2
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?
Reply
#3
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
B 4 EVER
Reply
#4
Good that you got it solved. Now could you enlighten me as to what the bug was?
Reply
#5
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
B 4 EVER
Reply
#6
Now, that was impossible for anyone to tell as we wouldnt have had known where you are storing the data Wink.
Reply
#7
Yeah, that's true. I just can say sorry
B 4 EVER
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)