No gravity

Official forum for the Chipmunk2D Physics Library.
Post Reply
Monbo
Posts: 6
Joined: Sun Feb 17, 2013 2:53 pm
Contact:

No gravity

Post by Monbo »

Hi

I'm new to ChipMunk - I'm trying to make this Rose move in space - but I'm missing something but can't figure out the problem.

Code: Select all

CCSprite *rose = [CCSprite spriteWithFile:@"rose.png"];
        [rose setPosition:CGPointMake(100, 200)];
        [self addChild:rose z:-1];
        
        cpVect roseVerts[]={
            cpv(-rose.contentSize.width/2, -rose.contentSize.height/2),
            cpv(-rose.contentSize.width/2, rose.contentSize.height/2),
            cpv(rose.contentSize.width/2, rose.contentSize.height/2),
            cpv(rose.contentSize.width/2, -rose.contentSize.height/2)};
        
        cpFloat mass = 100; //5;
        cpBody *roseBody = cpBodyNew(mass, INFINITY);//cpBodyNew(mass, cpMomentForPoly(mass, 4, roseVerts, cpvzero));
        roseBody->p = cpv(100,200);
        cpSpaceAddBody(_space, roseBody);
        
        cpShape *shape =cpPolyShapeNew(roseBody, 4, roseVerts, cpvzero);
        shape->e=0.3;
        shape->u=1.0;
        shape->data=rose;
        cpSpaceAddShape(_space, shape);
        
Hope you can help me though I'm such a newbie.

Maybe guide me to a great resource.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: No gravity

Post by slembcke »

Sounds like you just need to set the space's gravity. Just call cpSpaceSetGravity().

I have some tutorials on the documentation page here:
http://chipmunk-physics.net/documentation.php

In particular you might want to checkout out the Chipmunk Color Match one as it has a variation for Cocos2D 2.1 and the Chipmunk C API.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Monbo
Posts: 6
Joined: Sun Feb 17, 2013 2:53 pm
Contact:

Re: No gravity

Post by Monbo »

Thanks

Gravity is defined by default in the template:

Code: Select all

-(void) initPhysics
{
	CGSize s = [[CCDirector sharedDirector] winSize];
	
	_space = cpSpaceNew();
	
	cpSpaceSetGravity( _space, cpv(0, -100) );
	
	//
	// rogue shapes
	// We have to free them manually
	//
	// bottom
	_walls[0] = cpSegmentShapeNew( _space->staticBody, cpv(0,0), cpv(s.width,0), 0.0f);
	
	// top
	_walls[1] = cpSegmentShapeNew( _space->staticBody, cpv(0,s.height), cpv(s.width,s.height), 0.0f);
	
	// left
	_walls[2] = cpSegmentShapeNew( _space->staticBody, cpv(0,0), cpv(0,s.height), 0.0f);
	
	// right
	_walls[3] = cpSegmentShapeNew( _space->staticBody, cpv(s.width,0), cpv(s.width,s.height), 0.0f);
	
	for( int i=0;i<4;i++) {
		cpShapeSetElasticity( _walls[i], 1.0f );
		cpShapeSetFriction( _walls[i], 1.0f );
		cpSpaceAddStaticShape(_space, _walls[i] );
	}
	
	_debugLayer = [CCPhysicsDebugNode debugNodeForCPSpace:_space];
	_debugLayer.visible = NO;
	[self addChild:_debugLayer z:100];
    
    
    CCSprite *rose = [CCSprite spriteWithFile:@"rose.png"];
    [rose setPosition:CGPointMake(100, 200)];
    [self addChild:rose z:-1];
    
    cpVect roseVerts[]={
        cpv(-rose.contentSize.width/2, -rose.contentSize.height/2),
        cpv(-rose.contentSize.width/2, rose.contentSize.height/2),
        cpv(rose.contentSize.width/2, rose.contentSize.height/2),
        cpv(rose.contentSize.width/2, -rose.contentSize.height/2),};
    
    cpFloat mass = 100; //5;
    cpBody *roseBody = cpBodyNew(mass, INFINITY);//cpBodyNew(mass, cpMomentForPoly(mass, 4, roseVerts, cpvzero));
    roseBody->p = cpv(100,200);
    cpSpaceAddBody(_space, roseBody);
    
    cpShape *shape =cpPolyShapeNew(roseBody, 4, roseVerts, cpvzero);
    shape->e=0.3;
    shape->u=1.0;
    shape->data=rose;
    cpSpaceAddShape(_space, shape);
}
The rose doesn't move but Grisinni does.

Thanks for the link.

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

Re: No gravity

Post by slembcke »

Cocos2D sprites don't automatically follow the movement of Chipmunk bodies. The two libraries are completely separate. You need to set the sprite's position and rotation to the position and rotation of the body each frame.

I did create the CCPhysicsSprite class for a sprite that does automatically follow Chipmunk bodies however. It's officially part of Cocos2D now. You need to enabled CC_CHIPMUNK_INTEGRATION in ccConfig.h to use it though.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Monbo
Posts: 6
Joined: Sun Feb 17, 2013 2:53 pm
Contact:

Re: No gravity

Post by Monbo »

Ok thanks.

I've followed the example from the book Alice on Ipad ( Designing for the iPad) and he used it some kind of that way...
But he doesn't explain the controller.

Now I've implemented it with a node and CCPhysicsSprite *sprite - and now it works.

Thanks for helping me on way :-)

Cheers
Bo
Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests