Set polygonShape to hexagon shape

518 Views Asked by At

I'm using LiquidFun with spritekit in objective c, In my Box2d world I want to create b2PolygonShape for my sprite, my sprite is shown in the image below:

enter image description here

I tried the following code:

b2PolygonShape polygonShape;
b2Vec2 vertices[6];
for (int i = 0 ; i < 6 ; i++) {
floate angle = -i/6.0 * 360 * DEGTORAD;
vertices[i].Set(sinf(angle), cosf(angle));
}

polygonShape.Set(vertices, 6);

It works but it's smaller than the sprite's image and I can't see the physics lines as I can see them in spritekit's physics world by setting:

SkView.showsPhysics = YES;

My question is: How can I set polygonShape exactly to my sprite image size?

Thanks.

1

There are 1 best solutions below

0
Mariam On

It's an awesome feeling when nobody answer your question, then you post an answer for your question :)

It was as simple as these lines:

b2Vec2 Verts[6];

    Verts[5].Set(-(sprite.frame.size.width/2)/DISPLAY_SCALE,0/DISPLAY_SCALE);
    Verts[4].Set(-(0.5*sprite.frame.size.width/2)/DISPLAY_SCALE,(sprite.frame.size.height/2)/DISPLAY_SCALE);
    Verts[3].Set((0.5*sprite.frame.size.width/2)/DISPLAY_SCALE,(sprite.frame.size.height/2)/DISPLAY_SCALE);
    Verts[2].Set((sprite.frame.size.width/2)/DISPLAY_SCALE,0/DISPLAY_SCALE);
    Verts[1].Set((0.5*sprite.frame.size.width/2)/DISPLAY_SCALE,-(sprite.frame.size.height/2)/DISPLAY_SCALE);
    Verts[0].Set(-(0.5*sprite.frame.size.width/2)/DISPLAY_SCALE,-(sprite.frame.size.height/2)/DISPLAY_SCALE);

    b2PolygonShape Shape;
    Shape.Set(Verts,num);

Note that:

const float DISPLAY_SCALE = 32.0;