In my java game,I have a stickSprite in vertical position.I want to rotate this stick in 90 degrees.so that it can hit the floor,once I press a key.
I used a an array of stick sprites to create a large stick.Why I am doing this because I want to increase the stick length differently at different times.
I am using this method to rotate the stick image:
public Image rotateImage(Image src, float angle) {
int sw = src.getWidth();
int sh = src.getHeight();
int[] srcData = new int[sw * sh];
src.getRGB(srcData, 0, sw, 0, 0, sw, sh);
int[] dstData = new int[sw * sh];
double rads = angle * Math.PI / 180.f;
float sa = (float) Math.sin(rads);
float ca = (float) Math.cos(rads);
int isa = (int) (256 * sa);
int ica = (int) (256 * ca);
int my = -(sh >> 1);
for (int i = 0; i < sh; i++) {
int wpos = i * sw;
int xacc = my * isa - (sw >> 1) * ica + ((sw >> 1) << 8);
int yacc = my * ica + (sw >> 1) * isa + ((sh >> 1) << 8);
for (int j = 0; j < sw; j++) {
int srcx = (xacc >> 8);
int srcy = (yacc >> 8);
if (srcx < 0)
srcx = 0;
if (srcy < 0)
srcy = 0;
if (srcx > sw - 1)
srcx = sw - 1;
if (srcy > sh - 1)
srcy = sh - 1;
dstData[wpos++] = srcData[srcx + srcy * sw];
xacc += ica;
yacc -= isa;
}
my++;
}
return Image.createRGBImage(dstData, sw, sh, true);
}
and rotating the single stick(that is not an element of the array.Stick array elements are placed above this base stick.)like this.It worked well.
private void RotateBaseStick() {
if (smashBool &&angleValue<90) {
angleValue = angleValue + stickMovingSpeed;
rotatedImage = rotateImage(stickImage, angleValue);
stickSprite.setImage(rotatedImage, rotatedImage.getWidth(),
rotatedImage.getHeight());
}
if (angleValue >= 90) {
smashBool = false;
}
}
I am wondering how can I rotate the entire stickArray efficiently to make the feel of a long single stick hitting the ground? Please help me.
