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

2015/2/23

Chopped off image when rotated

jimboweb jimboweb

2015/2/23

#
So I have created a general class that allows an image to be animated and to "vertically lock" rotation, so that when you move it only rotates left and right, not up and down. (This is the equivalent of the left-right rotation style in Scratch). I'm acquiring a "newImage" from an array of image names, and rotating the image in the opposite direction of the rotation of the actor, like this:
1
2
3
4
5
6
GreenfootImage newImage = new GreenfootImage(imageNames[currentImage]);
 int rot = theActor.getRotation();
 if(rot>0)
     newImage.rotate(-rot);
//some other stuff to rotate left-right
theActor.setImage(newImage);
And it works, except, when I rotate the image from the vertical or horizontal direction, it chops it off like this: I get that the problem is that it's rotating the image but not changing the image height and width. But not sure what the fix is. Any ideas>
jimboweb jimboweb

2015/2/23

#
Darn it, the image didn't work. Let's just say that the left and right half of the image is cut off. Let me try it again: There, that's what the chopped-off image looks like.
lordhershey lordhershey

2015/2/23

#
Is the goal you want to make an actor who can rotate, but whose image stays upright? Never have seen the new image route done, but I have seen this chopping problem (some thread in the distant past about trying to get and save a rotated image) Some people have made classes that do the former, I do not know what the solution is to the latter, but you could use a Java 2D trick of setting up a transform to rotate the image - the only catch is you have to make sure the target drawing canvas is big enough and a good trick for that is to make sure the targets width and height are the length of the diagonal of the original image. A greenfoot way may be take the image and draw it into a blank image that is of a size that will not cut the image when it is rotated.
danpost danpost

2015/2/23

#
There are two simple solutions to "rotating" an image and not having any part of it chopped off. (1) create a new square blank image whose is base is the larger of the two dimensions of the original and draw the original in the exact middle; then assign that image to the actor and use the getImage().rotate(int) method as you had intended; (2) add an int field to hold the current rotation of the actor and at the end of the act method, save the current rotation and turn to actor back to zero; at the beginning of the act method, return it back to the saved rotation; The second way is more efficient as no image manipulation is needed.
lordhershey lordhershey

2015/2/23

#
Dan, do you ever sleep?
danpost danpost

2015/2/23

#
lordhershey wrote...
Dan, do you ever sleep?
I recharge. I would not call it sleeping. ( I am a robot -- NOT !! ).
lordhershey lordhershey

2015/2/23

#
Being a robot would be great. If I could take a pill and not sleep I would. Sleep is the biggest waste of life.
danpost danpost

2015/2/23

#
@lordhershey, I was incorrect and you were correct that the blank image to use needs to be the length of the long diagonal of the original image. That would work for all images. One of the major problems in rotating a larger image is that collision detection will also be taking in a larger area because of the amount of transparency around the actual image. Using a field to hold the current rotation does not have that problem; although, it would require that all collision detection with the actor be done in its class (not having other actor classes try to detect this actor). This is because its "true" state is only realized during the execution of its own act method.
lordhershey lordhershey

2015/2/23

#
I go with the remember what the rotation is suppose to be and just place it and set it back when needed - otherwise collisions get wonky.
jimboweb jimboweb

2015/2/24

#
Oh, yeah, I should have thought of that. Rotate the actor instead of the image. I'll do it that way.
You need to login to post a reply.