开发者

How to change the particle angle in Cocos2D

开发者 https://www.devze.com 2023-03-15 20:16 出处:网络
Now I met a new question. How to modify every particle\'s angle to make it toward the center. Just like the images:

Now I met a new question. How to modify every particle's angle to make it toward the center. Just like the images:

Image 1: normal particles effecing:

How to change the particle angle in Cocos2D

Image 2: which I need:

Image 2: which I need http://tinypic.com/images/404.gi开发者_开发技巧f


How about this code? You need to override CCParticleSystemQuad update: or updateQuadWithParticle:newPosition: method for specify the rotation of the particles. CCParticleSystemPoint can't rotate particles.

@interface MyParticleSystem : CCParticleSystemQuad
@end

@implementation MyParticleSystem
- (void)updateQuadWithParticle:(tCCParticle*)particle newPosition:(CGPoint)pos
{
    particle->rotation = ccpToAngle(particle->pos) * 180.0f / M_PI;
    [super updateQuadWithParticle:particle newPosition:pos];
}
@end


In order to turn particles towards their direction of movement (in your case: towards the center), you can do the following:

  1. Add the oldPos property to the particle tCCParticle struct in CCParticleSystem.h
  2. Initialize the oldPos property with the initial particle position in initParticle: in CCParticleSystem.m
  3. Update the oldPos property with the current particle position in update: in CCParticleSystem.m before the new position is computed. I do this in line 512 immediately after checking whether the particle is still alive.
  4. Override CCParticleSystemQuad as suggested by Kazuki:

    - (void)updateQuadWithParticle:(tCCParticle *)particle 
                       newPosition:(CGPoint)pos
    {
        CGPoint direction = ccpSub(particle->pos, particle->oldPos);
        CGPoint n = ccpNormalize(direction);
        CGFloat a = -CC_RADIANS_TO_DEGREES(ccpToAngle(n) - M_PI_2);
        particle->rotation = a;
    
        [super updateQuadWithParticle:particle newPosition:pos];
    }
    
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号