2

I'm doing a game with SceneKit for which the size of the whole game can change, i.e. the size of the reference node (GameRootNode) to which all nodes are linked can change, then all nodes size change accordingly when it happens.

When it comes to particle system, it is getting tricky. So far I had to do a particle system for each extreme of the size range the game can have, and play with the properties of the particle system to adjust the effect. The result is not good.

So I have tried to scale the particle system with 2 approaches :

  • attach the BulletNode to the GameRootNode, attach the particle system to the BulletNode then scale the GameRootNode (as described below)

  • scale the GameRootNode, create the BulletNode, attach the particle system to the BulletNode then scale the BulletNode to the same scale than the GameRootNode

In both cases, the particle system stays with its original size (i.e. the one defined in the SceneKit editor)

Is there a way to scale particle system?

GameRootNode = SCNNode()
let Sequence = SCNAction.sequence([SCNAction.scale(to: VCOfScene.VCPlayAR.ScalingFactor, duration: 0), 
                                   SCNAction.move(to: SCNVector3.init(-Float(kNbColonnesLevel) / 2 * Float(VCOfScene.VCPlayAR.ScalingFactor), 
                                   0, 
                                  -Float(kNbLignesLevel) / 2 * Float(VCOfScene.VCPlayAR.ScalingFactor)), 
                                   duration: 0)])
GameRootNode.runAction(Sequence)

BulletNode = SCNNode(geometry: Level3D.Bullet.geometry)
BulletNode.position = Position.PositionToVector()
BulletNode.name = ProjectileImageName + String(ProjectileID)

let BulletShape = SCNParticleSystem(named: "Fireball.scnp", inDirectory: nil)!
BulletShape.emittingDirection = DirectionBullet
BulletShape.particleColor = Character.Color

BulletNode.addParticleSystem(BulletShape)
GameRootNode.addChildNode(BulletNode)
GameRootNode.runAction(Sequence)
Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
Jonah Begood
  • 317
  • 2
  • 14

1 Answers1

1

There are two ways to scale a particle system: Shapes' Width,Height,Length as well as particle system's X,Y,Z Scale.

Here's an example how you can scale your particle system using SCNAction:

override func viewDidLoad() {

    super.viewDidLoad()
    let scene = SCNScene(named: "art.scnassets/myScene.scn")!

    //.......................................................

    let particleSystem = SCNParticleSystem()
    particleSystem.emitterShape = SCNBox(width: 1,
                                        height: 1,
                                        length: 1,
                                 chamferRadius: 0)

    particleSystem.birthRate = 50
    particleSystem.particleSize = 0.05
    particleSystem.stretchFactor = 2
    particleSystem.particleLifeSpan = 3
    particleSystem.particleVelocity = 10

    let psNode = SCNNode()
    psNode.addParticleSystem(particleSystem)
    psNode.scale = SCNVector3(1, 1, 1)

    let action = SCNAction.sequence([SCNAction.scale(by: 5, duration: 10)])
    psNode.runAction(action)
    scene.rootNode.addChildNode(psNode)
}

When you scale up a particle system you should increase Birth rate property accordingly. For instance, your scale was (x:1, y:1, z:1) and Birth rate = 50. And you decided to increase a scale to (x:5, y:5, z:5).

In order to retain the same particles' density you have to increase Birth rate property (by 125 times, for example, if your scale factor is 5).

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • Thanks but it seems to be close to what I've tried. The scaling action is done on the node but not on the particle system. You need as you mentioned to change the parameters of the particle system in order to have the same behavior, not only birth rate but also some others. – Jonah Begood Oct 30 '18 at 16:24
  • Ok, I'll try to change some other parameters. Could you tell me what parameters precisely? – Andy Jazz Oct 30 '18 at 16:28
  • Well, you have to play with almost all parameters to conserve the same effect when the scale is changing, especially with complex and multiple particle systems attached to one node. In fact, scaling the node to which the particle system(s) is(are) attached doesn't do anything. I guess that scaling a particle system is not possible. – Jonah Begood Nov 01 '18 at 16:34