“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();
}