RevoluteJoint is misplaced

Hello,

I want to create a revoluteJoint between 2 bodies, but the joint is totally misplaced and I don't know why..

Code: Select all

#import "NeonSnake.h"
#define PTM_RATIO 32.f

@implementation NeonSnake
@synthesize spriteArray;

+(id)createSnake:(b2World*)world;
{
    NeonSnake* snake = [[NeonSnake alloc]init];
    b2Vec2 vec[20];
    int rand = 0;
    NSArray* array;
#pragma mark Initialisierung Kopf
    rand = 1+arc4random()%10;
    CCSprite* head = [CCSprite spriteWithFile:[NSString stringWithFormat:@"bPart%i.png",rand]];
    head.scale = 0.6;
    head.position = ccp(400,700);

    if (rand == 1)array = [snake bPart1];
    if (rand == 2)array = [snake bPart2];
    if (rand == 3)array = [snake bPart3];
    if (rand == 4)array = [snake bPart4];
    if (rand == 5)array = [snake bPart5];
    if (rand == 6)array = [snake bPart6];
    if (rand == 7)array = [snake bPart7];
    if (rand == 8)array = [snake bPart8];
    if (rand == 9)array = [snake bPart9];
    if (rand == 10)array = [snake bPart10];

    int i = 0;
    for (NSValue* value in array)
    {
        vec[i].Set((head.position.x/PTM_RATIO+[value CGPointValue].x*head.scale),
                   (head.position.y/PTM_RATIO+[value CGPointValue].y*head.scale));
        i++;
    }

    b2BodyDef bodyHeadDef;
    bodyHeadDef.type = b2_dynamicBody;
    bodyHeadDef.userData = head;
    b2Body* bodyHead = world->CreateBody(&bodyHeadDef);
    int count = [array count];

    b2PolygonShape polygonHead;
    polygonHead.Set(vec, count);

    b2FixtureDef fixtureHeadDef;
    fixtureHeadDef.shape = &polygonHead;
    fixtureHeadDef.density = 1.0f;
    fixtureHeadDef.friction = 0.3f;
    bodyHead->CreateFixture(&fixtureHeadDef);
    NSLog(@"KOPF OK");
#pragma mark part 2
    rand = 1+arc4random()%10;
    CCSprite* head2 = [CCSprite spriteWithFile:[NSString stringWithFormat:@"bPart%i.png",rand]];
    head2.scale = 0.5;
    head2.position = ccp(head.position.x,head.position.y-(head2.contentSize.height*head2.scale));

    if (rand == 1)array = [snake bPart1];
    if (rand == 2)array = [snake bPart2];
    if (rand == 3)array = [snake bPart3];
    if (rand == 4)array = [snake bPart4];
    if (rand == 5)array = [snake bPart5];
    if (rand == 6)array = [snake bPart6];
    if (rand == 7)array = [snake bPart7];
    if (rand == 8)array = [snake bPart8];
    if (rand == 9)array = [snake bPart9];
    if (rand == 10)array = [snake bPart10];

    i = 0;
    for (NSValue* value in array)
    {
        vec[i].Set((head2.position.x/PTM_RATIO+[value CGPointValue].x*head2.scale),
                   (head2.position.y/PTM_RATIO+[value CGPointValue].y*head2.scale));
        i++;
    }

    b2BodyDef bodyHeadDef2;
    bodyHeadDef2.type = b2_dynamicBody;
    bodyHeadDef2.userData = head2;
    b2Body* bodyHead2 = world->CreateBody(&bodyHeadDef2);
    count = [array count];

    b2PolygonShape polygonHead2;
    polygonHead2.Set(vec, count);

    b2FixtureDef fixtureHeadDef2;
    fixtureHeadDef2.shape = &polygonHead2;
    fixtureHeadDef2.density = 10.0f;
    fixtureHeadDef2.friction = 0.3f;
    bodyHead2->CreateFixture(&fixtureHeadDef2);
     NSLog(@"KOPF 2 OK");
#pragma mark part 3
    rand = 1+arc4random()%10;
    CCSprite* head3 = [CCSprite spriteWithFile:[NSString stringWithFormat:@"bPart%i.png",rand]];
    head3.scale = 0.4;
    head3.position = ccp(head.position.x,head2.position.y-(head3.contentSize.height*head3.scale));

    if (rand == 1)array = [snake bPart1];
    if (rand == 2)array = [snake bPart2];
    if (rand == 3)array = [snake bPart3];
    if (rand == 4)array = [snake bPart4];
    if (rand == 5)array = [snake bPart5];
    if (rand == 6)array = [snake bPart6];
    if (rand == 7)array = [snake bPart7];
    if (rand == 8)array = [snake bPart8];
    if (rand == 9)array = [snake bPart9];
    if (rand == 10)array = [snake bPart10];

    i = 0;
    for (NSValue* value in array)
    {
        vec[i].Set((head3.position.x/PTM_RATIO+[value CGPointValue].x*head3.scale),
                   (head3.position.y/PTM_RATIO+[value CGPointValue].y*head3.scale));
        i++;
    }

    b2BodyDef bodyHeadDef3;
    bodyHeadDef3.type = b2_dynamicBody;
    bodyHeadDef3.userData = head3;
    b2Body* bodyHead3 = world->CreateBody(&bodyHeadDef3);
    count = [array count];

    b2PolygonShape polygonHead3;
    polygonHead3.Set(vec, count);

    b2FixtureDef fixtureHeadDef3;
    fixtureHeadDef3.shape = &polygonHead3;
    fixtureHeadDef3.density = 10.0f;
    fixtureHeadDef3.friction = 0.3f;
    bodyHead3->CreateFixture(&fixtureHeadDef3);
    NSLog(@"KOPF 3 OK");
#pragma mark part 4
    rand = 1+arc4random()%10;
    CCSprite* head4 = [CCSprite spriteWithFile:[NSString stringWithFormat:@"bPart%i.png",rand]];
    head4.scale = 0.3;
    head4.position = ccp(head.position.x,head3.position.y-(head4.contentSize.height*head4.scale));

    if (rand == 1)array = [snake bPart1];
    if (rand == 2)array = [snake bPart2];
    if (rand == 3)array = [snake bPart3];
    if (rand == 4)array = [snake bPart4];
    if (rand == 5)array = [snake bPart5];
    if (rand == 6)array = [snake bPart6];
    if (rand == 7)array = [snake bPart7];
    if (rand == 8)array = [snake bPart8];
    if (rand == 9)array = [snake bPart9];
    if (rand == 10)array = [snake bPart10];

    i = 0;
    for (NSValue* value in array)
    {
        vec[i].Set((head4.position.x/PTM_RATIO+[value CGPointValue].x*head4.scale),
                   (head4.position.y/PTM_RATIO+[value CGPointValue].y*head4.scale));
        i++;
    }

    b2BodyDef bodyHeadDef4;
    bodyHeadDef4.type = b2_dynamicBody;
    bodyHeadDef4.userData = head4;
    b2Body* bodyHead4 = world->CreateBody(&bodyHeadDef4);
    count = [array count];

    b2PolygonShape polygonHead4;
    polygonHead4.Set(vec, count);

    b2FixtureDef fixtureHeadDef4;
    fixtureHeadDef4.shape = &polygonHead4;
    fixtureHeadDef4.density = 10.0f;
    fixtureHeadDef4.friction = 0.3f;
    bodyHead4->CreateFixture(&fixtureHeadDef4);
    NSLog(@"KOPF 4 OK");
#pragma mark part 5
    rand = 1+arc4random()%10;
    CCSprite* head5 = [CCSprite spriteWithFile:[NSString stringWithFormat:@"bPart%i.png",rand]];
    head5.scale = 0.2;
    head5.position = ccp(head.position.x,head4.position.y-(head5.contentSize.height*head5.scale));

    if (rand == 1)array = [snake bPart1];
    if (rand == 2)array = [snake bPart2];
    if (rand == 3)array = [snake bPart3];
    if (rand == 4)array = [snake bPart4];
    if (rand == 5)array = [snake bPart5];
    if (rand == 6)array = [snake bPart6];
    if (rand == 7)array = [snake bPart7];
    if (rand == 8)array = [snake bPart8];
    if (rand == 9)array = [snake bPart9];
    if (rand == 10)array = [snake bPart10];

    i = 0;
    for (NSValue* value in array)
    {
        vec[i].Set((head5.position.x/PTM_RATIO+[value CGPointValue].x*head5.scale),
                   (head5.position.y/PTM_RATIO+[value CGPointValue].y*head5.scale));
        i++;
    }

    b2BodyDef bodyHeadDef5;
    bodyHeadDef5.type = b2_dynamicBody;
    bodyHeadDef5.userData = head5;
    b2Body* bodyHead5 = world->CreateBody(&bodyHeadDef5);
    count = [array count];

    b2PolygonShape polygonHead5;
    polygonHead5.Set(vec, count);

    b2FixtureDef fixtureHeadDef5;
    fixtureHeadDef5.shape = &polygonHead5;
    fixtureHeadDef5.density = 10.0f;
    fixtureHeadDef5.friction = 0.3f;
    bodyHead5->CreateFixture(&fixtureHeadDef5);

#pragma mark revolute joint
    b2RevoluteJointDef jointDef1;
   b2Joint *joint1;
    jointDef1.Initialize(bodyHead2, bodyHead, bodyHead2->GetWorldCenter());
   jointDef1.collideConnected = NO;
   joint1 = world -> CreateJoint(&jointDef1);

    return snake;
}


I hope some can help....

Original thread: https://box2d.org/forum/viewtopic.php?t=8259