setBody(b2Body*) Bug?

Hi all!

Help me,please. Is this a Bug

I use Cocos2d-x 3.2 RC0 and Cocostudio 1.5 animation

Here my code

armature = Armature::create("DemoPlayer");
armature->setPosition(Point(500,400));
armature->getAnimation()->playByIndex(0);
armature->setScale(0.25);

this->addChild(armature); 


// Set body for armature 
auto playerPos = Point(armature->getPositionX(), armature->getPositionY());    
b2BodyDef bodyDef2;
bodyDef2.type = b2_dynamicBody;           
bodyDef2.position.Set(playerPos.x/PT_RATIO,playerPos.y/PT_RATIO);
bodyDef2.userData = armature;    

b2Body *playerBody = world->CreateBody(&bodyDef2);    
playerBody->SetGravityScale(10); 
armature->setBody(playerBody);

Sorry I don’t know Chinese!

Help me!

Nobody know or get this bug?

Hix, :6::6::6::6:

Nobody could help you without the error details.

“Nobody could help you without the error details.”

My problem is in above Picture

the body of cowboy is not FIT the armature. Its is far from the armature

But the ball is OK. Why?

This is my code, init fuction

bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();        


//initWorld();

b2Vec2 gravity(b2Vec2(0,-1.0f));
world = new b2World(gravity);
world->SetContinuousPhysics(true);     

auto m_debugDraw = new GLESDebugDraw( PT_RATIO );
world->SetDebugDraw(m_debugDraw);    
world->SetAllowSleeping(false);

uint32 flags = 0;
flags += b2Draw::e_shapeBit;
flags += b2Draw::e_jointBit;
flags += b2Draw::e_aabbBit;
flags += b2Draw::e_pairBit;
flags += b2Draw::e_centerOfMassBit;

m_debugDraw->SetFlags(flags); 

addWall(visibleSize.width ,10,(visibleSize.width / 2) ,0);
addWall(10 ,visibleSize.height ,0,(visibleSize.height / 2) ); 
addWall(10 ,visibleSize.height ,visibleSize.width,(visibleSize.height / 2) );


//------BALL


ball =Sprite::create("ball.png");
ball->setPosition(Vec2(500,400));
this->addChild(ball);

bodyShape.m_radius = 45 / PT_RATIO;

fixtureDef.density=10;
fixtureDef.friction=0.8;
fixtureDef.restitution=0.6;
fixtureDef.filter.categoryBits =1;
fixtureDef.filter.maskBits =1;
fixtureDef.shape=&bodyShape;

bodyDef.type= b2_dynamicBody;
bodyDef.userData=ball;
bodyDef.position.Set(ball->getPosition().x/PT_RATIO,ball->getPosition().y/PT_RATIO);


ballBody = world->CreateBody(&bodyDef);
ballBody->CreateFixture(&fixtureDef);
ballBody->SetGravityScale(10);      


// Load Armature

ArmatureDataManager::getInstance()->addArmatureFileInfo("DemoPlayer0.png","DemoPlayer0.plist","DemoPlayer.ExportJson");     

armature = Armature::create("DemoPlayer");
armature->setPosition(Point(500,400));
armature->getAnimation()->playByIndex(0);
armature->setScale(0.25);
this->addChild(armature); 


// Set body for armature 
auto playerPos = Point(armature->getPositionX(), armature->getPositionY());    
b2BodyDef bodyDef2;
bodyDef2.type = b2_dynamicBody;           
bodyDef2.position.Set(playerPos.x/PT_RATIO,playerPos.y/PT_RATIO);
bodyDef2.userData = armature;    

b2Body *playerBody = world->CreateBody(&bodyDef2);    
//playerBody->CreateFixture(&fixtureDef2);
playerBody->SetGravityScale(10); 
armature->setBody(playerBody);

// filter
b2Filter filter;
filter.categoryBits = 1;
filter.maskBits = 1; 


for (b2Fixture* shapes = armature->getBody()->GetFixtureList(); shapes; shapes = shapes->GetNext())
{
    //shapes->SetSensor(true);
    shapes->SetDensity(5.0f);
    shapes->SetRestitution(1.0f);
    shapes->SetFriction(0.2f); 
    shapes->SetFilterData(filter);     
}
//playerBody->SetFixedRotation(true);       
this->scheduleUpdate();        
return true;

}

and update function

void HelloWorld::update(float dt)
{
int velocityIterations = 8;
int positionIterations = 1;

world->Step(dt, velocityIterations, positionIterations);

for (b2Body *body = world->GetBodyList(); body != NULL; body = body->GetNext())   
    if (body->GetUserData()) 
    {  
        Sprite *sprite = (Sprite *) body->GetUserData();  
        body->SetTransform(body->GetPosition(),body->GetAngle());
        sprite->setPosition(Vec2(body->GetWorldCenter().x*PT_RATIO ,body->GetWorldCenter().y*PT_RATIO));  
        sprite->setRotation(-1 * CC_RADIANS_TO_DEGREES(body->GetAngle())); 

    }

    world->ClearForces(); 
    world->DrawDebugData(); 

}

HUHU

Nobody help?

:8::8::8:

Hi ,kiddi2202;
Can you provide some specific information? I need to test this bug.If you can provide one project.TKS!

The 4th floor is your code?What is the version number you use “Cocos Studio” and "Cocos 2d-x "?

@ hanrea

Thank for your care!

4th is my code. And the problem is shown in the picture

My system is: Cocos Studio 1.5.0.1 newest ? " and "Cocos 2d-x 3.1.1 and 3.2.RC0.

I haven’t tried Cocos 2d-x 3.2 Final yet.

Can you see in my picture? The body of ball, is OK. But the body of armature is not OK. How can I fix this bug:

  • armature->setBody(playerBody); ------- BUG??
    or
  • update function -------------------------------BUG??
    or
  • is my Box2D knowledge not enough? -> BUG?

or

  • Cocostudio + Cocos2d-x bug?

I Found on the internet, some one have the same problem, but nobody can fix this

one of this: http://discuss.cocos2d-x.org/t/adding-body-to-armature-places-it-to-wrong-location/13316

=> I’M VERY BORED.

Do you need all my project? I cand send it to you.

My English is not good, hihi.

hanrea,But didn’t you get this promlem like me? I know you very good at Cocostudio, could you show me a solution to solve this.

Thank you very much!!

Sorry, here are Class, Resource of my Project

Ah, Box2D or Chipmunk intergration is the same bug. HUHU

Sorry, here are Class, Resource of my Project

http://www.mediafire.com/download/3hte120kno6i6u2/HELPme.zip

Ah, Box2D or Chipmunk intergration is the same bug. HUHU

Sorry again, I can’t delete my post above

10 day passed! And nobody help me? :6::6::8::8:

hey,kid.Have you solve this problem?I have read your code,but i get confused in the following code.
body->SetTransform(body->GetPosition(),body->GetAngle()); //I haven’t get it.Maybe you can delete this block.
sprite->setPosition(Vec2(body->GetWorldCenter().xPT_RATIO ,body->GetWorldCenter().yPT_RATIO)); //I guess the problem is here.
Try this:
sprite->setPosition(Vec2(body->GetPosition().xPT_RATIO ,body->GetPosition().yPT_RATIO));