Programatic shape winding - Not colliding with Segment
Posted: Wed Jul 22, 2009 10:44 pm
I have chipmunk working wonderfully! So awesome
Bullets collide with Enemies etc. When I collide with an enemy, I add its body to the space. Until then, it's a static shape, and I'm doing a static rehash position as I run Cocos2d Actions on the Sprite etc. This all works! The enemy is hit, and I add him to the space (where chipmunk takes over)
My problem, is these enemy bodys do not collide with my "floor" segment!! arg!! I tried reversing the vector coordinates, but nothing seems to work. Actually today was the first time I read about "winding" issues hah.. I can see it's been a headache for many with cocos2d world coords. But i've tried several tips from other posts, no luck.. Here's the code that generates the enemy Body/Shape from the Cocos2d Texture of the given sprite.
That should all be straightforward! Like I said, everything appears to work nicely! the bullets will collide etc. When they DO collide, I do this:
For the segment, I do this:
Lastly, the update stuff:
Very sorry for so much code/postage.. Really struggling to get this working, desperate times as I near midnight 
PS, I love this forum. Code blocks are real slick
-Marco

My problem, is these enemy bodys do not collide with my "floor" segment!! arg!! I tried reversing the vector coordinates, but nothing seems to work. Actually today was the first time I read about "winding" issues hah.. I can see it's been a headache for many with cocos2d world coords. But i've tried several tips from other posts, no luck.. Here's the code that generates the enemy Body/Shape from the Cocos2d Texture of the given sprite.
Code: Select all
-(void) SetupEnemyShapeAndBody
{
CGRect rec = [main_sprite textureRect];
float width = rec.size.width;
float height = rec.size.height;
float halfwidth = width / 2;
float halfheight = height / 2;
float nhalfwidth = halfwidth * -1;
float nhalfheight = halfheight * -1;
int num = 4;
CGPoint verts[] = {
ccp(nhalfwidth,nhalfheight),
ccp(nhalfwidth, halfheight),
ccp( halfwidth, halfheight),
ccp( halfwidth,nhalfheight),
};
body = cpBodyNew(10.0f, cpMomentForPoly(1.0f, num, verts, CGPointZero));
body->p = ccp(main_sprite.position.x, main_sprite.position.y);
shape = cpCircleShapeNew(body, halfwidth, CGPointZero);
shape->e = 1.0; shape->u = 1.0;
shape->data = (void*)self;
shape->collision_type = 1;
shape->group = 1;
cpSpaceAddStaticShape(space, shape);
}
Code: Select all
cpSpaceAddBody(space, body); //if not added already
Code: Select all
- (void) initChipmunk
{
cpInitChipmunk();
space = cpSpaceNew();
space->gravity = ccp(0, -100);
space->elasticIterations = 10;
cpSpaceResizeStaticHash(space, 60.0, 1000);
cpSpaceResizeActiveHash(space, 60.0, 300);
staticBody = cpBodyNew(INFINITY, INFINITY);
//staticBody->p = ccp(0,0); should I be doing this?
cpShape * shape;
shape = cpSegmentShapeNew(staticBody, ccp(0,0), ccp(1000,0) , 0);
shape->e = 0.5f; shape->u = 1.0f;
shape->collision_type = 0;
cpSpaceAddStaticShape(space, shape);
cpSpaceAddCollisionPairFunc(space,1,2, &bulletCollision, self);
cpSpaceAddCollisionPairFunc(space,0,1, &platformCollision, self);
[self schedule: @selector(step:)];
}
Code: Select all
-(void) step: (ccTime) delta {
cpSpaceHashEach(space->staticShapes, &eachShape, NULL);
cpSpaceHashEach(space->activeShapes, &eachShape, NULL);
cpSpaceRehashStatic(space);
int steps = 2;
CGFloat dt = delta/(CGFloat)steps;
for(int i=0; i<steps; i++){
cpSpaceStep(space, dt);
}

PS, I love this forum. Code blocks are real slick
-Marco