Ball Rolling

Official forum for the Chipmunk2D Physics Library.
Post Reply
gizzerd91
Posts: 1
Joined: Fri Dec 18, 2009 11:05 pm
Contact:

Ball Rolling

Post by gizzerd91 »

So I've gotten into chipmunk a bit in cocos2d, and have a working scene, with a ball bounding around a bunch of static land blocks. The ball bounces and falls beautifully, but it never rolls. I've been updating the rotation of the ball correctly, because when I manually input a new rotation for the rigidbody, the sprite follows. The trouble is the rigidbody doesn't seem to want to roll on its own. When it collides, it just bounces like it would in a physics sim if the rotation were locked. I was wondering if there is a default lock on rotation, or some other code I was missing to make this work. Here's my (simple) code, if that helps anyone figure out why there's no rolling:

To create my ball:

Code: Select all

	cpBody *ballBody = cpBodyNew(100.0,INFINITY);
	ballBody->p = cpv(160,300);
	cpSpaceAddBody(space, ballBody);
	ballShape = cpCircleShapeNew(ballBody, 10.0, cpv(1.0,4.0));
	ballShape->e = 0.5; // Elasticity
	ballShape->u = 1.0; //Friction
	ballShape->collision_type = 1;
	cpSpaceAddShape(space,ballShape);
To create my floor:

Code: Select all

cpVect blockVerts[] = { cpv(blockOffsetX, blockOffsetY), cpv(blockOffsetX+blockWidth, blockOffsetY), cpv(blockOffsetX+blockWidth,blockOffsetY-blockHeight), cpv(blockOffsetX, blockOffsetY-blockHeight) };
	cpShape *floorShape = cpPolyShapeNew(floorBody, 4, blockVerts, cpv(320.0f / 2, 81.0f / 2));
	floorShape->collision_type=0;
	floorShape->u = 0.5;
	cpSpaceAddStaticShape(space, floorShape);
And finally to move my ball:

Code: Select all

	cpBodyApplyImpulse(ballShape->body, cpv(walking*20,0.0), cpv(0.0,10.0));
	cpSpaceStep(space, 1.0f/30.0f);
	posX = ballShape->body->p.x; //The ball sprites position is set elsewhere, these are value for the enemy AI to use
	posY = ballShape->body->p.y;
	CGFloat radians = ballShape->body->a;
	[ballSprite setRotation:radians*360/3.1415];
Why wont it roll?
mobilebros
Posts: 90
Joined: Tue Aug 04, 2009 9:53 am
Contact:

Re: Ball Rolling

Post by mobilebros »

It doesn't roll because your moment of inertia is set to INFINITY, this line:

Code: Select all

  
cpBody *ballBody = cpBodyNew(100.0,INFINITY);
Try doing this instead:

Code: Select all

cpBody *ballBody = cpBodyNew(100.0, cpMomentForCircle(100.0, 10, 10, cpvzero));
DH01
Posts: 1
Joined: Thu Apr 29, 2010 12:37 am
Contact:

Re: Ball Rolling

Post by DH01 »

Thanks! I was having this same problem and couldn't find a resolution.. You made my day!
Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests