Chipmunk physics doesn't start changing scenes with cocos2d

Discuss any Chipmunk bugs here.
Post Reply
returnvoid
Posts: 12
Joined: Sun Jan 22, 2012 5:17 pm
Contact:

Chipmunk physics doesn't start changing scenes with cocos2d

Post by returnvoid »

Hi, I have integrated Chipmunk with Cocos2d and I'm having a lot of problems when cocos2d change scenes. When I have a new scene with Chipmunk implementation physics doesn't start. It happens randomly. Sometimes it starts ok and another didn't start at all.
Logs said the update method is running but the objects position doesn't change.

Chipmunk v6.1.1
cocos2d v2.0.0

Thanks for helping
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Chipmunk physics doesn't start changing scenes with coco

Post by slembcke »

Not nearly enough information to tell you anything.

Chipmunk only "runs" when you call cpSpaceStep() to tell it that time has moved forward. There is also absolutely nothing automatic about how it integrates with Cocos2D. If you aren't calling cpSpaceStep() yourself Cocos2D isn't doing it for you either. Unless you are using the CCPhysicsSprite class that comes with the latest Cocos2D 2.1 betas, there is also nothing that automatically makes sprites move to follow Chipmunk bodies. You have to copy that information over yourself after calling cpSpaceStep().

The two libraries are completely separate, and neither was designed to be used specifically with the other.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
returnvoid
Posts: 12
Joined: Sun Jan 22, 2012 5:17 pm
Contact:

Re: Chipmunk physics doesn't start changing scenes with coco

Post by returnvoid »

Thanks for your reply

I know your library is not responsible about cocos2d and vice versa. But The problem is very specific. My app starts initializing chipmunk [self initPhysics]; Then update method runs and updates the cocos2d sprite. Thats fine. But when I change to a new scene, update methods keeps running (Thread). I wrote a new method when old scene leaves removing displayLink so new scene starts a new [self initPhysics] and so on.

Code: Select all

[displayLink removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

My question is if I need to remove displayLink everytime a new space instance enters

-(void) initPhysics{
    space = [[ChipmunkSpace alloc] init];
    space.gravity = ccp(0, -5);
    [space addBounds:CGRectMake(22, 22, 980, 726) thickness:10.0f elasticity:1.0f friction:1.0f layers:CP_ALL_LAYERS group:CP_NO_GROUP collisionType:@"borderType"];
    multi = [[ChipmunkMultiGrab alloc] initForSpace:space withSmoothing:cpfpow(0.8, 60.0) withGrabForce:500000];
    displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
    displayLink.frameInterval = 1;
    [displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
}

- (void)update {
	cpFloat dt = displayLink.duration*displayLink.frameInterval;
	[space step:dt];
       [sprite updatePosition];
}
thanks
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Chipmunk physics doesn't start changing scenes with coco

Post by slembcke »

Why not use a Cocos2D update method instead of trying to mix display links and other Cocos Scheduled code. That seems like it would create a timing disaster. The order that display links run in is undefined AFAIK and they don't get cleaned up for you automatically when changing Cocos scenes (which I suspect is your problem).
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
returnvoid
Posts: 12
Joined: Sun Jan 22, 2012 5:17 pm
Contact:

Re: Chipmunk physics doesn't start changing scenes with coco

Post by returnvoid »

Ok, I will try what you suggest

thanks
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests