Rotating a sprite 90 degrees

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

Post Reply
User avatar
battagline
Posts: 152
Joined: Wed Sep 05, 2018 11:13 am
Location: Colorado
Contact:

Rotating a sprite 90 degrees

Post by battagline »

Is it possible to rotate a sprite by 90 degrees? Or do I need to do it in photoshop and include it in the .chr file that way?

Thanks
A few of my web games
https://www.embed.com
Or if you're bored at work
https://www.classicsolitaire.com
lidnariq
Posts: 11430
Joined: Sun Apr 13, 2008 11:12 am

Re: Rotating a sprite 90 degrees

Post by lidnariq »

The hardware does not support rotation by 90 degrees, no. Only horizontal and vertical flips, which together are equivalent to a 180 degree rotation.
User avatar
rainwarrior
Posts: 8731
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Rotating a sprite 90 degrees

Post by rainwarrior »

If you're using CHR-RAM you could write some code to rotate the sprite tile before uploading it to the PPU. This would only really be helpful if you have a lot of stuff that needs a rotated form, though. Doesn't really solve any problems of e.g. pattern table management.
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: Rotating a sprite 90 degrees

Post by dougeff »

It'd be terribly slow.

Rotate 1 tile left 90°

LDY #0
LDX #0
Loop:
LDA Source, Y
Loop2:
LSR a
ROL Dest, X
INX
CPX #8
BNE Loop2
LDX #0
INY
CPY #8
BNE Loop
LDX #8
;x and y should be 8 now
Loop3:
LDA Source, Y
Loop4:
LSR a
ROL Dest, X
INX
CPX #16
BNE Loop4
LDX #8
INY
CPY #16
BNE Loop3

Edit, wrote the 2nd half.

Hasn't been tested. Only in my head.


Edit, and I think, rotate right 90° would be...replace LSR with ASL and ROL with ROR. (edited about 6 times)
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
battagline
Posts: 152
Joined: Wed Sep 05, 2018 11:13 am
Location: Colorado
Contact:

Re: Rotating a sprite 90 degrees

Post by battagline »

It sounds like the best thing to do is just rotate it 90 degrees photoshop and have 2 versions in the .chr

Thanks for your help, was just hoping there was some better way
A few of my web games
https://www.embed.com
Or if you're bored at work
https://www.classicsolitaire.com
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: Rotating a sprite 90 degrees

Post by dougeff »

It works. I made a demo, with source. Also included flip tile H and flip tile V, which were easier.

It is very slow. About 2250 cycles per tile. (and only 300 cycles for flip V).
Attachments
ROTATE.zip
(2.39 KiB) Downloaded 158 times
ROTATE.png
ROTATE.png (4.46 KiB) Viewed 5159 times
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
nesrocks
Posts: 563
Joined: Thu Aug 13, 2015 4:40 pm
Location: Rio de Janeiro - Brazil
Contact:

Re: Rotating a sprite 90 degrees

Post by nesrocks »

Would it make it faster to use a pre-calculated table of conversion? (I couldn't understand your code, where are you writing to?) edit: oh okay, it's the LSR and ROL. I'm rusty :)
Last edited by nesrocks on Thu Oct 11, 2018 6:52 am, edited 2 times in total.
https://twitter.com/bitinkstudios <- Follow me on twitter! Thanks!
https://www.patreon.com/bitinkstudios <- Support me on Patreon!
User avatar
Bregalad
Posts: 8055
Joined: Fri Nov 12, 2004 2:49 pm
Location: Divonne-les-bains, France

Re: Rotating a sprite 90 degrees

Post by Bregalad »

I'm fairely sure at least one of the Dragon Quest/Warrior games does this (in software), could probably be 3 I don't remember. As this is a complex/slow operation they probably do it when loading the graphics on the screen, not on the fly.
lidnariq
Posts: 11430
Joined: Sun Apr 13, 2008 11:12 am

Re: Rotating a sprite 90 degrees

Post by lidnariq »

dougeff wrote:It is very slow. About 2250 cycles per tile.
A partial unroll (replacing the inner X: lsr A / rol $0300,x / inx / cpx #8 / bne X) speeds up each row from 127 cycles to 64 cycles, speeding it up in total to just 1242 cycles.
User avatar
rainwarrior
Posts: 8731
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Rotating a sprite 90 degrees

Post by rainwarrior »

I would only think it should be useful if you need to mass rotate a bunch of tiles, e.g. if you wanted to rotate your world map or something like that. Could save a bunch of ROM space, maybe. Wasn't really suggesting it as a realtime effect, though that's not entirely out of the question.
User avatar
pubby
Posts: 583
Joined: Thu Mar 31, 2016 11:15 am

Re: Rotating a sprite 90 degrees

Post by pubby »

It could be useful for CHR compression algorithms I suppose.
tepples
Posts: 22705
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Rotating a sprite 90 degrees

Post by tepples »

JRoatch's Donut codec, developed for a forthcoming version of Action 53, has a rotation mode. But it's not for reusing the same tile in unrotated and 90 degree rotated modes; it's for handling non-tile data in CHR ROM.
Post Reply