How to change the particle angle in Cocos2D
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:
Image 2: which I need:
Image 2: which I need http://tinypic.com/images/404.gi开发者_开发技巧fHow 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:
- Add the
oldPos
property to the particletCCParticle
struct in CCParticleSystem.h - Initialize the
oldPos
property with the initial particle position ininitParticle:
in CCParticleSystem.m - Update the
oldPos
property with the current particle position inupdate:
in CCParticleSystem.m before the new position is computed. I do this in line 512 immediately after checking whether the particle is still alive. 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]; }
精彩评论