Search code examples
objective-ccocos2d-iphonechipmunkspritebuilder

iOS cocos2D Chipmunk Body's velocity is invalid


In cocos2d-spritebuilder 3.4 I've create my physicworld and my player like many of the official examples.

    self.sprite.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:10 andCenter:self.anchorPoint];
    self.sprite.physicsBody.type = CCPhysicsBodyTypeDynamic;
etc...

So, when I try to move my sprite to the right for example with

[self.sprite.physicsBody applyImpulse: CGPointMake(32,0)];

my app going in crash reporting this:

Aborting due to Chipmunk error: Body's velocity is invalid.
    Failed condition: v.x == v.x && v.y == v.y
    Source:.../Source/libs/cocos2d-iphone/external/Chipmunk/src/cpBody.c:106

Chipmunk source's have:

-(void)applyImpulse:(CGPoint)impulse { _body.velocity = cpvadd(_body.velocity, cpvmult(CCP_TO_CPV(impulse), 1.0f/_body.mass));}

So, I knew MASS was setted by default to 1.0f, but I want to try to set this value by hand..) :

self.sprite.physicsBody.mass = 1.0f

Same error, same results... But if I try to set:

self.sprite.physicsBody.body.mass = 1.0f

It work great. So, what's the differences btw these two parameters? Why applyImpulse take only body.mass?


Solution

  • As far as I know, Chipmunk doesn't play nice when applying a "big" impulse. What you should try and do is build up velocity over several frames.

    Edit: Check this link http://forum.cocos2d-swift.org/t/ccphysics-applyimpulse-not-working/12151/12