SKLabelNode with Children not affected by gravity - not respecting zPosition - ios

I have a wrapper class to create a label node with some subnodes. This is the init for the class:
let label = SKLabelNode(text: word)
let background = SKShapeNode(rectOfSize: size, cornerRadius: 0.5)
let mainNode = SKSpriteNode(color: color, size: background.frame.size)
label.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Center
label.verticalAlignmentMode = SKLabelVerticalAlignmentMode.Center
label.addChild(background)
label.addChild(mainNode)
mainNode.zPosition = 1
background.zPosition = 2
label.zPosition = 5
self.node = label
In my main scene I instantiate the node, set its physics body and set the gravity for the scene:
self.physicsWorld.gravity = CGVectorMake( 0, -10 );
let node = MyNode()
node.node.zPosition = 99
node.node.position.y = endOfScreenTop
if let name = node.node.name as String!, let sprite = node.node.childNodeWithName(name) as? SKSpriteNode {
node.node.physicsBody = SKPhysicsBody(rectangleOfSize: sprite.size)
node.node.physicsBody!.mass = 1.0;
node.node.physicsBody!.angularDamping = 0.0;
node.node.physicsBody!.linearDamping = 0.0;
node.node.physicsBody!.friction = 0.0;
node.node.physicsBody!.affectedByGravity = true
node.node.physicsBody!.dynamic = true
node.node.physicsBody!.allowsRotation = false
}
addChild(node.node)
I have 2 problems:
The label appears behind the SKSpriteNode, even though the zPosition should place it at the front
The SKShapeNode (that's just an insets to smooth the corners) doesn't appear at all
None of the nodes are affected by gravity
PS. Even changing the following to true doesn't change anything:
skView.ignoresSiblingOrder = false
PS2. the MyNode class has these properties
var node: SKLabelNode
var speed: CGFloat = 1.0
var word: String
var color: UIColor
var kind: Kind

Assuming that skView.ignoresSiblingOrder = true in the view controller (the default setting), some thoughts...
When ignoresSiblingOrder is set to true, Sprite Kit ignores sibling relationships when determining the order in which to draw nodes. It does not, however, affect the parent-sibling draw order.
In either mode, the parent is drawn first and then the child nodes are rendered. Normally, the children appear on top of the parent (if they're at the same location) regardless of the zPositions of the parent and children! The exception is if a child's zPosition is less than zero. In this case, the child will appear beneath its parent (even if the parent's zPosition is less than the child).
It doesn't appear that you are setting node.node.name nor the name of the sprite node. If the names aren't set, the if condition will fail and the physics body won't be attached to node.node.
The default settings for shapes only draws a white border around its path (in this case, a rectangle). You should see this as an white outline around the sprite node. Setting background.fillColor = SKColor.greenColor() will show the SKShapeNode filled with green.

Related

Can't detect collision between rootNode and pointOfView child nodes in SceneKit / ARKit

In an AR app, I want to detect collisions between the user walking around and the walls of an AR node that I construct. In order to do that, I create an invisible cylinder right in front of the user and set it all up to detect collisions.
The walls are all part of a node which is a child of sceneView.scene.rootNode.
The cylinder, I want it to be a child of sceneView.pointOfView so that it would always follow the camera.
However, when I do so, no collisions are detected.
I know that I set it all up correctly, because if instead I set the cylinder node as a child of sceneView.scene.rootNode as well, I do get collisions correctly. In that case, I continuously move that cylinder node to always be in front of the camera in a renderer(updateAtTime ...) function. So I do have a workaround, but I'd prefer it to be a child of pointOfView.
Is it impossible to detect collisions if nodes are children of different root nodes?
Or maybe I'm missing something in my code?
The contactDelegate is set like that:
sceneView.scene.physicsWorld.contactDelegate = self so maybe this only includes sceneView.scene, but will exclude sceneView.pointOfView???
Is that the issue?
Here's what I do:
I have a separate file to create and configure my cylinder node which I call pov:
import Foundation
import SceneKit
func createPOV() -> SCNNode {
let pov = SCNNode()
pov.geometry = SCNCylinder(radius: 0.1, height: 4)
pov.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
pov.opacity = 0.3 // will be set to 0 when it'll work correctly
pov.physicsBody = SCNPhysicsBody(type: .kinematic, shape: nil)
pov.physicsBody?.isAffectedByGravity = false
pov.physicsBody?.mass = 1
pov.physicsBody?.categoryBitMask = BodyType.cameraCategory.rawValue
pov.physicsBody?.collisionBitMask = BodyType.wallsCategory.rawValue
pov.physicsBody?.contactTestBitMask = BodyType.wallsCategory.rawValue
pov.simdPosition = simd_float3(0, -1.5, -0.3) // this position only makes sense when setting as a child of pointOfView, otherwise the position will always be changed by renderer
return pov
}
Now in my viewController.swift file I call this function and set is as a child of either root nodes:
pov = createPOV()
sceneView.pointOfView?.addChildNode(pov!)
(Don't worry right now about not checking and unwrapping).
The above does not detect collisions.
But if instead I add it like so:
sceneView.scene.rootNode.addChildNode(pov!)
then collisions are detected just fine.
But then I need to always move this cylinder to be in front of the camera and I do it like that:
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
guard let pointOfView = sceneView.pointOfView else {return}
let currentPosition = pointOfView.simdPosition
let currentTransform = pointOfView.simdTransform
let orientation = SCNVector3(-currentTransform.columns.2.x, -currentTransform.columns.2.y, -currentTransform.columns.2.z)
let currentPositionOfCamera = orientation + SCNVector3(currentPosition)
DispatchQueue.main.async {
self.pov?.position = currentPositionOfCamera
}
}
For completeness, here's the code I use to configure the node of walls in ViewController (they're built elsewhere in another function):
node?.physicsBody = SCNPhysicsBody(type: .dynamic, shape: SCNPhysicsShape(node: node!, options: nil))
node?.physicsBody?.isAffectedByGravity = false
node?.physicsBody?.mass = 1
node?.physicsBody?.damping = 1.0 // remove linear velocity, needed to stop moving after collision
node?.physicsBody?.angularDamping = 1.0 // remove angular velocity, needed to stop rotating after collision
node?.physicsBody?.velocityFactor = SCNVector3(1.0, 0.0, 1.0) // will allow movement only in X and Z coordinates
node?.physicsBody?.angularVelocityFactor = SCNVector3(0.0, 1.0, 0.0) // will allow rotation only around Y axis
node?.physicsBody?.categoryBitMask = BodyType.wallsCategory.rawValue
node?.physicsBody?.collisionBitMask = BodyType.cameraCategory.rawValue
node?.physicsBody?.contactTestBitMask = BodyType.cameraCategory.rawValue
And here's my physycsWorld(didBegin contact) code:
func physicsWorld(_ world: SCNPhysicsWorld, didBegin contact: SCNPhysicsContact) {
if contact.nodeA.physicsBody?.categoryBitMask == BodyType.wallsCategory.rawValue || contact.nodeB.physicsBody?.categoryBitMask == BodyType.wallsCategory.rawValue {
print("Begin COLLISION")
contactBeginLabel.isHidden = false
}
}
So I print something to the console and I also turn on a label on the view so I'll see that collision was detected (and the walls indeed move as a whole when it works).
So Again, it all works fine when the pov node is a child of sceneView.scene.rootNode, but not if it's a child of sceneView.pointOfView.
Am I doing something wrong or is this a limitation of collision detection?
Is there something else I can do to make this work, besides the workaround I already implemented?
Thanks!
Regarding the positioning of your cyliner:
instead to use the render update at time, you better use a position constraint for your cylinder node to move with the point of view. the result will be the same, as if it were a child of the point of view, but collisions will be detected, because you add it to the main rootnode scenegraph.
let constraint = SCNReplicatorConstraint(target: pointOfView) // must be a node
constraint.positionOffset = positionOffset // some SCNVector3
constraint.replicatesOrientation = false
constraint.replicatesScale = false
constraint.replicatesPosition = true
cylinder.constraints = [constraint]
There is also an influence factor you can configure. By default the influence is 100%, the position will immediatly follow.

Physics bodies keep falling away from the sprite node

I am trying to create nodes, that are put into an array, and then are added to the scene with their physics bodies.
Here is the code for creating the initial sprites:
let name = createTarget()
let targetNode = SKSpriteNode(imageNamed: name)
targetNode.name = name
chickenNodes.append(targetNode)
targetNode.position = generateRandomLocation()
let range = SKRange(lowerLimit: targetNode.position.y, upperLimit: targetNode.position.y)
let lockToCenter = SKConstraint.positionY(range)
targetNode.constraints = [lockToCenter]
if movingItems { animateTargets(targetNode) }
Once all of these nodes are in the array, I add them in didMove to a background node, fgNode, in the scene, like this:
for chicken in chickenNodes {
let texture = SKTexture(imageNamed: chicken.name!)
chicken.physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
chicken.physicsBody?.isDynamic = true
chicken.physicsBody?.affectedByGravity = true
chicken.physicsBody?.allowsRotation = false
chicken.physicsBody?.linearDamping = 0.0
chicken.physicsBody?.restitution = 1.0
chicken.physicsBody?.friction = 0.0
fgNode.addChild(chicken)
}
When I view the physics through the scene, the physics bodies keep falling away from the sprite (as though they're responding to the gravity in the scene); and the sprite is just locked where it is. How do I ensure that the physicsBody sticks to the sprite?

SKEffectNode to an SKTexture?

SKEffectionNodes have a shouldRasterise "switch" that bakes them into a bitmap, and doesn't update them until such time as the underlying nodes that are impacted by the effect are changed.
However I can't find a way to create an SKTexture from this rasterised "image".
Is it possible to get a SKTexture from a SKEffectNode?
I think you could try a code like this (it's just an example):
if let effect = SKEffectNode.init(fileNamed: "myeffect") {
effect.shouldRasterize = true
self.addChild(effect)
...
let texture = SKView().texture(from: self)
}
Update:
After you answer, hope I understood better what do you want to achieve.
This is my point of view: if you want to make a shadow of a texture, you could simply create an SKSpriteNode with this texture:
let shadow = SKSpriteNode.init(texture: <yourTexture>)
shadow.blendMode = SKBlendMode.alpha
shadow.colorBlendFactor = 1
shadow.color = SKColor.black
shadow.alpha = 0.25
What I want to say is that you could proceed step by step:
get your texture
elaborate your texture (add filters, make some other effect..)
get shadow
This way of working produces a series of useful methods you could use in your project to build other kind of elements.
Maybe, by separating the tasks you don't need to use texture(from:)
I've figured this out, in a way that solves my problems, using a Factory.
Read more on how to make a factory, from BenMobile's patient and clear articulation, here: Factory creation and use for making Sprites and Shapes
There's an issue with blurring a SKTexture or SKSpriteNode in that it's going to run out of space. The blur/glow goes beyond the edges of the sprite. To solve this, in the below, you'll see I've created a "framer" object. This is simply an empty SKSpriteNode that's double the size of the texture to be blurred. The texture to be blurred is added as a child, to this "framer" object.
It works, regardless of how hacky this is ;)
Inside a static factory class file:
import SpriteKit
class Factory {
private static let view:SKView = SKView() // the magic. This is the rendering space
static func makeShadow(from source: SKTexture, rgb: SKColor, a: CGFloat) -> SKSpriteNode {
let shadowNode = SKSpriteNode(texture: source)
shadowNode.colorBlendFactor = 0.5 // near 1 makes following line more effective
shadowNode.color = SKColor.gray // makes for a darker shadow. White for "glow" shadow
let textureSize = source.size()
let doubleTextureSize = CGSize(width: textureSize.width * 2, height: textureSize.height * 2)
let framer = SKSpriteNode(color: UIColor.clear, size: doubleTextureSize)
framer.addChild(shadowNode)
let blurAmount = 10
let filter = CIFilter(name: "CIGaussianBlur")
filter?.setValue(blurAmount, forKey: kCIInputRadiusKey)
let fxNode = SKEffectNode()
fxNode.filter = filter
fxNode.blendMode = .alpha
fxNode.addChild(framer)
fxNode.shouldRasterize = true
let tex = view.texture(from: fxNode) // ‘view’ refers to the magic first line
let shadow = SKSpriteNode(texture: tex) //WHOOPEE!!! TEXTURE!!!
shadow.colorBlendFactor = 0.5
shadow.color = rgb
shadow.alpha = a
shadow.zPosition = -1
return shadow
}
}
Inside anywhere you can access the Sprite you want to make a shadow or glow texture for:
shadowSprite = Factory.makeShadow(from: button, rgb: myColor, a: 0.33)
shadowSprite.position = CGPoint(x: self.frame.midX, y: self.frame.midY - 5)
addChild(shadowSprite)
-
button is a texture of the button to be given a shadow. a: is an alpha setting (actually transparency level, 0.0 to 1.0, where 1.0 is fully opaque) the lower this is the lighter the shadow will be.
The positioning serves to drop the shadow slightly below the button so it looks like light is coming from the top, casting shadows down and onto the background.

How to calculate an SKPhysicsBody on the fly while animating a sprite?

In my original build, my sprites were static, so to make the collision physics as accurate as I could rather than use rectangleOfSize for SKPhysicsBody passing in the node, I used this tool to define a path SpriteKit's SKPhysicsBody with polygon helper tool.
However now I'm animating the sprites so that they move back and forth during gameplay (obviously my physics path remains static given the above) so my physicsbody no longer matches what the player sees on screen.
The helper tool seemed like a bit of hack that Apple would eventually fix in the API, has there been anything recent in SpriteKit that would help me out here so that I can pass in the node and define a precise physicsbody rather than the hard-coded approach? If not, any other alternatives?
Not sure how performant this is but you can pre-generate the physics bodies for each texture then animate the sprite along with its physicsBody using a custom action.
Here is an example:
func testAnimation() {
var frameTextures = [SKTexture]()
var physicsBodies = [SKPhysicsBody]()
for index in 1...8 {
// The animation has 8 frames
let texture = SKTexture(imageNamed: "guy\(index)")
frameTextures.append(texture)
let physicsBody = SKPhysicsBody(texture: texture, size: texture.size())
physicsBody.affectedByGravity = false
physicsBodies.append(physicsBody)
}
let sprite = SKSpriteNode(texture: frameTextures[0])
let framesPerSecond = 16.0
let animation = SKAction.customAction(withDuration: 1.0, actionBlock: { node, time in
let index = Int((framesPerSecond * Double(time))) % frameTextures.count
if let spriteNode = node as? SKSpriteNode {
spriteNode.texture = frameTextures[index]
spriteNode.physicsBody = physicsBodies[index]
}
})
sprite.run(SKAction.repeatForever(animation));
sprite.position = CGPoint(x: 0, y: 0)
addChild(sprite)
}

Have two of the same nodes on the screen at once Swift SpriteKit

I am creating a space shooter game. I have created an enemy node. What I want to do is have 4 of those nodes in each corner of the screen. In other words, I want to spawn multiple copies of the same node at the same time. When the game loads, I want there to be four nodes, that are all the same. How can I do this?
Thanks
Here's a section of code I use to generate a row of 4 space invaders. I reuse the same SKSpriteNode() variable as once the node has been added to the scene, the variable is no longer required:
for invaderPosition in 100.stride(to: 500, by: 120) {
invader = SKSpriteNode(texture: texturesA[0])
invader.position = CGPoint(x: CGFloat(invaderPosition), y: 200)
invader.xScale = 8
invader.yScale = 6
invader.color = SKColor.redColor()
invader.colorBlendFactor = 1.0
invader.name = "InvaderA"
invader.texture = texturesA[1]
invader.runAction(SKAction.repeatActionForever(SKAction.animateWithTextures(texturesA, timePerFrame: self.timePerMove)))
addChild(invader)
}
invader is defined as an SKSpriteNode property. texturesA is just an array of SKTextures; timePerMove is just a time interval after which the invader should move (in Update) and change shape(texture).
var invader = SKSpriteNode()
var texturesA:[SKTexture] = []
let timePerMove: CFTimeInterval = 1.0
So you could do something similar except the invader's positions would be the 4 corners rather than a row.
Just use the copy command,
let newNode = originalNode.copy() as! SKSpriteNode;

Resources