Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)
You are not logged in.
I'm trying to get some form of collision detection working so that the player can't walk through an object. Both the player and the object are 16x16 sprites and I use the top left corner to compare their positions. However when I use this code, the player can still walk through the object fine:
case J_UP: if (SprY>36){ SprY=SprY-8; wait_vbl_done(); set_sprite_tile(5,21); set_sprite_tile(6,22); set_sprite_tile(7,23); set_sprite_tile(8,24); delay(75); } else if (SprY<20){ SprY=SprY-8; wait_vbl_done(); set_sprite_tile(5,21); set_sprite_tile(6,22); set_sprite_tile(7,23); set_sprite_tile(8,24); delay(75); } else if (SprX>20){ SprY=SprY-8; wait_vbl_done(); set_sprite_tile(5,21); set_sprite_tile(6,22); set_sprite_tile(7,23); set_sprite_tile(8,24); delay(75); } else if (SprX<20){ SprY=SprY-8; wait_vbl_done(); set_sprite_tile(5,21); set_sprite_tile(6,22); set_sprite_tile(7,23); set_sprite_tile(8,24); delay(75); } break;
I don't understand why this doesn't stop the player from colliding with the object. Can someone help explain it to me?
Offline
There are a few issues with your logic. The If/else structure you are using will only run 1 case on a match. So if both of your SprX and SprY conditions are met you will only run the block for the SprY check since its first.
Your SprX checks are looking at every position except 20.
Your SprX checks modify SprY, not sure if that was intentional.
Try looking up logic for a bounding box collision in google. Unless you are making bounds across the entire X/Y axis you will need to check both axis to determine if there was a collision.
Lets say you have a fixed bounding box from your sample at 20, 20 that is 16x16.
To test if SprX/SprY is a point inside the box.
if ((SprX > 20 && SprX < 36) && (SprY > 20 && SprY < 36))
You might need to play with <=, >= if you want the bounds to be inclusive.
Offline