David Gourde
David Gourde

Reputation: 3914

Center a specific point in an SKShapeNode in Spritekit

I am building a 2D map editor on iPad for a pinball game. To implement a zoom in/out, I decided to scale the pinball table and it's content, which works very well. The problem is that I would like the center of the zoom (which is the center of the scene) to stay in the center.

Right now, if I scale the table up (zoom in), the table scales up and takes all the space in the scene, but the center have nothing to do with the center at the start of the zoom. (see picture below)

enter image description here

As another exemple, if I am zoomed in (the table is scaled up) and I see a part of the table (let's say the right border) and I zoom out, the table will be scaled down from it's center and I will not see the table at all in the scene, even if I had a part of the table in the center of the scene. This is not use friendly and I would like to always have the same portion of the table in the center.

Thank you anyway for your help. I am a bit lost on this one.

PS: I am using a UIPinchGesture to scale my SKShakeNode that is my pinball table.

Upvotes: 1

Views: 249

Answers (1)

Knight0fDragon
Knight0fDragon

Reputation: 16837

If I am understanding this correctly, when you are zooming, you want to zoom in at the spot of the center of the screen, regardless of where your table is. To achieve this, you need to get the distance from the center of the entire scene (Usually you want to try and make this is (0,0) but I believe sprite kits default is (scenewidth/2,sceneheight/2), to the center of the rectangle, scale this distance, then move the center of the rectangle to the new point. so If my screen center is (0,0) and the center of the table is at (0,1) to do a 2x zoom, I would get the distance (0,1) multiply it by 2 (0,2) and place the center of the rectangle at (0,2) now with a 2x size rectangle (Normally you would take your object, translate it to the origin, scale it, then translate it back at the scaled distance, but I believe sprite kit scaling already does this for you, minus the translating back the scaled distance)

Upvotes: 2

Related Questions