So I'm revisiting the tower defense game I started making some time back, and decided to give it another whirl in Java2D, while waiting for LWJGL to hit 1.9.0 and be usable on OS X with Java 7.

The reason I stopped working on it, was that the rendering became horribly slow after placing ~110 towers. This time around I did a little digging and found out that it's because I rotate my tower images that it starts to get slow. I rotate them, so that they're always facing the nearest enemy they can reach.

EDIT:You're currently creating a rotated version of the image instance each time you render. It's incredibly inefficient. Instead, just call g2d.rotate(rotation). It'll be much faster to draw the original image rotated than to create a rotated version of the image and then drawing it (note the difference).

A trick you could use is to make a spritesheet of the turret image rotated in 10' steps and just draw the one closest to the true rotation value. (You could alternatively generate 36 rotated sprites at runtime and use them in the same way). Not perfect, but very fast!

A trick you could use is to make a spritesheet of the turret image rotated in 10' steps and just draw the one closest to the true rotation value. (You could alternatively generate 36 rotated sprites at runtime and use them in the same way). Not perfect, but very fast!

I was considering that, if there isn't any faster way to rotate it "live."

The g.rotate is the fastest other then probably doing the sprite sheet. I can confirm that almost all computers will be able to do at least 2k objects changing rotation every frame at 60fps. So I do not think rotation is going to be a huge bottle neck in a TD game unless you have like a biggilion towers which would be silly.

Set RenderingHints on the graphics object.

I recommend setting them in the main graphics object passed around at the beginning of each render. If you find my old post that was a menu demo in java2D, there should also be a source link that shows how I did the rendering which I found to be fast but Java2D unreliable.

Alright, I got it working now. But it's still making jagged edges on my beautiful circles.. :/ But at least I can render lots more towers now, without it starting to drop fps, it's steady around 60 where it would've been ~25 with the above code.

When you render images, I always have it so the location in the world (x,y) is the center of the sprite. If you do that, and then call g.rotate() with the x,y then you will not have that issue.

So if my sprite is 32x64 at 256,432

I will take 256-32/2 and 432-64/2 and draw with the width 32 and height 64. Then have the shadow and main sprite at the same location. If the shadow needs and offset, add it to the loc before rendering.

what is the size of your tower and shadow sprites? are they the same? I would almost always give the width and height even if they are the same. Scaling is rather cheap in java2D and basically free in opengl.

I nice way to draw things I think is to have the width and height already know in a class like sprite so you don't have to use magic constants. This will also allow you to easily change the size without have to do any extra coding.

Ooh! ooh! I know this one! when you call g.rotate(....);, you are providing the coordinates for the origin as the top right corner of the tower. You need to add half of the width to the x position and half of the height to the y position. So it would look like:

The tower and shadow sprite are the same size, 32x32 pixels. And as you can see, then the tower is rendering in the right spot, but the shadow isn't, and the only difference is the "-2" on each axis of the shadow. :/

(In the picture it was something like -5 to show how odd it behaves. If I don't use any offset, then it's rendered correctly, right underneath the tower)

a) rotate based on the coordinates of the shadow, draw shadow, then rotate on the coordinates of the turret and draw the turret.

b) work out the trig to compensate for the angle of the rotation and add it to the draw position of the shadow (hurts my head at this late hour, but I know it is solvable). Don't know if it is faster or slower doing a bit of trig compared to a second rotate.

However, none of this matters if you are managing a pair of good sprite sheets for the turret and shadow.

"We all secretly believe we are right about everything and, by extension, we are all wrong." W. Storr, The Unpersuadables

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org