Rotating About A Point


(TheHuckleberryWill) #1

I am currently writing a pistol, and using FP.angle to make sure it points towards the mouse.

However, this works fine when the player faces one way, rotating about the top left corner of the pistol, but when the player is facing in the other direction, I want it to rotate about the top right corner.


(Ultima2876) #2

You can set the graphic origin to achieve this like so:

(graphic as Image).originX = (graphic as Image).width;
(graphic as Image).x = -(graphic as Image).width;

I haven’t tested that code but that’s the kind of thing you’ll want to do. The x is set after to counteract the fact that the origin has been modified (otherwise your sprite will jump to a different position suddenly). Also if you’re using a Spritemap for the graphic type, replace “Image” with “Spritemap”. :slight_smile:


(Abel Toy) #3

I’d like to add that I use to keep a

private var img:Image;

thing for my entities, so I don’t have to continuously type-cast the graphics variable.


(Ultima2876) #4

Yep, definitely recommend doing that. I do it too; I used the typecast in the snippet above to keep compatibility maximal.


(TheHuckleberryWill) #5

Thanks! I need to tweak it a bit, but it generally works Ok! :smiley:


(Zachary Lewis) #6

If you’d like to do more advanced rotating in the future, you can use FP.rotateAround().