How to using SKEmitterNode effect to drawing like SKShapeNode does? - ios

I want to make a drawing app by using SKEmitterNode effect.
I know that SKShapeNode are used to stroke or fill a CGPath and I can use SKShapeNode to do it,
But what I want is using SKEmitterNode effect like following images to do the same work.
Is it possible ?

Yes, and this is a very good use of SKEmitterNodes.
First, you'll need to use Additive blend mode on your particles to get this effect.
And you should then set the pace (motion) of the particles to 0 in all directions, and not have them impacted by gravity. When they're emitted, they should be stationary.
The particle emitter should move with the touch of the artist/user, and emit at a rate slightly inverse to the rate of change in position of the emitter.

Related

Converting SKSpriteNode into SKShapeNode

I'm working in Swift 3.1. I want to create a shadow for a SKSpriteNode. I'm thinking of doing a SKShapeNode from my SKSpriteNode and giving it some glow and alpha.
But how can I convert SKSpriteNode into SKShapeNode?
If your plan is to make a shadow that looks like your sprite, then using another SpriteNode instead would be advisable. SKShapeNodes have poor performance and the process of converting the sprite into a vector shape will be processing heavy.
You should have read about SKEffectNode because you could use them to create a SpriteNode with your current texture and apply some effects on it to turn your texture black, blured ans maybe even use affine transformation to distort and rotate the texture so it looks like a shadow.
To make things more GPU/CPU efficient in the case where you have many shadows, you could simply put all the shadow sprites as child of a single SKEffectNode that will apply the effects on every shadow sprite all at once. I assume that positionning the shadow under the main sprite would not be a challenge for you.
Having the shadow as a child of the main sprite would force you to put an effect node for every sprite casting a shadow, and would cause the effects to be processed over and over. By putting every shadow as a child of the same node, they will get rendered all at once and the effect will be processed only once every frame.
I hope it helps!
SKShapeNode's are known to have poor performance in SpriteKit. Instead, you should add another SKSpriteNode for the shadow, and add it as a child to the main SKSpriteNode.
If you want the shadow to appear below its parent, you may have to adjust it's relative z-position as well.

Move along uneven physics body with Sprite Kit

I have a physics body that is uneven but not dynamic (the terrain) and a physics body (character) that is dynamic and is on top of the terrain, and I want this character to move along the terrain simulating kind of a "walking" action where it will keep going up the terrain but it won't fall back (or move back) like a ball because of gravity, and to set a maximum tilt so that it does not tip over.
My attempt was to add a force in the direction I want the character to move but this is causing the character to fall back due to gravity, and I don't want to disable gravity because then character won't fall down when going down the terrain.
Thank you very much
this is why using a physics engine for a platforming game is very difficult and not always a great approach..
you can try sprite.physicsBody.allowsRotation = false
might help
Create SKAction to move your character to destination point or dx and dy. Run that SKAction from your character node with the runAction. Like
SKAction.moveByX(...
When the action is finished, gravity will bring the character down.
If the movement is slow, play with the character's physicsBody friction and/or mass.
You can also change the the character's physicsBody.velocity = CGVectorMake(
And your terrain will be better with edgeLoop physicsBody which won't be dynamic.
And with uneven you mean inclined but smooth? If not smooth and with edges, your character may get held up at an edge depending on center of gravity of character and if you are allowing rotation.

Cocos2d - lunar eclipse effect on iPhone

i have a question about achieving an effect like on a lunar eclipse. The effect should look like in the first seconds of this gif. So just like a black shadow which goes over the circle. The ideal situation would be a function where i can passed a parameter in percentage to get this amount as a shadow on the circle:
The problem which i am facing is that my background is an gradient. So it's not possible to have a black circle which moves over the moon to get the effect.
I tried something with CCClippingNode but it looks not nice. Furthermore the clip on the edges was always a bit pixelated.
I thought about using something like a GLSL Shader to achieve the effect but i am not so familiar with GLSL and i can't find an example.
The effect is for an app game developed for an iphone. I use the cocos2d framework in version 3 (the current one).
Has somebody an idea how to get this effect? An idea where i can start to search?
Thank you in advance
The physics behind is simple you change the light shining on the moon. So
I would create a 1D gradient texture representing the lighting conditions
compute each rendered pixel of moon
you obviously have the 2D texture of moon. So you now need to obtain the position of each pixel inside the 1D lighting texture. So if moon is fully visible you are in sunlight. When partially eclipsed then you are in the umbra region. And finaly while total eclipse you are in penumbra region. so just compute the middle point's of the moon position. And for the rest use relative position in the moons motion direction.
So now just multiply the Moon surface with the lighting texture and render the output.
when working you can add the curvature correction
Now you got linerly cutted Moon phases but the real phases are curved as the lighting conditions differs also with radial distance from motion direction and moons center. To fix this you can do
convert the lighting to 2D texture
or shift the texture coordinate by some curvature dependent on the radial distance

How to add lighting effect in SKShapeNode

I am using SpriteKit, does SKLightNode can only work with SKSpriteNode? because there is no shadowCastBitMask property on SKShapeNode, I cannot make SKLightNode work with SKShapeNode. Is there a way can make them work together? Thanks!
SKShapeNode does not have lighting properties like SKSpriteNode. If you create a SKShapeNode you can make it disappear into a shadow but you cannot make it cast a shadow or set any other lighting properties.

Sprite-Kit: Applying physics to a projectile under the influence of moveTo

I am making a top down game where I am having cannons fire and having their projectiles move to the clicked location via the SKAction moveTo:duration. I am supposed to have wind change the trajectory so I have the cannonball implemented as an SKPhysicsBody and I am setting gravity to be a the windspeed since it is the only thing I can find that applies a constant force like wind would. The problem I am having is moveTo is probably the wrong way to be implementing the cannonball. The ball moves according to the path it should but then lands at the tapped location which is not what I want. I can't find a good alternative to moveTo. Any ideas?
You have to solve the projectile equations to determine the force needed and angle to applyImpulse. Solving the equation is only considering the gravity force and impulse force applied to projectile using the applyImpulse with your distances known from your cannons to user's click projectile destination.
Your applyImpulse launches the projectile, you start your wind and other external forces which will have an impact on the projectile path, changing it's destination from that of user's clicked.
You need to know few physics and math to re-arrange the equations and solve them :)
Not sure if you need to do all the advanced math, just set the angle of your cannon and apply impulse to the cannonball, and use the scene.physicsWorld.gravity as your wind, just make sure you also give your cannonball a physicsbody that has dynamic and affectedByGravity set to true.
To calculate the angle, you would do:
let angle = atan2(Double(touch.y - cannon.y),Double(touch.x - cannon.x))

Resources