How to know which SKSpriteNode is affected by collision detection in Swift? - ios

Situation: I have two or more ships on my iOS screen. Both have different attributes like name, size, hitpoints and score points. They are displayed as SKSpriteNodes and each one has added a physicsBody.
At the moment those extra attributes are variables of an extended SKSpriteNode class.
import SpriteKit
class ship: SKSpriteNode {
var hitpoints: Int = nil?
var score: Int = nil?
func createPhysicsBody(){
self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width / 2)
self.physicsBody?.dynamic = true
...
}
}
In this 'game' you can shoot at those ships and as soon as a bullet hits a ship, you get points. 'Hits a ship' is detected by collision.
func didBeginContact(contact: SKPhysicsContact){
switch(contact.bodyA.categoryBitMask + contact.bodyB.categoryBitMask){
case shipCategory + bulletCategory:
contactShipBullet(contact.bodyA, bodyB: contact.bodyB)
break;
default:
break;
}
}
Problem: Collision detection just returns a physicsBody and I do not know how to get my extended SKSpriteNode class just by this physicsBody.
Thoughts: Is it a correct way to extend SKSpriteNode to get my objects like a ship to life? When I add a ship to my screen it looks like:
var ship = Ship(ship(hitpoints: 1, score: 100), position: <CGPosition>)
self.addChild(ship)
Or is this just a wrong approach and there is a much better way to find out which object with stats so and so is hit by a bullet thru collision detection?
This question is similar to my other question - I just want ask this in broader sense.

The SKPhysicsBody has a property node which is the SKNode associated to the body. You just need to perform a conditional cast to your Ship class.
if let ship = contact.bodyA.node as? Ship {
// here you have your ship object of type Ship
print("Score of this ship is: \(ship.score)!!!")
}
Please note that the Ship node could be the one associated with bodyB so.
if let ship = contact.bodyA.node as? Ship {
// here you have your ship...
} else if let ship = contact.bodyB.node as? Ship {
// here you have your ship...
}
Hope this helps.

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.

swift scene.sks file objects not applying z rotation value

I'm making a simple game application for IOS devices, loosely based from the book 'swift game development'. I have created a protocol which I use as a template for creating a class for each type of in game object. A platform for the player to jump on has the following class based on the protocol
import SpriteKit
class GrassyPlatform: SKSpriteNode, GameSprite {
var textureAtlas:SKTextureAtlas = SKTextureAtlas(named: "Enviroment")
var initialSize: CGSize = CGSize(width:630, height:44)
init() {
super.init(texture: textureAtlas.textureNamed("GrassPlatform1"), color: .clear, size: initialSize)
self.anchorPoint = CGPoint(x: 0.5, y:0.5)
physicsBody = SKPhysicsBody(rectangleOf: initialSize)
physicsBody?.restitution = 0
self.physicsBody?.isDynamic = false
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
I'm using the scene editor to place these objects onto the scene, assigning each object the relevant custom class. Like the one above.
When I run the game the objects position (assigned only from the scene editor) is respected, but the zRotation value is ignored. For example setting the platform in the scene editor as so
But this results in the platform appearing at the correct position but with the default zRotation value and not the one assigned in the game scene.
I can adjust the rotation manually through self.zRotation but this defeats the whole point of using the scene.sks for level design.
Is there away to adjust the zRotation through game scene and if so how?
Thanks
Solved the problem was in a piece of code that I never added to the original post because I 'thought' it was irrelevant! boy was I wrong. Another class handles the different scenes. This code I took from the book and was not 100% on its operation when I added it, before getting caught up in something else
class EncounterManager {
let encounterNames:[String] = ["Level1A", "Level1B"] //A array of all the scenes in the level
var encounters:[SKNode] = [] //each scene is a node
var currentEncounterIndex:Int?
var previousEncounterIndex:Int?
init() {
for encounterFileName in encounterNames { //Loop all scenes in the scene array
let encounterNode = SKNode() //create a new node for the encounter/scene
if let encounterScene = SKScene(fileNamed: encounterFileName) { //load the encounter to the skscene
for child in encounterScene.children { //Loop through each child node of the skscene
let copyOfNode = type(of: child).init() //copy the node type and initilize to the encounterNode
copyOfNode.position = child.position //copy the position
copyOfNode.zPosition = child.zPosition //copy of zPosition
copyOfNode.name = child.name //copy the name
encounterNode.addChild(copyOfNode) //add child to encounter node
}
}
encounters.append(encounterNode)
//Save initial sprite positions for this encounter
saveSpritePositions(node: encounterNode)
}
}
This takes a copy of each object node and adds it to the game scene. The origonal author never needed to worry about the rotation of objects. So I added this and it worked
copyOfNode.zRotation = child.zRotation //copy of zRotation
Thanks anyway if you looked.

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)
}

Track the position of SKSpriteNode while doing a SKAction moveTo

I'm trying to figure a way to track a postions for multiple nodes, that spwan randomly on the screen so i can make a changes to them while moving when the reach random postion.
the nodes just move along the x axis and i want to be able to generate random number from 0 to postion.x of the ball, and change the color when it reachs the postion
override func update(currentTime: CFTimeInterval)
i tried tacking changes in update method but as soon as new node appers i lost track of the previos one
i also tried
let changecolor = SKAction.runBlock{
let wait = SKAction.waitForDuration(2, withRange: 6)
let changecoloratpoint = SKAction.runBlock { self.changecolorfunc(self.ball)}
let sequence1 = SKAction.sequence([wait, changecoloratpoint])
self.runAction(SKAction.repeatAction(sequence1, count: 3))
}
but it doesn't give me any control over the random postion
You have already all you needed.
Suppose you have a reference for your node:
var sprite1: SKSpriteNode!
And you want to spawn it to a random position (an example method..):
self.spawnToRandomPos(sprite1)
And suppose you want to moveTo your sprite1:
self.sprite1.runAction( SKAction.moveToY(height, duration: 0))
Everytime you check his position you know where is it:
print(sprite1.position)
So to know always your sprite1 position you could do this code and you see all it's movements:
override func update(currentTime: CFTimeInterval) {
print(sprite1.position)
}
P.S.:
In case you dont have references about your object spawned you can also give a name to a generic object based for example by a word followed to a counter (this is just an example):
for i in 0..<10 {
var spriteTexture = SKTexture(imageNamed: "sprite.png")
let sprite = SKSpriteNode(texture: spriteTexture, size: spriteTexture.size)
sprite.name = "sprite\(i)"
self.addChild(sprite)
}
After this to retrieve/re-obtain your sprite do:
let sprite1 = self.childNodeWithName("sprite1")
There are probably a hundred different ways of doing this. Here is how I would do it.
in your update func
checkForCollisions()
this will just scroll through "obstacles" that you randomly generate, and place whoever you want the color trigger to happen. They can be anything you want just change the name to match. if they overlap your "ball" then you can color one or the other.
func checkForCollisions() {
self.enumerateChildNodesWithName("obstacles") { node, stop in
if let obstacle: Obstacle = node as? Obstacle {
if self.ball.intersectsNode(obstacle) {
//color node or ball whichever you want
}
}
}
}

SpriteKit Magnetic/Electric fields producing no results

I'm playing around with some of the new SpriteKit tools and I've run into a frustrating problem.
As you know, iOS 8 introduced the SKFieldNode class which enables the user to create custom "force fields" which affect other SKNodes. I have gotten great results using the springField and the radialGravityFields, however I have yet to figure out how to use the magneticField and electricField types.
Let me explain.
The following code produces absolutely no effects on node4, the SKSpriteNode which I would like to be affected by the SKFieldNode.
SKSpriteNode *node4 = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:#"Red"] size:CGSizeMake(25.0, 25.0)];
node4.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:12.5];
node4.position = CGPointMake(300.0, 250.0);
node4.physicsBody.dynamic = YES;
node4.physicsBody.charge = 30;
node4.physicsBody.linearDamping = 3;
node4.physicsBody.affectedByGravity = NO;
node4.physicsBody.collisionBitMask = 0;
node4.physicsBody.mass = 1;
SKFieldNode *centerNode = [SKFieldNode magneticField];
centerNode.position = CGPointMake(150.0, 200.0);
centerNode.strength = 100000000000;
centerNode.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:25];
centerNode.physicsBody.charge = -30;
centerNode.physicsBody.dynamic = NO;
centerNode.falloff = 0;
The node is drawn on screen, yet stays completely static. As you can see, this happens even with a falloff of 0 and a strength of a 10000000000.
Changing the line:
SKFieldNode *centerNode = [SKFieldNode magneticField];
Into:
SKFieldNode *centerNode = [SKFieldNode springField];
Causes the centerNode to act as a springField, and sends the node4 all over the place.
Using an electricField, rather than a magneticField, yields no results either.
Before someone asks, yes, I am adding both nodes to the scene, I just didn't include those lines.
Can anyone see where I'm going wrong?
There's not enough code here for me to test it fully, but I see two likely problems up front:
If node4 isn't moving to start with, a magnetic field has no effect on it. SpriteKit's magnetic field models the second half of the Lorentz force equation (F = qv тип B), where the force on a particle relates to its charge and velocity.
You're setting node4's linear damping to greater than the maximum. The linearDamping property takes a value between 0 and 1, where 1.0 fully arrest's a body's motion. Values greater than 1 are probably clamped to 1.0, so your body shouldn't be moving even if hit with tremendous force.
Probably unrelated: you don't need to add a charged physics body to the field node unless you want that node to be affected by other electric/magnetic fields.
I have the same problem only when I create nodes by writing code. If you work with SpriteKit Editor, it works well!
You can add a new sks file and then unarchive it to SKScene by this code:
extension SKScene {
class func unarchiveFromFile(file : NSString) -> SKNode? {
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)
archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as SKScene
scene.size = GameKitHelper.sharedGameKitHelper().getRootViewController().view.frame.size
archiver.finishDecoding()
return scene
} else {
return nil
}
}
}
Open the sks file and drag sprites on the scene, it really works well.

Resources