Position the camera correctly when panning

It’s hard for me to set the boundaries and correctly position the camera in my view after panning. So here is my script.

I have a node that is larger than the screen, and I want the user to go around to see the full map. My node is 1000 by 1400 when the view is 640 by 1136 . The sprites inside the node map have a default anchor point. Then I added a camera to the node map and set it to (0.5, 0.5) .

enter image description here

Now I'm wondering if I need to change the position of the camera or map node when the user clicks the screen? The first approach seems problematic, because I can’t just add the translation to the camera position, because the position is defined as (0.5, 0.5), and the translation values ​​are much larger. So I tried to multiply / divide it by screen size, but this does not seem to work. Is the second approach improved?

 var map = Map(size: CGSize(width: 1000, height: 1400)) override func didMove(to view: SKView) { (...) let pan = UIPanGestureRecognizer(target: self, action: #selector(panned(sender:))) view.addGestureRecognizer(pan) self.anchorPoint = CGPoint.zero self.cam = SKCameraNode() self.cam.name = "camera" self.camera = cam self.addChild(map) self.map.addChild(self.cam!) cam.position = CGPoint(x: 0.5, y: 0.5) } var previousTranslateX:CGFloat = 0.0 func panned (sender:UIPanGestureRecognizer) { let currentTranslateX = sender.translation(in: view!).x //calculate translation since last measurement let translateX = currentTranslateX - previousTranslateX let xMargin = (map.nodeSize.width - self.frame.width)/2 var newCamPosition = CGPoint(x: cam.position.x, y: cam.position.y) let newPositionX = cam.position.x*self.frame.width + translateX // since the camera x is 320, our limits are 140 and 460 ? if newPositionX > self.frame.width/2 - xMargin && newPositionX < self.frame.width - xMargin { newCamPosition.x = newPositionX/self.frame.width } centerCameraOnPoint(point: newCamPosition) //(re-)set previous measurement if sender.state == .ended { previousTranslateX = 0 } else { previousTranslateX = currentTranslateX } } func centerCameraOnPoint(point: CGPoint) { if cam != nil { cam.position = point } } 
+5
source share
1 answer

Your camera is actually located at a pixel point of 0.5 points to the right of the center and 0.5 points from the center. At (0, 0) your camera is at the dead center of the screen.

I think that the mistake you made is conceptual, assuming that the reference point of the scene (0.5, 0.5) coincides with the central coordinates of the scene.

If you work in pixels, which seems to you, the camera position (500, 700) will be in the upper right corner of your map, (-500, -700) will be in the lower left.

It is assumed that you are using midpoint binding, which is the default Xcode SpriteKit pattern.

This means that the answer to your question is: literally move the camera around your map, as you wish, since now you will be sure to know that this pixel is literal.

With one caveat ...

many games use restrictions to stop the camera before it reaches the edge of the map so that the map is not half and half on the screen. Thus, the edge of the map is displayed, but the farthest way to move the camera is enough to reveal this edge of the map. This becomes the basis for restrictions when you have a player / character who can walk / move to the edge, but the camera does not go all the way.

+4
source

Source: https://habr.com/ru/post/1263250/


All Articles