Detect collision between particles and nodes in SceneKit - ios

I have few nodes with a Kinematic phyisicsBodyType and I want it to interact with some particles System, and by that I mean that I want to detect collision and execute some code at the moment when node collide with nay particle from an particle system. I've found a way by adding an event handler, but I'm not quite sure how can I use it(link to method here).
Can someone explain me how can I use that event handler(preferred would be to explain me in Swift), or otherwise can you give me another idea?

In the end was very simple. First, you have to assign an array of nodes to the colliderNodes property of particle system. After that, code in swift looks something like that:
particleSystem.handleEvent(SCNParticleEvent.Collision, forPropierties:[SCNParticlePropertyContactPoint], withBlock: {
(data: UnsabeMutablePointer<UnsafeMutablePointer<Void>>, dataStride: UnsafeMutablePointer<Int>, indicies: UnsafeMutablePointer<UInt32>, count:Int) in
//code on detection collision goes here
})
Every parameter is briefly explained right in apple documentation.

Related

SKEmitterNode: modify properties like velocity and angle for individual particles?

The SKEmitterNode in SpriteKit lets you change particle properties, but it's not clear how you change properties for specific particles.
For instance, if we want particles to radiate in a circle shape, it seems we need to dictate the angle and speed for each particle -- not specify values for particles as a group.
Is this possible?
Put another way, is it possible to use the SKEmitterNode to create animations like the one from this video at the 0:22 mark: https://www.youtube.com/watch?v=wYy2G0lVTAM
you can do:
The image is a little star.
setting:
You have no control over individual particles, you can only determine what they are like when they are born. (think of it like a test-tube baby, you can specify what genes you want the baby to have, but after that, the baby will grow however it feels like)
At some point apple may get SKActions working on the particles so that you can do this kind of stuff, but I wouldn't hold my breath on it working anytime soon, they seem to have no care in the SpriteKit platform, just introducing new broken things to get people excited. ( I am cringing on how buggy ARKit will be)

Removing SKNodes When Not Visible On Screen

In my game, the size of the level can be larger than the screen of the phone and the camera will follow the player around the level, so there can be a decent amount of content(such as SKEmitterNodes) in the scene that is not visible at any given time. I've been reading through some of the SpriteKit documentation and found this quote in the SMEmitterNode section:
"Consider removing a particle emitter from the scene when it is not
visible onscreen. Add it just before it becomes visible."
Is this something that can be done in my type of game design? I don't want the nodes to be completely removed since they will eventually be put on the screen, but is there a good way for me to add/remove the EmitterNodes (or other SpriteNodes) that are a certain distance from the screen/is this a good idea to do? I'm looking to improve my frame-rate and don't want costly nodes like SMEmitterNodes working while they're not even being displayed, but will adding/removing them as the player moves around reduce the performance?
Here is the idea I currently have: create a rectangle that extends a certain distance around the screen and detect when a node comes into that rectangle, and if it's not already added to the scene, go ahead and add it. Thank you for any suggestions.
SKNodes really aren't a problem because when they are off screen they are not being rendered anyway, just evaluated. So the main thing to worry about with SKNodes are any physics bodies attached to them,
SKEmitterNodes however require some processing power, and that is why apple is recommending not having them emit if they are not on screen. I would just subclass my SKScene class, and do a checks only on SKEmitterNodes whether or not they are in frame, and emit based on that.
So, I would throw all your SKEmitterNodes into a container like an array, and have a loop function to have the node do a CGRectIntersectsRect check based on your camera location and viewable screen size. and if they intersect, add it to the scene, if not remove it from the scene. The array will keep a strong reference so you do not have to worry about it deiniting on you

iOS Swift SpriteKit Achieving a sprite "explosion" effect

In my game I would like that when a collision occurs, the designated sprite would undergo an "explosion" or "glass break" effect, in which the sprite is split up into random pieces which are then moved at a random rate, speed, and angle. I would imagine that something like this may require using particles or at the very a least texture atlas.
I found a little bit on this, but the questions/explantations were catered for Objective-C. I am fairly new to iOS development and have solely used swift, so I can't really translate from one language to another. Thanks.
I would suggest you to try using the SpriteKit Emitter class for this. Add a new SpriteKit Particle Effect file to the project and configure the type of explosion there. You do not need any code, to configure it as Apple has very conveniently provided an editor window for us to easily change the values.
Once you are satisfied with the way the emitter looks, you can then open the Game Scene (assuming that is where this collision would be detected) and type:
let explosionEmitterNode = SKEmitterNode(fileNamed:"the file name")
sprite.addChild(explosionEmitterNode)
Here sprite is the actual node to which you would like to add the emitter effect to. Or you could add it to the scene directly and set its position as:
let explosionEmitterNode = SKEmitterNode(fileNamed:"the file name")
explosionEmitterNode.position = CGPointMake(200,300)
addChild(explosionEmitterNode)

Hit-testing a UIGestureRecogniser in 3d space

I'm reasonably new to iOS's SceneKit and have come across a dilemma with regards to user-interaction in a 3d scene:
I have a set of SCNNode cubes in an SCNView, and would like to be able to pin-point where a user touches the mesh of a given cube, as a 3d coordinate (so as to later manipulate the scene according to touch vectors). At present, I've been using a UIGestureRecognizer in order to achieve basic hit-testing, but this seems to be limited to returning 2d-points.
This isn't a problem when wanting to hit-test a whole node itself, as this can be achieved via a UIGestureRecognizer's hittest method in the SCNView. However, does anybody have any suggestions as to how to precisely locate where a touch landed on a node, in terms of coordinates (i.e. SCNVector3)?
Thanks!
You are on the right track with calling hitTest:options: on the SCNView. As you have probably seen it results in an array of SCNHitTestResults.
The hit test result can tell you many things about the hit, one of them being what node was hit. What you are looking for is either the localCoordinates or the worldCoordinates.
The local coordinate is relative to the node that was hit. Since you are asking "how to precisely locate where a touch landed on a node" this is probably the one you are looking for.
The world coordinate is relative to the root node.

Blast Radius Spritekit

Im trying to create an explosion that when a certain object is hit, objects in the nearby radius also get destroyed. For example, in a brick breaker game, the ball hits a brick and then the brick explodes but also explodes 2 bricks in every direction as well. How can this be accomplished? Any help is appreciated.
There are lots of ways to do this, but the spritekit way is to create a blast sprite node. You can animate it as needed with SKAction and use the same SpriteKit Physics Contact logic as with other sprites.
Add a physics contact category bit mask for the blast nodes.
When an event occurs that should create the blast, call a method that creates a blast node at the point of the event.
Set the blast node position to the point.
Add any actions to the blast node.
Add the blast node to the scene.
Its actions will run.
In the physics contact delegate method, didBeginContact: you test for for contact with any nodes of categories that should react to contacting the blast node.
If contact is made, basic thing is to tell the contacted node to removeFromeParent.
Once you have that working, you can get fancy.
Your blast node should probably get a physics body of a circle based on a CGPath.
You should probably experiment with actions that scale it up and down.
You can get more sophisticated by adding actions that provide animation, sound etc to the destruction of the other nodes, prior to removing them.
If they are custom subclasses, it's a good time for a protocol that defines the method name to be blown up and removed. Then have each class implement that.
Sounds like work but keeps your game logic code clean.
This overall process is going to be common and one you will get familiar with.

Resources