0
\$\begingroup\$

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.enter image description here

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.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

You should have a method which rotates an image using a pivot point.

Then it does not matter how many sticks there are, you always use the same pivot point to make the images rotate. In your case, the pivot point would be the bottom right corner of your right stick. Below enjoy my paint's skills.

enter image description here

\$\endgroup\$
2
  • \$\begingroup\$ Thank you!Will you please explain how can I set pivot point of rotation in Java Me? \$\endgroup\$
    – Niranjana
    Commented Jan 18, 2017 at 11:34
  • \$\begingroup\$ It's not about setting a pivot point in Java Me, it's about taking a sheet of paper and thinking mathematically what it means to use a pivot point. If your object is represented by a Position and a Rotation, how are the two going to change during a rotation? You just need some basic trigonometry IMHO \$\endgroup\$ Commented Jan 18, 2017 at 14:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .