Is it generally better for performance to try and rotate an image “on the fly” using an equation, or to load in a series of pre-rotated images and display them as needed?
Which method is usually practiced with Java 1.1?
Thanks,
Drew
Is it generally better for performance to try and rotate an image “on the fly” using an equation, or to load in a series of pre-rotated images and display them as needed?
Which method is usually practiced with Java 1.1?
Thanks,
Drew
It depends what you are doing as to whether it is feasable to pre-calculate the rotations.
It is always faster, but will always use more memory.
[quote]It depends what you are doing as to whether it is feasable to pre-calculate the rotations.
[/quote]
Well, let’s say it’s a spaceship image/sprite that I want to control with arrow keys (ala Asteroids, etc.)
It seems like a major hassle to create all of those pre rotated images, but is that the way it’s usually done?
Is there any software to automate this task?
Drew
Depends what degree of freedom you want on the rotation I suppose… if you want the player to be able to rotate the ship by 1 degree each step then if might be worth doing it at runtime.
However you’re going to rotate the ship at runtime (AffineTransform I assume?), just use that at startup to generate the sprites for each of the rotations. Or, if you’re really worried about the minor ammount of time this will add to startup, write a seperate little app to do it.
Either way, loading in an image, applying an AffineTransform and then saving it out isn’t too difficult in Java.
Kev
infact, its pretty trivial…
heres the code taken directly from my 4k racer :-
/********************* START Car Image Loading and Generation *****************/
BufferedImage carImage = ImageIO.read(getClass().getResource(STRING_CAR_FILENAME)); //load the car Image
BufferedImage [] carFrames = new BufferedImage[CAR_ROTATIONS];
for(int i = 0;i < CAR_ROTATIONS;i++)
{
carFrames[i] = gc.createCompatibleImage(CAR_SIZE,CAR_SIZE,Transparency.BITMASK);
Graphics2D cfg2d = carFrames[i].createGraphics();
cfg2d.rotate(((Math.PI*2)*i)/CAR_ROTATIONS, CAR_SIZE/2,CAR_SIZE/2);
cfg2d.drawImage(carImage,(CAR_SIZE-CAR_WIDTH)/2,(CAR_SIZE-CAR_HEIGHT)/2,null);
}
/********************* END Car Image Loading and Generation *******************/
I see, except that I’m using Java 1.1 and there is no Buffered Image or Graphics 2D, etc.
Can I still use this technique with Java 1.1?
Drew
Could you use 1.1 for the game and pre-generate the sprites in a 1.4 app before hand?
I suppose you’re using 1.1 so that it can be web based?
Kev
oh yeah, 4got your using java1.1.
Its a little more complex then, you need to either write the rotation algorithm yourself, or find 1 on the web somewhere.
This might be somewhere close:
// sorta pseudo code
double xd = Math.sin(ang);
double yd = Math.cos(ang);
double xp;
double yp;
for (int x=0;x<imageWidth;x++) {
for (int y=0;y<imageHeight;y++) {
int pixel = getPixelFromImage(x,y);
drawPixelAt((int) xp,(int) yp,pixel)
yp += yd;
}
xp += xd;
yp = 0;
}
// save the image here
You’ll probably have to add an offset and I’ve just assumed you know how to get a pixel from an image and write a pixel to a graphics context.
keep in mind that if you can rotate in 1 degree increments that doesn’t mean you have to have 360 different images… chances are the images are small enough that 32 different angles will work. Just pick the closest match.
Depending on how many rotation you will need to do per frame rotating on the fly make work just as well… For instance if you did a true asteroids clone where you drew the ships and thigns by drawing lines instead of bitmaps, then rotations would be much cheaper because you only need to rotate the endpoints of the lines.
A friend of mine was working on a game where he was rotating images for many sprites on the fly and it performed quite well. But it wasn’t very fast action… a slow moving train and the cars were rotated to fit the track on the fly.