Maintaining the relative position of a variably scaled image

About Monkey 2 Forums Monkey 2 Programming Help Maintaining the relative position of a variably scaled image

This topic contains 8 replies, has 5 voices, and was last updated by  cocon 4 days, 19 hours ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #16248

    rusty_shackleford
    Participant

    While starting on a new project I encountered an issue which I, sadly, and over the course of more hours than I care to admit, have been unable to solve. So here it is: I have found that while up-scaling or down-scaling an image, the relative position of a co-ordinant or pixel changes unevenly, such that at the top-right of the image the relative position changes close to a factor of zero. But increases as you get closer to the bottom-left of the image. This uneven scaling makes it quite difficult to maintain the relative position of the image while scaling, which is the purpose I am using it for. This uneven scaling also makes more novel approaches such as changing the offset of the image by adding or subtracting however many pixels in width/height it increases or decreases unusable, as this is different across the image. I have included a sample image and code to help illustrate what I mean by this. Any help would be much appreciated, bonus points for a solution which supports images of varying dimensions.

    #16253

    Abe _King_
    Participant

    I have an idea of what’s happening, but I don’t know exactly what it is you’re asking for. Are you looking for a way to specify an image origin? If I recall correctly, images are rendered based on an origin so if you transform them it is done around said origin. Or perhaps you’re looking for something else?

    #16254

    AdamStrange
    Participant

    imageSample.Handle = New Vec2f( 0.5 )

    Handle is the position when the rotation and scaling is centered.

    0,0 is the default top left corner

    1,1 is the bottom right corner

    0.5,0.5 is the center of the image

    It’s simple – but only when you know how and why 🙂

    #16256

    rusty_shackleford
    Participant

    Setting the origin worked wonderfully for scaling, thank you. Could you tell a way to alter the image offsets so that the relative centered region remains centered? Let’s say the image is centered above spain and the player zooms in, is there a way to maintain that center over spain?

    #16257

    AdamStrange
    Participant

    Handle( 4.35, 2.9 )

    #16258

    abakobo
    Participant

    handle is supposed to be from 0 to 1 for it to be inside the image.. (0,0) being top left and (1,1) bottom right.

    image size is 1285*587

    spain is at (560,170)

    so your handle should be (560/1285,170/587) i.e. (0.435,0.29)

    EDIT: note that dividing an int by an int will give an int. So in your code you’ll have to cast one of them to float: (zoomSpot.x/float(myImage. Width), zoomSpot.y/float(myImage.Height))

    #16259

    rusty_shackleford
    Participant

    Setting the handle works great for just zooming into a point on the image. The issue I am having is that I am unsure of how to alter the offsets of multiple points on the map to maintain their relative positions on the map while increasing scale, without altering the handle besides the initial setting to mid handle. For examples sake, say the player has a unit in south america, a unit in scotland, and a unit in New Zealand, is there an algorithm to alter the positions of these units so that they maintain their relative positions in these countries and don’t end up in the ocean?

    #16261

    abakobo
    Participant

    I’m not sure I get what you want but here is some code where you can move arond the map and zoom/unzoom spain only.
    It uses matrix transfrom which is something used a lot for zoom/rotate/translate. When you move around the map, you don’t move the map but the “camera”(ie the transform) instead. I’ve added a method to canvas to get the transfrom as a “center the camera on this point”.
    You can zoom/unzoom spain with the I and O keys.

    #16262

    cocon
    Participant

    I had previously a similar request where abakobo again offered some help.  🙂

    Trying to pan and zoom

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

You must be logged in to reply to this topic.