0

enter image description here

in short my question is how to connect 2 polygons shapes that are complex and texture them .

here is info about my problem:
i have problem with b2WeldJointDef that do attach 2 b2body's but when i start the debug with e_jointBit turns on. this is my code and i have the image attached

![b2BodyDef bodyDef;
bodyDef.position.Set((screenSize.width/2.0)/PTM_RATIO,(screenSize.height/2.0)/PTM_RATIO);
//CCLOGWARN("position:pX:%f pY:%f",pX,pY);
bodyDef.type=b2_dynamicBody;
bodyDef.userData = sprite;     
bodyDef.linearDamping = 1.0;
bodyDef.angularDamping = 1.0;
b2FixtureDef* fixtureDef=new b2FixtureDef();
fixtureDef->density=3.0;
fixtureDef->restitution=0.0;
fixtureDef->friction=1.0;
fixtureDef->filter.groupIndex=-1;
b2Body* car_body = world->CreateBody(&bodyDef);

// here i build the custom polygons to form complex body 
polygonShape = new b2PolygonShape();
b2Vec2 *vertices = new b2Vec2\[10\];
// setting the vertices all working great : 
...
...
...
polygonShape->Set(vertices,10);
fixtureDef->shape = polygonShape;
car_body->CreateFixture(fd);



// --------  trying to attach the axle to the main car buy 


b2BodyDef axlecontainerBodyDef;
axlecontainerBodyDef.userData = axlecontainerSprite;
axlecontainerBodyDef.type=b2_dynamicBody;
axlecontainerBodyDef.position.Set(((screenSize.width/2.0))/PTM_RATIO,(screenSize.height/2.0)/PTM_RATIO);
b2Body* axlecontainerBody = world->CreateBody(&axlecontainerBodyDef);

b2FixtureDef * axleContainerFixture = new b2FixtureDef();
axleContainerFixture->density=3.0;       
axleContainerFixture->friction=0.0;
axleContainerFixture->restitution=1.0;
axleContainerFixture->filter.groupIndex=-1;  

//LEFT AXLE CONTAINER 
// here i build the custom polygons to form complex new body to be attached to the main car body
polygonShape = new b2PolygonShape();
b2Vec2 *vertices = new b2Vec2\[10\];
// setting the vertices all working great : 
...
...
...
polygonShape->Set(vertices,10);
axleContainerFixture->shape = polygonShape;
axlecontainerBody->CreateFixture(fd);


b2WeldJointDef *weldJointDef = new b2WeldJointDef();

weldJointDef->bodyA=axlecontainerBody;
weldJointDef->bodyB=body;
float x = body->GetWorldCenter().x/PTM_RATIO;
float y = body->GetWorldCenter().y/PTM_RATIO;
weldJointDef->localAnchorA.Set(-0.5, -0.3);
weldJointDef->localAnchorB.Set(-0.5, -0.3);
weldJointDef->referenceAngle = 0* M_PI /3;
//weldJointDef->collideConnected = false;
world->CreateJoint(weldJointDef);][2]

now as you can see in the picture attached the weldjoint is not in the right position , what am i doing wrong ? or what do you thing about using weld joint for this ? is it the right approach?

UPDATE
after reading and changing the code over and over again i still can make the thing work as i need the jonits are all missplaced still : this is my fixed code as you can see it get the center of the car body buy way off the axle body:

 b2BodyDef bodyDef;
    bodyDef.position.Set((screenSize.width/2.0)/PTM_RATIO,(screenSize.height/2.0)/PTM_RATIO);    
    bodyDef.type=b2_dynamicBody;
    bodyDef.userData = sprite;     
    bodyDef.linearDamping = 1.0;
    bodyDef.angularDamping = 1.0;
    b2FixtureDef* fixtureDef=new b2FixtureDef();
    fixtureDef->density=3.0;
    fixtureDef->restitution=0.0;
    fixtureDef->friction=1.0;
    fixtureDef->filter.groupIndex=-1;
    b2Body* car_body = world->CreateBody(&bodyDef);

    // here i build the custom polygons to form complex body 
    polygonShape = new b2PolygonShape();
    b2Vec2 *vertices = new b2Vec2 [10 ];
    // setting the vertices all working great : 
    ...
    ...
    ...
    polygonShape->Set(vertices,10);
    fixtureDef->shape = polygonShape;
    car_body->CreateFixture(fd);



    // --------  trying to attach the axle to the main car buy 


    b2BodyDef axlecontainerBodyDef;
    axlecontainerBodyDef.userData = axlecontainerSprite;
    axlecontainerBodyDef.type=b2_dynamicBody;
    axlecontainerBodyDef.position.Set(((screenSize.width/2.0))/PTM_RATIO,(screenSize.height/2.0)/PTM_RATIO);
    b2Body* axlecontainerBody = world->CreateBody(&axlecontainerBodyDef);

    b2FixtureDef * axleContainerFixture = new b2FixtureDef();

    axleContainerFixture->density=1.0;       
    axleContainerFixture->friction=1.0;
    //axleContainerFixture->restitution=1.0;
    axleContainerFixture->isSensor = false;

    //LEFT AXLE CONTAINER 
    // here i build the custom polygons to form complex new body to be attached to the main car body
    polygonShape = new b2PolygonShape();
    b2Vec2 *vertices = new b2Vec2\[10\];
    // setting the vertices all working great : 
    ...
    ...
    ...
    polygonShape->Set(vertices,10);
    axleContainerFixture->shape = polygonShape;
    axlecontainerBody->CreateFixture(fd);


    b2WeldJointDef *weldJointDef = new b2WeldJointDef();



    weldJointDef->bodyA=body;
    weldJointDef->bodyB=axlecontainerBody;
    float x = axlecontainerBody->GetWorldCenter().x/PTM_RATIO;
    float y = axlecontainerBody->GetWorldCenter().y/PTM_RATIO;
    weldJointDef->Initialize(body,axlecontainerBody,body->GetWorldCenter());
    world->CreateJoint(weldJointDef);

enter image description here

user63898
  • 29,839
  • 85
  • 272
  • 514
  • I have seen several developers using the "b2FixtureDef* fixtureDef = new b2FixtureDef()" idiom. I am used to just creating it on the stack, and using it because I don't want to accidentally forget to do the delete and create a memory leak. Is there a reason the new/delete idiom is preferred? Is there some kind of garbage collection (ARC?)? – FuzzyBunnySlippers Dec 22 '13 at 12:41

1 Answers1

1

This section of your code looks a little suspicious:

weldJointDef->localAnchorA.Set(-0.5, -0.3);
weldJointDef->localAnchorB.Set(-0.5, -0.3);

It seems odd that the local space position relative to the body for both the weld anchor points would be exactly at (-0.5, -0.3). Is this correct?

The last time I had to create a weld joint, I did it like this using the joint's Initialize(...) function.:

   void CreateIdleJoint()
   {
      // Create a weld joint between the
      // body and the thing it is moving on.
      b2WeldJointDef jointDef;
      jointDef.Initialize(_bodyMoving, _bodyMovingOn, _bodyMovingOn->GetWorldCenter());
      jointDef.dampingRatio = 0.5;
      jointDef.frequencyHz = 2.0;
      jointDef.collideConnected = true;
      _bodyMoving->GetWorld()->CreateJoint(&jointDef);
   }

For reference, this is what the inside of the Initialize function looks like:

void b2WeldJointDef::Initialize(b2Body* bA, b2Body* bB, const b2Vec2& anchor)
{
    bodyA = bA;
    bodyB = bB;
    localAnchorA = bodyA->GetLocalPoint(anchor);
    localAnchorB = bodyB->GetLocalPoint(anchor);
    referenceAngle = bodyB->GetAngle() - bodyA->GetAngle();
}
FuzzyBunnySlippers
  • 3,387
  • 2
  • 18
  • 28
  • Thanks for the answer but i still can't make it work , even if i use Initialize method ( that is the same as i did ) . also i noticed that : jointDef.dampingRatio = 0.5; jointDef.frequencyHz = 2.0; breaking the weld action i want to preform – user63898 Dec 22 '13 at 13:47
  • You don't have to set the damping ratio/frequency. I did it in mine because I wanted the spider to stop with a little bounce back when he was walking around the asteroid: http://www.youtube.com/watch?v=yDdJhqwTIWo. – FuzzyBunnySlippers Dec 22 '13 at 13:51
  • Where on the body do you want to set the anchor? Is it coincident with one of the fixtures you have attached? Where is the center of that body in the world space...I think this would be the anchor point you feed to the Initialize(...) function. – FuzzyBunnySlippers Dec 22 '13 at 13:57
  • my body origin is : "x": 0.480000019073486, "y": 0.462499976158142 that is the center of the body cool game in the video by the way – user63898 Dec 22 '13 at 14:12
  • in short my question is how to connect 2 polygons shapes that are complex and texture them – user63898 Dec 22 '13 at 14:26
  • For the second question, this article may help...the post talks about rotating a body with multiple fixtures, all with a separate sprite attached: http://stackoverflow.com/questions/20299960/rotating-multiple-sprites-as-one-around-same-origin/20300196#20300196. The code solution (cocos2d-x, c++, xcode), is here: https://github.com/NonlinearIdeas/RotatedFixtureDemo – FuzzyBunnySlippers Dec 22 '13 at 16:52
  • 1
    I updated the project on github (https://github.com/NonlinearIdeas/RotatedFixtureDemo) to include a weld joint between the bodies. With the debug bits on, you can see it going from the center of the main body to the second. If you remove the CreateJoint(...) in the MainScene code, you can see the rotating box hits the second. Without it, the second body rotates with the first. – FuzzyBunnySlippers Dec 22 '13 at 17:45
  • Thanks allot for your answer i will try this later – user63898 Dec 22 '13 at 19:23
  • ho man you have great tutorials , i need time to read them all , also can you please point me where is the code with the weldjoint ( i want to view it online) many thanks! – user63898 Dec 22 '13 at 19:41
  • 1
    This is the specific one that I altered to add the weld joint: https://github.com/NonlinearIdeas/RotatedFixtureDemo. It was created to answer a specific question on SO, so it's not referenced from any of the tutorials on the site. There are other projects at that github site as well. Feel free to contact me via the site if you have any questions. – FuzzyBunnySlippers Dec 22 '13 at 19:45
  • thanks for you kindness i will use you offer and connect you when i have problem – user63898 Dec 23 '13 at 05:14
  • all so i opened issue in you github repo for fix for windows and cocos2d-x 3 – user63898 Dec 23 '13 at 06:37
  • Your fix is fine, but it leads to a larger question: http://stackoverflow.com/questions/20743976/where-should-system-initialization-methods-be-placed-in-cocos2d-x – FuzzyBunnySlippers Dec 23 '13 at 12:50