This site requires JavaScript, please enable it in your browser!
Greenfoot back
GreenGoo
GreenGoo wrote ...

2013/4/12

Image rotation

1
2
GreenGoo GreenGoo

2013/4/12

#
In Greenfoot, the actor rotates around the centre of it's image. Is there any way to manually designate this point without having to alter the image?
danpost danpost

2013/4/12

#
Are you wanting your actor to "rotate" around a point other than the center-point?
GreenGoo GreenGoo

2013/4/12

#
I know. I phrased my question badly. I have a turret which rotates around the centre of the image, and thus looks wrong. I would like it to rotate around a different point on the picture. Is there any way I can do this without altering the picture (so the that the centre is now where I want it to be, perhaps by a adding a line at the back)? So yes, in answer to your question.
danpost danpost

2013/4/12

#
Let us say you wanted the point (rotX, rotY) to be the center-point of rotation (not the center-point of the image of the actor). Then, without altering the original, but by drawing the original on a larger image-base, we can achieve this effect.
1
2
3
4
5
6
7
8
9
int wide = getImage().getWidth();
int high = getImage().getHeight();
int xOff = wide/2-rotX;
int yOff = high/2-rotY;
int xDiff = (int)Math.abs(xOff);
int yDiff = (int)Math.abs(yOff);
GreenfootImage base;
base = new GreenfootImage(wide+2*xDiff, high+2*yDiff);
base.drawImage(getImage(), xDiff+xOff, yDiff+yOff);
I believe that last line is correct (will not have time to test it right now). If it does not turn out right, try subtracting on the last line (instead of adding). EDIT: on second thought, maybe it should be subtraction, there. Sorry, I am in a bit of a rush, here. Later.
GreenGoo GreenGoo

2013/4/12

#
This does compile, but produces a bizarre result. I put in setImage(base), and when I click run the turret teleports to the top left of the screen. When I use the arrow keys to rotate it, it goes in a massive circle, with the starting co-ordinates in the centre.
danpost danpost

2013/4/12

#
rotX and rotY are offsets from the top-left of the image of the turret (not world coordinates).
danpost danpost

2013/4/12

#
The value of rotX should be between zero and the width of the image of the turret. The value of rotY should be between zero and the height of the image of the turret.
GreenGoo GreenGoo

2013/4/12

#
Okay, I understand now, I'll see how it works.
GreenGoo GreenGoo

2013/4/12

#
It still rotates around the centre of the image.
danpost danpost

2013/4/12

#
You could use:
1
2
int rotX = getImage().getWidth()/2 + /* x-offset from center of image*/;
int rotY = getImage().getHeight()/2 + /* y-offset from center of image */;
Oh, and yes, I see I forgot to 'setImage(base);' at the end of the code.
danpost danpost

2013/4/12

#
BTW, I finally got a chance to test it. The code above (after setting the image) works as I had thought it would.
danpost danpost

2013/4/12

#
If you are still having issues, post the code you are using (the Turret class).
GreenGoo GreenGoo

2013/4/12

#
Hold on, I've just realised something.
GreenGoo GreenGoo

2013/4/12

#
Here is the code. The turret shoots off to the bottom of the screen import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class TurretGun extends Turret { public void act() { int rotX = getImage().getWidth()/2 + 0 ; int rotY = getImage().getHeight()/2 - 28; int wide = getImage().getWidth(); int high = getImage().getHeight(); int xOff = wide/2-rotX; int yOff = high/2-rotY; int xDiff = (int)Math.abs(xOff); int yDiff = (int)Math.abs(yOff); GreenfootImage base; base = new GreenfootImage(wide+2*xDiff, high+2*yDiff); base.drawImage(getImage(), xDiff + xOff, yDiff + yOff); setImage(base); if (Greenfoot.isKeyDown("left")) { turn(-1); } if (Greenfoot.isKeyDown("right")) { turn(1); } } }
danpost danpost

2013/4/12

#
The problem is you only need to set the image once (when it is created). Therefore, the code to fix the image should be in the TurretGun constructor. Change the class code to the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import greenfoot.*;
 
public class TurretGun extends Turret
{
    public TurretGun()
    {
        int wide = getImage().getWidth(); 
        int high = getImage().getHeight(); 
        GreenfootImage base; 
        base = new GreenfootImage(wide, high+56); 
        base.drawImage(getImage(), 0, 56);
        setImage(base);
    }
 
    public void act()
    {
        if (Greenfoot.isKeyDown("left"))
        {
            turn(-1);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turn(1);
        }
    }   
}
Since you are only moving the point of rotation up along the y-axis and not changing the x-axis value of the point of rotation, I removed the un-necessary code related to the x-axis coordinate change and simplified the code.
There are more replies on the next page.
1
2