Rotating a sprite stored as int[][]

Hi there,

I’m trying to write a 4k contest entry and I’m a bit stuck. I’ve not done any Java2d stuff before.

I have an int[][] which contains a list of colour indexes e.g. 0 is transparent, 1 is black 2 is white etc. and I have a sprite setup in this format of size 16x16.

Now as the player turns I want to rotate this sprite around a given point. I’m currently drawing my sprite using a two for loops (inner and outer) and a drawPixel method I wrote.

How do I go about performing these rotations, do I need to create a temp image or similar, or do I need to write some form of Bhremsian line algorithm?

I have a BufferStrategy setup already to make things nice and smooth, any pointers would be appreciated.

Thanks,

Andy.

PS: Reading a bit more Javadoc, I’ll now ask the question, do I need to somehow use an AffineTransform to solve this?

sorry I don’t have an answer about the rotation, but I just though of something. You may want to use an array with a single dimension instead of two so you could use just one for loop instead of two for every operation you do on a sprite.

index = row*colLength+col;

I’d suggest creating a managed Translucent BufferedImage of those dimensions. Draw your black and white pixels, then do your rotation with AffineTransform.

[quote]Hi there,
[/quote]
Hello. :slight_smile:

[quote]I’m trying to write a 4k contest entry and I’m a bit stuck. I’ve not done any Java2d stuff before.
[/quote]
Welp, this is the place for help. :slight_smile:

[quote]I have an int[][] which contains a list of colour indexes e.g. 0 is transparent, 1 is black 2 is white etc. and I have a sprite setup in this format of size 16x16.
[/quote]
Funny. Sounds very familiar. :slight_smile:

[quote]Now as the player turns I want to rotate this sprite around a given point. I’m currently drawing my sprite using a two for loops (inner and outer) and a drawPixel method I wrote.
[/quote]
Well, you can read this article for info on how to do that. Personally, I think you’re better off loading data into a BufferedImage and using that. My SuperPack and SuperPackME tools can do that for you automagically. That’s why I wrote them. :slight_smile:

Just an FYI, if you’re not using many images (e.g. 1 16x16 image) then you’re going to lose more than you gain by encoding the images like you did. If you look at my Defender 4000 entry from last year, you’ll notice that I packed in 21 images, thus making the decoder worthwhile.

[quote]PS: Reading a bit more Javadoc, I’ll now ask the question, do I need to somehow use an AffineTransform to solve this?
[/quote]
That is probably the best way to do it.

indeed,

Also, make sure all your image drawing goes through the method,

Graphics.drawImage(Image img, AffineTransform xform, ImageObserver obs)

and you will save yourself a few dozen bytes.