It is currently Thu Oct 19, 2017 4:10 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: What is Sprite 0?
PostPosted: Fri Apr 20, 2012 11:08 am 
Offline
User avatar

Joined: Tue Feb 13, 2007 9:02 pm
Posts: 147
Location: Richmond, VA
An elementary question, but I can't seem to find an answer in the wiki or elsewhere online.

I understand how sprite 0 works and why it is used. Visually, I've seen how it is used in both Super Mario Bros. and Gyromite. But I don't understand what it is. In both of the above cases, the sprite used as sprite 0 is located at position $FF in the sprite table (unless I'm mistaken). So, is sprite 0 assigned by the programmer or is it always a given sprite located at a certain position in the sprite table? Is it called sprite 0 simply due to its display order priority?

My reason for asking: I was reading about R.O.B.'s behavior / interaction with Gyromite in this thread on AtariAge. Apparently, there's a check for sprite 0 prior to the screen flashing green and black (which triggers the robot's movements). I'm wondering why this technique was used. Seems sort of atypical, since there's no status bar split necessary.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2012 11:28 am 
Offline
User avatar

Joined: Fri Nov 19, 2004 7:35 pm
Posts: 3943
It's called Sprite 0 because it is literally the first sprite in the OAM (sprite table). Just like any other sprite, the programmer assigns where it is positioned, and what tile it uses. The PPU was designed to make sprite #0 special for 'hit testing', and thus screen splitting.

Another thing Sprite 0 Hit is used for is determining when Vblank time ends. The only signals you get are that the VBL flag gets cleared, and the Sprite 0 Collision flag gets cleared as well. The VBL flag also gets cleared whenever you read PPU status, so reading it repeatedly won't help you there. So you use sprite 0 collision to find out when vblank time ends. The bit is still set to whatever its last state was, so if it collided, you can wait for it to become clear.
Note that you're not trying to see when it collides, you're seeing when it clears. And that tells you that vblank time has ended, and you're in the prerender scanline now. In order to use this, you need to make sure sprite 0 will collide the next frame.

_________________
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!


Top
 Profile  
 
 Post subject: Re: What is Sprite 0?
PostPosted: Fri Apr 20, 2012 11:30 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10057
Location: Rio de Janeiro - Brazil
noattack wrote:
Is it called sprite 0 simply due to its display order priority?

Pretty much this. Sprite 0 works like all others (it can use any tile or palette, no limitations whatsoever), but since it's the one with highest priority (meaning it's never dropped even if it's in the same scanline as several other sprites) it has this extra functionality of indicating collisions with the background.

Whenever an opaque pixel in sprite 0 collides with an opaque pixel in the background, the sprite 0 hit flag gets set. Programs uses this to synchronize with the display.

The purpose isn't always to split the screen, there are other reasons for a programmer to want to synchronize with the rendering. I can't think of any right now, though! =) OTOH, I have no idea how R.O.B. works.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2012 11:39 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10057
Location: Rio de Janeiro - Brazil
Dwedit wrote:
Another thing Sprite 0 Hit is used for is determining when Vblank time ends.

True, I forgot about that.

Quote:
In order to use this, you need to make sure sprite 0 will collide the next frame.

Which isn't always easy in games that scroll vertically and horizontally, unless you place a solid tile in a corner every frame, after erasing the tile used in the previous frame.

The sprite overflow flag could also be used for detecting the end of VBlank, and it's really easy to make sure overflows happen every frame (just place 9 sprites in a row). The disadvantage is that you lose 9 sprites... But there are games that sacrifice more sprites than that to hide scrolling artifacts (Felix the Cat and Alfred Chicken come to mind) and are still pretty good!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2012 11:41 am 
Offline
User avatar

Joined: Tue Feb 13, 2007 9:02 pm
Posts: 147
Location: Richmond, VA
This makes a lot more sense.

So whatever tile is indexed in OAM position 0 serves as sprite 0. This happens regardless of whether the programmer uses it or not. So presumably sprite 0 could be part of an active player sprite, moving all over the place, triggering the PPUSTATUS flag -- but ignored since it's not needed.

I can't think of a reason why this would ever be necessary, but are there any instances where sprite 0 is used but its tile index is changed during gameplay?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2012 11:51 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10057
Location: Rio de Janeiro - Brazil
noattack wrote:
So presumably sprite 0 could be part of an active player sprite, moving all over the place, triggering the PPUSTATUS flag -- but ignored since it's not needed.

Sure, no problem.

Quote:
I can't think of a reason why this would ever be necessary, but are there any instances where sprite 0 is used but its tile index is changed during gameplay?

You mean used for scanline detection or as part of an active player moving all over the place? In both cases the pattern can be switched without problems, but when detecting sprite hits I don't see much reason to do this. But you can, if you think different parts of your game will benefit from using different patterns for sprite 0 collisions. Most games I'm aware of just use a horizontal line for all hits.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2012 1:09 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19099
Location: NE Indiana, USA (NTSC)
And of course, the index of what's currently a "horizontal line tile" may change as a game bankswitches in different tile sets.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2012 1:31 pm 
Offline
User avatar

Joined: Tue Feb 13, 2007 9:02 pm
Posts: 147
Location: Richmond, VA
Turns out I was wrong about Gyromite's sprite 0 usage. The game has some simple horizontal scrolling and sprite 0 sits right at the status bar split.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2012 1:35 pm 
Offline
User avatar

Joined: Wed Dec 06, 2006 8:18 pm
Posts: 2800
noattack wrote:
This makes a lot more sense.

So whatever tile is indexed in OAM position 0 serves as sprite 0. This happens regardless of whether the programmer uses it or not. So presumably sprite 0 could be part of an active player sprite, moving all over the place, triggering the PPUSTATUS flag -- but ignored since it's not needed.

I can't think of a reason why this would ever be necessary, but are there any instances where sprite 0 is used but its tile index is changed during gameplay?


Sprite 0 will either be used as a timing application like for raster effects or just like any other sprite. In games that use it for raster effects, the game will take care to never overwrite sprite 0 as doing so will cause the game to glitch or possibly lock up. Sprite 0 Hit I assume was cheaper to implement than a scanline IRQ interrupt. It would have been nice if NES had that like the SNES or Gameboy did. But the NES is where they learned that from.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2012 1:54 pm 
Offline
User avatar

Joined: Tue Feb 13, 2007 9:02 pm
Posts: 147
Location: Richmond, VA
Is sprite 0 the only built-in collision detection on the NES?

Just in the technical sense, presuming it would never be wise (or practical) to use sprite 0 for object-to-object collisions.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2012 2:51 pm 
Offline

Joined: Wed Feb 17, 2010 5:42 pm
Posts: 359
Location: Denine's Devil Mansion
If by "built-in" you mean "no mapper used(NROM)" then you can also try looking up for 5th bit of $2002.It's "Sprite overflow flag" and is set when 8 or more sprites are met on one line.It's cleaned at the end of each frame.
I never tried it, but I think it'll be the same as zero sprite, except for the fact that you will not have more sprites on the same line.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2012 3:07 pm 
Offline

Joined: Sun Sep 19, 2004 11:12 pm
Posts: 19099
Location: NE Indiana, USA (NTSC)
Due to a bug in the PPU, namely the so-called "diagonal" OAM scanning pattern, the sprite overflow flag behaves inconsistently except in the case where the ninth sprite is the one immediately following the eighth. Sometimes it turns on where are only eight; sometimes it doesn't turn on when there are nine. And if you're using it for a split point, and if you have a lot of objects in the main playfield above the split point, sometimes it turns on before the split point.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2012 3:29 pm 
Offline

Joined: Wed Feb 17, 2010 5:42 pm
Posts: 359
Location: Denine's Devil Mansion
I see. Sorry for unconvenience, and thanks for clearing that up.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2012 3:35 pm 
Offline
User avatar

Joined: Fri Nov 19, 2004 7:35 pm
Posts: 3943
Rumor has it that the sprite 0 stuff was used to avoid patent issues with something else. You could claim it was collision detection rather than a way to synchronize the CPU with the video picture.

_________________
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 20, 2012 4:20 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10057
Location: Rio de Janeiro - Brazil
noattack wrote:
Just in the technical sense, presuming it would never be wise (or practical) to use sprite 0 for object-to-object collisions.

The sprite 0 hit was never meant for object-to-object collisions. It can't even indicate collisions with other sprites, only with the background.

The Atari 2600, for example, has hardware registers to indicate collisions among all screen objects (players, missiles, ball and playfield), but on the NES you just have to do it all in software, usually by comparing the objects' bounding boxes.

tepples wrote:
the sprite overflow flag behaves inconsistently

Yeah, this makes the feature slightly less useful. The way I use it is fairly safe though: I detect the start of the frame by placing the 9 highest priority sprites at the very top of the screen. This always sets the flag. As a bonus, they hide all other sprites on the first several scanlines, which is an area where I also keep the background disabled (in order to hide scrolling artifacts), so the whole thing works like a pretty nice border, and sprites can scroll smoothly into the game window.

Dwedit wrote:
Rumor has it that the sprite 0 stuff was used to avoid patent issues with something else.

I wonder if this was really the case... I mean, cartridges soon started to offer more traditional scanline counters, why weren't they afraid of infringing patents then?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 17 posts ]  Go to page 1, 2  Next

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 4 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