rmonjo
rmonjo

Reputation: 2745

Create a cpShape from a CGRect in cocos2d and chipmunk

I can't figure out how to create a cpShape that will fit a CGRect. Here is what I tried so far, without any success :

    CGPoint p1 = rect.origin;
    CGPoint p2 = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y);
    CGPoint p3 = CGPointMake(rect.origin.x, rect.origin.y + rect.size.height);
    CGPoint p4 = CGPointMake(rect.origin.x + rect.size.width, rect.origin.y + rect.size.height);

    int num = 4;
    CGPoint verts[] = {p2, p1, p3, p4};
    shape = cpPolyShapeNew(body, num, verts, cpvzero);

I don't understand why this doesn't fit perfectly my CGRect ?

Upvotes: 0

Views: 378

Answers (1)

CodeSmile
CodeSmile

Reputation: 64477

Answered on behalf of user251552

Ok I figured this out:

//Converting points in chipmunk coordinates
    CGPoint p1 = CGPointMake(-rect.size.width/2, -rect.size.height/2);
    CGPoint p2 = CGPointMake(-rect.size.width/2, +rect.size.height/2);
    CGPoint p3 = CGPointMake(+rect.size.width/2, +rect.size.height/2);
    CGPoint p4 = CGPointMake(+rect.size.width/2, -rect.size.height/2);

    int num = 4;
    CGPoint verts[] = {p1, p2, p3, p4};

    shape = cpPolyShapeNew(body, num, verts, cpvzero);

Upvotes: 1

Related Questions