|background collision off by a few pixels
|Page 1 of 1|
|Author:||CrowleyBluegrass [ Thu Mar 08, 2018 5:42 am ]|
|Post subject:||background collision off by a few pixels|
Hello again everyone Thank you for the assistance in my previous thread.
Now, metatiles aside, I've been trying my darndest to understand background collision and with the help of some code shamelessly lifted from tokumaru (thank you!!) and explanations from Bregalad (thank you!!) I'm slowly starting to piece everything together.
However... as per usual I've having some issues.
Right now I'm just focusing on collision with the top left corner of the metasprite, so up-moving and left-moving collisions. Everything seems to work fine, but the collision is "off" a few pixels.
When colliding upwards, there is a very slight gap between the player and the tile.
When colliding leftwards, I have the opposite problem, the player moves slightly into the tile before being stopped.
(Tile $00 is the player, $01 are non-solid, and $02 are solid tiles)
These can be fixed by simply CLC/ADCing and SEC/SBCing the X and Y values when checking collision, but, I'd like to know why this is happening as it must be an error in my code/understanding of the code, and that can't be good!
Sorry if my code is difficult to read through/badly organized, I'm not a programmer by nature I'm afraid but as always, I'm trying!
|Author:||Kasumi [ Thu Mar 08, 2018 3:06 pm ]|
|Post subject:||Re: background collision off by a few pixels|
One issue: There's not an RTS under at the end of CheckCollision.
Your program flow:
1. Get the tile number from the pixel at position player_x and player_y.
2. Store the tile number to collided. (2 means solid.)
3. If collided is not 2, move the player left.
Problem 1: The player is one pixel to the right of the wall. The position at player_x and player_y is not in a collision tile (Because the player is still to the right of the wall). A 1 gets stored into collided. Left sees that collided is not 2, and allows the player to move into the wall.
Problem 2: Once the player is in the wall, there's nothing that moves them out.
The fix: Move the player into the wall, then eject them.
;JSR CheckCollision;Don't check collision before you move
DEC player_x;Move the player left. Potentially into a wall.
JSR CheckCollision;Now we check collision
CMP #$01;If we're not in a collision tile
BEQ +;We don't need to eject
;If here, they're in a wall
INC player_x;Move them out of the wall before drawing.
+ JMP Draw
Code above is entirely untested. In the future you may also wanna share your .chr file so people can build your code without hacking it out of the ROM.
Edit: Be aware that sprites are drawn one pixel below the position you write so make sure you account for that.
If you want to see for yourself, write #$00 to $0200. There will be one pixel between the top of the sprite and the top of the screen. This makes it actually impossible to draw sprites to the top pixel of the screen. That's just how NES is. To fix this, you may just want to subtract one in your draw function like this:
SBC #$01;Offset sprites being delayed one scanline
so sprites will be drawn on the screen where you expect based on their position.
|Page 1 of 1||All times are UTC - 7 hours|
|Powered by phpBB® Forum Software © phpBB Group