Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.scale(2,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.scale(2,2);
 aGraphics.translate(width/4, height/4);

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

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

share|improve this question
1  
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); –  Gustavo Maciel Jul 9 '13 at 16:10
    
Yep. You've adjusted the second translation backwards; you need to use (width, height) in the second one instead of (width/4, height/4). –  Nathan Reed Jul 9 '13 at 17:04
    
@NathanReed, could you please detail your answer? Yes - it's expected, or Yes - there is a workaround ? –  Andrei Podoprîgora Jul 9 '13 at 18:39
    
@AndreiPodoprîgora I was agreeing with Gustavo's comment. –  Nathan Reed Jul 9 '13 at 20:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.