## Calculate sprite coordinates on the nametable

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Pokun
Posts: 1322
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

### Calculate sprite coordinates on the nametable

Let's say I want to draw a 8x8 sprite so that it exactly covers a 8x8 BG tile. Is there an easy way to calculate the X- and Y-coordinates of the sprite if I know the nametable address? The scroll register is 0.

tepples
Posts: 21837
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

### Re: Calculate sprite coordinates on the nametable

X coordinate: (((address << 3) & 0xF8) - camera_x) % 256
Y coordinate: (((address >> 2) & 0xF8) - (camera_y + 1)) % 240

A simplification is possible if your scroll position is (0, 0), as you mentioned:
X coordinate: (address << 3) & 0xF8
Y coordinate: ((address >> 2) & 0xF8) - 1

Translation of these expressions from C to 6502 assembly code is an exercise for the reader.

Pokun
Posts: 1322
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

### Re: Calculate sprite coordinates on the nametable

Thanks a lot! These formulas works like a charm!