It is currently Fri May 24, 2019 8:01 am

All times are UTC - 7 hours

Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Mar 08, 2018 5:42 am 

Joined: Sun Jun 30, 2013 7:59 am
Posts: 37
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!
Thank you :)

collision.nes [24.02 KiB]
Downloaded 60 times
collision.asm [11.04 KiB]
Downloaded 62 times
PostPosted: Thu Mar 08, 2018 3:06 pm 
User avatar

Joined: Wed Apr 02, 2008 2:09 pm
Posts: 1287
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 ReadController
  ;JSR CheckCollision;Don't check collision before you move
  JMP ButtonJump

  DEC player_x;Move the player left. Potentially into a wall.
  JSR CheckCollision;Now we check collision
  LDA collided
  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:
  LDA player_y
  SBC #$01;Offset sprites being delayed one scanline
  STA $0200
  LDA #$00
  STA $0201
  STA $0202
  LDA player_x
  STA $0203

so sprites will be drawn on the screen where you expect based on their position.


Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 7 hours

Who is online

Users browsing this forum: Google Adsense [Bot] and 8 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group