I am trying to implement zoom using Slick2D library.

As I understand, to zoom something on non (0,0) coordinates, we need:

  • translate so the target point is in (0,0)
  • apply scale factor
  • translate the target point back from (0,0)

Code example :

 aGraphics.translate(-width/2, -height/2);
 aGraphics.translate(width/2, height/2);

The issue is, as far I understand, that the scale factor is applied to the translation too. To compensate this, I have use the next code :

 aGraphics.translate(-width/2, -height/2);
 aGraphics.translate(width/4, height/4);

Is this expected behaviour for graphics ? Can I workaround the above effect using push/pop matrix ?

  • 1
    \$\begingroup\$ After you scale your object by scale, your coordinates will be scale times bigger. So try multiplying the offset by the scale factor, after the scale have been applied. aGraphics.translate(offsetX*scaleX, offsetY*scaleY); \$\endgroup\$ – Gustavo Maciel Jul 9 '13 at 16:10
  • \$\begingroup\$ Yep. You've adjusted the second translation backwards; you need to use (width, height) in the second one instead of (width/4, height/4). \$\endgroup\$ – Nathan Reed Jul 9 '13 at 17:04
  • \$\begingroup\$ @NathanReed, could you please detail your answer? Yes - it's expected, or Yes - there is a workaround ? \$\endgroup\$ – StKiller Jul 9 '13 at 18:39
  • \$\begingroup\$ @AndreiPodoprîgora I was agreeing with Gustavo's comment. \$\endgroup\$ – Nathan Reed Jul 9 '13 at 20:45

Seems that slick 2d doesn't consider the center coordinates when scaling. It just multiplies any coordinates using current scale.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.