How to rotate an image like in graphic apps

About Monkey 2 Forums Monkey 2 Programming Help How to rotate an image like in graphic apps

This topic contains 6 replies, has 3 voices, and was last updated by  Ratchet 1 year, 8 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #9734

    Ratchet
    Participant

    I got kind of a graphical app where the user can add images and can place it on the screen with drag and drop anywhere. Simple stuff, guess I don’t have to explain, how it works.
    Now I want let the user to rotate the image like you can so in most graphical apps. See the attached GIF/MP4, please.

    First problem: I can’t select the image anymore just by checking if the mouse position inside the image’s rect. Now the rect might be rotated and is more like an polygon. Ok, I know how to check the if a point inside a polygon, BUT

    Second problem: How to rotate a polygon?

    Then more you think about it, then more problems are coming out.

    Does anyone got a good idea how to do this rotation stuff?

    #9737

    abakobo
    Participant

    The most common way to do this is to use matrix transform. It’s what mojo uses. There is an affineMat3 class wich is usable for rotate/size/translate transforms. Matrices are your friends, this is the most usefull Math stuff you can have for video games after trigonometry..

    #9745

    abakobo
    Participant

    here’s a little example of a transformable poly that can be enhanced to get what you need, you can add size too..

    you can extract translate,rotate and size from a AffineMat3 too if you need. see http://monkeycoder.co.nz/forums/topic/matrix-scale/

    #9765

    abakobo
    Participant

    Here is a much more intuitive form of Tpoly.. It’s using position, angle and scale fields instead of affineMat3. But is of course internally using an affineMat3 to transform the original poly (centered at the origin, you could add a center/handle field too and make one more translation to center it to the origin before rotating/scaling).

    #9770

    nerobot
    Participant

    You can rotate images by canvas.DrawImage( image:Image,tx:Float,ty:Float,rz:Float ) where rz is rotation angle.

    Also you need to use image.Handle property to setup rotation anchor for image.

    For detection images by click you can use polygon / rect function like that  or even chipmunk physics collider (I want to use it for complex shapes, but didn’t try yet).

    #9772

    abakobo
    Participant

    Now the rect might be rotated and is more like an polygon. Ok, I know how to check the if a point inside a polygon, BUT

    Second problem: How to rotate a polygon?

    If you only want rotated rects then nerobot’s solution is probably better (more monkeyish)

    #9773

    Ratchet
    Participant

    Great, thank you so much. That will do it.

    I thought about Chipmunk, too. But I guess it’s way oversized for that “simple” stuff I need it for.

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.