Change draw order of sprites within one sknode - ios

I need to force/specify the drawing order of all children within node A. I have SKView.ignoresSiblingOrder = NO. If I start setting zPosition on the sprites inside node A (inside update: of the scene), the drawing order would also affect globally so that the children inside node A draws on top of/behind other nodes that are not children of node A, and this messes things up. So I specifically need to adjust the drawing order of the children within node A. How? I can't rearrange the order of the sprite in the children array in update: now can I (performance hit...)?
Update: judging from the SpriteKit reference, for complex scenes we probably have no choice but to set zPosition for everything. I'll do that for now, see if it holds.

Related

SceneKit nodes aren't changing position with scene's root node

I'm using SceneKit with ARKit, and right now have a simple app where I tap on the screen and it adds an ARAnchor and a SCNNode to my scene.
At some point, I'm going to want to move the entire scene, so I tried changing sceneView.scene.rootNode.position.x += 10 to test this out. If I call this on any particular node, that node does move appropriately. But calling this on rootNode, nothing happens, where I'd expect every child node (which is every node in the scene) to move along with it.
Why are my other nodes not moving appropriately, and is there something I can do to fix this? Or am I thinking about this wrong?
Per the docs for SCNScene.rootNode:
You should not modify the transform property of the root node.
The root node defines the origin of the world coordinate system — all other measurements are relative to it. Hence, it's not meaningful (and is often problematic) to change its position, orientation, scale, or any other aspect of its transform.
If you want to move all the content in your SceneKit scene, create a new node to contain all of the others, and change that node's transform. (You can't do this for nodes added by ARSCNView, because ARKit makes those direct children of the root node, but the whole point of those is positioning them in world space.)

Prevent Node From Leaving Boundary

I am using GameplayKit to create paths around obstacles in a given map boundary (lets say a rectangle 1000x1000).
I know that you can make certain nodes avoid "obstacles" when pathfinding, which I am using quite nicely. What I am curious about however is such:
Is there a way to use this same logic and count anything not in the map boundary as an "obstacle"?
A work around would be to create 2 SKNodes and fit them together to create an inner "hole" which becomes 1000x1000, but I am trying to avoid unnecessary addition of nodes if there is a better way. Below I am showing what I could do.
Ideally I want to make the red and black area treated as an obstacle so that all paths remain inside the main square.
UPDATE:
I am using GameplayKit as I have already said, and the pathfinding algorithm can not count regions that are NOT included in a given physics body as an obstacle. It can only count obstacles to be closed polygons that lie within a region. Creating the 2 nodes as shown above works because the pathfinding will now not be able to create any points that lie outside the green rect.
Just have your game layered like this
Scene
SKNode topshape
SKNode bottomshape
SKNode innerbox <-- This is the size of your inner square
SKNode gamenodes <-- Place all inner nodes here
...
Then attach a SKPhysicsBody using an edge loop rectangle the size of innerbox, to the innerbox SKNode and make it a wall category, this will keep all your nodes inside, providing your nodes do not move at insane speeds breaking the engine.
You would be adding 1 additional node instead of 2 (Technically you could make bottomshape and topshape 1 node, making it 0 nodes added), but all processing would get done within the inner node, so not much overhead gets added.

Can parent node positions be moved via child node positions in SceneKit (swift)

I am currently trying to get to grips with SceneKit using Apple's Swift language, it seems nice and straight forward but I was wondering if anyone has been able to move an entire nodes position by changing a child nodes position?
I mean, naturally moving the parent would move the child but I am using snap points in models to position objects correctly and the snap points are children of the overall object node.
For example, I want to snap object A's child node to object B's child node (overall moving object B to the correct offset). I do currently have this implemented, the positions are working correctly, all the correct values are in place however the parent model isn't moving on demand. Is this actually possible or am I following a wrong path?
Thanks in advance :)
that's definitely possible, but you will have to write your own utils.
If T is the transform (translation) that allows you to position your child nodes correctly, you can use [nodeBChildNode convertTransform:T toNode:nodeB] to convert that transform and then apply it on nodeB.

SpriteKit - Changing anchorpoint on node changes position of all childnodes

I'm so freaking confused about anchorpoint in SpriteKit.
If I have a Node consisting of multiple childnodes, and I change the anchorpoint of the Node, then all the childnodes gets misplaced. Is there any way around this?
I think it's really annoying to position nodes if I always use the default anchorpoint (0.5,0.5), so sometimes I change the anchorpoint to for example 1.0,0.0 so it's easily placed in the right lower corner. But if this node has childnodes, then they are all misplaced when I change the anchorpoint.
What am I missing?
Avoid changing the default anchorPoint whenever possible.
If you do change anchorPoint, make sure you do so only on nodes without children.
In your case (and in general) the fix is relatively easy. Instead of adding child nodes to the sprite node whose anchorPoint got changed, use a common parent node (SKNode). Add the sprite node to the parent node, change the sprite's anchorPoint but don't add any children to it. Instead add those children to the parent node. You can then use the parent node to position this branch of the node tree together.
Alternatively create a subclass of (or category on) SKSpriteNode with methods that allow you to align the node to a certain border, taking the sprite's frame size and anchorPoint into account. For example you could add two properties verticalAlign and horizontalAlign with center, left/top and right/bottom alignment. Whenever the position property is changed, the point is offset before being passed to super setPosition so that the alignment mode is adhered to.

How to replicate the effect of a ccsprite being a child of another but using a batch node?

The game I'm designing uses multiple nested CCSprites, so that when the parent sprite moves, rotates, scales, etc. the child sprite does as well, automatically, and so do its grandchildren.
It works great but bogs down very quickly since I haven't implemented batch nodes, currently it's making over 4000 draw calls at once which is obviously not optimal.
However as far as I know the only way to use batch nodes is to make all the sprites a child of the batch node. I thought maybe I could add each parent to the batch node and then add the rest as children of that one but that doesn't work.
Any ideas? I'd like to avoid having to manually calculate the position, rotation, scale, etc of each child sprite every time its parent moves, which at the moment seems to be the only way I can think of to make this work.
Looks like you CAN in fact nest children inside a ccsprite and have them be a part of the batch node, as long as the parent is a child of the batch node. When I first tried this I'd simply forgotten to change "spriteWithFile:" to "spriteWithSpriteFrameName:" when I created the children of one of the sprites. Now it works perfetly, all in one batch node, with one draw call. Hooray!

Resources