I can't seem to flip images with DrawImageRect

Monkey Forums/Monkey Beginners/I can't seem to flip images with DrawImageRect

Ponpoko(Posted 2017) [#1]
I can't seem to flip images with DrawImageRect (or any other draw command for that matter). If I put a negative scale factor the image just doesn't draw. I really don't want to double my number of images in order to manually draw the mirrored image if I can just flip them. I'm not sure what I am doing wrong?

This works fine:

DrawImageRect(test_image,0,0,1,1,24,120,0,4,4)

My image is drawn scaled by 4 in both x and y

This doesn't:

DrawImageRect(test_image,0,0,1,1,24,120,0,-4,4)

My expectation would be that the image would be scaled by 4 in both x and y and that x would be flipped left to right. BTW the 4x scale makes no difference. If I scale by 1 I still have the same issue.

Any ideas?


dawlane(Posted 2017) [#2]
You will need to do a little bit of matrix manipulation to do a mirror.
It would go some thing like this:
PushMatrix
Translate x,y ' position the drawing point
Scale x,y ' set scaling here
Translate -x,-y ' reset the drawing point
DrawImageRect(image,x,y,w,h)
PopMatrix

See the documentation on PushMatrix, Translate, Scale, PopMatrix.
Do a search for these of the forum. I'm sure some has some example code around.


Ponpoko(Posted 2017) [#3]
Thanks dawlane... I have been playing with this but I can't seem to make even the simplest texture mirror left to right. Could you possibly write up a short working example? I wanted to set this up as a function call or something from my main rendering loop so I could just sub in a MirrorRect (or whatever I call it) command when I need to. I am just too new to Monkey to understand the matrix stuff and there weren't any examples I could find.


Xaron(Posted 2017) [#4]
Actually it works for me, using negative scale, always did it that way:



Edit: I see where your problem is: When you draw at 0,0 your image is drawn to the left starting at 0,0 using negative scale in x direction, which means it's simply off screen then.


dawlane(Posted 2017) [#5]
OK finally got to have some time to answer this in a much better manner. As Xaron says, that version of DrawImageRect will work. But an image is drawn from a pivot point aka a handle. You can set this to the middle of an image as part of the parameters to load an image (see the bit about flags in the documentation for load image). The default is to set the handle to what would be the top left corner of an image, so when an image is drawn with negative scaling, it draws the mirror to the left or top of this handle. So to keep the image in a desired position; this handle has to be move to what would be the opposite side of the image e.g. it's width and height.



Ponpoko(Posted 2017) [#6]
Thanks guys,

These were great tutorials and I got everything working in my game. I also learned about a few other things like Strict etc. Monkey has quite a few differences from Blitz (I mostly used Blitz +). Thanks for all your help!