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

2022/5/29

image slightly shifted when rotating

rdFx rdFx

2022/5/29

#
Hi, I have a car (Actor) driving circles on a map which works fine (its only for testing / a small prototype). I tried some different things to acutally rotate the actor and/or the image of the car when it is driving a curve. I create an GFImage which is big enough to hold the image in every rotation state. For now the car is only rotated by 90 degrees. The problem is that the image is slightly shifted when driving to south/west but normal when driving north/east. Is there anything to prevent/fix this without providing an image file for each rotation state? nice: slightly shifted:
RcCookie RcCookie

2022/5/29

#
Generally, you seem to create a problem that does not really exist: there is a "setRotation(int)" method in actor which will show your actor in that orientation automatically, without you having to do the drawing of the rotated image. Regarding the actual problem, I could not replicate your issue. Could it be that the angle you are trying to set is off by +-1? My test: 1.:
GreenfootImage image = new GreenfootImage(200, 200);
image.setColor(Color.RED);
image.drawRect(50, 50, 100, 100);
addObject(new Actor(){{setImage(image);}}, 100, 100);
2:
GreenfootImage image = new GreenfootImage(200, 200);
image.setColor(Color.RED);
image.drawRect(50, 50, 100, 100);
image.rotate(180);
addObject(new Actor(){{setImage(image);}}, 100, 100);
3.:
GreenfootImage image = new GreenfootImage(200, 200);
image.setColor(Color.RED);
image.drawRect(50, 50, 100, 100);
image.rotate(181);
addObject(new Actor(){{setImage(image);}}, 100, 100);
RcCookie RcCookie

2022/5/29

#
Well, Google Drive doesn't seem to work for the images, you're just gonna have to believe me that both no rotation and 180 degrees of rotation look exactly the same, while 181 degrees looks pretty much like yours, just slightly off at the ends. You may replicate this test yourself.
danpost danpost

2022/5/29

#
It is usually best to start with an image that is facing east (right). That way the angle of rotation of the actor is the angle the actor is facing. Also, if your actor moves slowly at an angle, you may find that it does not quite move in the exact direction it is facing. To resolve this issue, you will need to use a smooth mover engine -- either the SmoothMover class provided by greenfoot or my QActor class (see my Asteroids w/Improved QActor class scenario).
rdFx rdFx

2022/6/9

#
Thanks for your replies! "Could it be that the angle you are trying to set is off by +-1?" Yes. "It is usually best to start with an image that is facing east (right)." I fixed my code and everything looks fine now. Will check out the SmoothMover / QActor if needed. Thanks for the hint.
You need to login to post a reply.