Change direction of existing particles

1.5k views Asked by At

I'm using SpriteKit's Particle Emitter system to construct a moving star field in the background with the player's ship in the center of the screen.

When the player touches an area of the screen I calculate the angle and animate the player sprite turning that direction.

When I apply it to the star field, however, the entire rectangle the star field is painted on rotates. What I want, however, are for the individual particles to simply start moving in a new direction.

It's the difference between rotating an entire sheet of paper with dots all over it and merely having the dots move toward a new angle. Does that make sense?

Here's what I have so far with the player rotating correctly but the star field "rotating like a whole sheet of paper":

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    // Choose one of the touches to work with
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode:self];
    CGPoint center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

    CGPoint offset = rwSub(location, center);

    SKEmitterNode *starfield = (SKEmitterNode *)[self childNodeWithName:@"starfield"];

    SKSpriteNode *player = (SKSpriteNode *)[self childNodeWithName:@"player"];

    SKAction *rotateNode = [SKAction rotateToAngle: (CGFloat)atan2(offset.y, offset.x) duration:0.5  shortestUnitArc:TRUE];
    [player runAction: rotateNode];

    SKAction *rotateStarfieldNode = [SKAction rotateToAngle: (CGFloat)(atan2(offset.y, offset.x) - M_PI_2) duration:0.5  shortestUnitArc:TRUE];
    [starfield runAction: rotateStarfieldNode];
}
3

There are 3 answers

1
Andrey Gordeev On
0
Larry On

You need to adjust the emissionAngle as follows:

[starField setEmissionAngle:(atan2(offset.y, offset.x) - M_PI_2)];
0
Fogmeister On

What you should probably do is have a particleField node or something and use that as the targetNode for the emitter that you have. (Also, put the emitter on this as a child node also). The particleField will just be a standard SKNode not an emitter. It will have an emitter as a child and all of the particles will be children also.

Treat it like a "particle layer" if you like.

Now you have a single node that "owns" all of the particles and the emitter etc...

The position of the particles and the direction of the particles all depends on the position of this particleField node.

Now you can rotate the particleField and move it around etc... (or scale it, etc...) and all of the particles will move along with it.