How to use Physics with Armature

Hi all!

I like using CocoStudio to create animation in game, but I have a Trouble: It’s Physic

How to build physics body with Armature, my code is (I use Box2D)

auto playerPos = Point(armature->getPositionX(), armature->getPositionY());	

b2BodyDef bodyDef2;
bodyDef2.type = b2_dynamicBody;	   	
bodyDef2.position.Set(playerPos.x/PTM_RATIO, playerPos.y/PTM_RATIO);
bodyDef2.userData = armature;


b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(3.0f, 3.0f);//These are mid points for our 1m box

b2FixtureDef fixtureDef2;
fixtureDef2.shape = &dynamicBox;
//fixtureDef2.isSensor = true;
fixtureDef2.density=10;
fixtureDef2.friction=0.8;
fixtureDef2.restitution=0.6;

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

armature->setBody(playerBody);

b2Filter filter;
filter.categoryBits = 2;
filter.maskBits = 2;

for (b2Fixture* shapes = armature->getBody()->GetFixtureList(); shapes; shapes = shapes->GetNext())
{
	//shapes->SetSensor(true);
	//shapes->SetDensity(10.0f / CC_CONTENT_SCALE_FACTOR());
	//shapes->SetRestitution(1.0f);
	//shapes->SetFriction(0.2f); 
	shapes->SetFilterData(filter);
}

playerBody->SetFixedRotation(true);

But, my player has a SQUARE body, it’s not good.

Any body know how to build a body for other parts of armature ( legs, hands, body,…),

Using Box2d or Chipmunk or vertex?

What is contour in CocoStudio, how to use it to set Body?

Help me!!!

Hey kiddi2202

I am not sure if you mean this but as I understood you want to know how to draw fixtures for an armature which will be added to body. Here is a Image where I show how to do that. After that you can just use your code to create a body all these fixtures will be loaded in.

I knew how to draw fixtures in cocostudio, but how to use this fixtures in cocos2d-x?

Do you use Box2D, Chipmunk or Verties to create physics?

I use box2d but I haven’t tried to get a fixture of one part of the body. I am sorry :slight_smile:

Hix!

So, how do you create body for a complex character. You use a rectang, circle ?? it’s funy. hihi

What do you mean? Just in box2d or in cocostudio? And have you tried to change the PT_RATIO at CCColliderDetector.h? Could you mayby send me a screenshot with what you see in-game.

@egordm

Help me, this is my Class, Resource of project. Could you fix it?

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

IMAGE

You must import 4 library: Box2D, Cocostudio, UI, Extension,

set ENABLE_PHYSICS_BOX2D_DETECT 1 in CCArmatureDefine.h

Thank you so much!

Hey,
I have tried your project and I found out you have the same problem as I had. The armature is following the wrong coordinate for its body. Here is a small explaination what i mean. I have scales the level by 0.2f. Here is code which you had.

As you can see there is a body but it’s on the wrong loaction. Now I set armature to insvisible, and now you can see the mass point or the origin of the armature is on the place where the armature is so its also on wthe wrong location.

if I change the startloaction of the armature to 0,0 then you see the body armature ale both on the same location.

I have had this problem before when I wanted to add a body to my armature but I didn’t succeded to fix this I have no Idea whats wrong with the code It just looks like the fixtures are moving and body also. I have created a topic about this but nobody has answered my question. Here is my helloworldclass code In which you can clearly see whats wrong. The armature is hidden in there and I applied a force to body and you see how the origin of teh body is moving.
The class I added in attachments.HelloWorldScene.zip (2.2 KB)

Thank you so much!

But how to set body shape FIT the armature?

Is this Bug??

I have just changed the coordinates of armature before creating body to Point(0.0f,0.0f).
And I have no idea if this is a bug or I am doing something wrong but I think it’s a bug.

I use chipmunk, instead box2d. Bug is the same. I think that’s Cocostudio bug

I’m very bored. You know that we have 2 animation

  • Spritesheet ( by SpirteFrame )
  • Skeleton ( by Cocostudio )

And I have trouble with both when setting physic body

  • Skeletion: position of body
  • Spritesheet: I can’t change many body ( or fixture? ) when frame change => we have animation with spriteframe, but body doesn’t change => it’s not FIT with animation

Here Physics Body and SPriteSheet Animation

Could you give me solution or your code? Help me please!

Thank you!

I have also no idea how to fix this I guess we have to post it in issues topic? But mayby I have another solution for you which I use in my game together with my armature. I just use PhysicsEditor You can create your fixtures and add to your body you can also create multiple and change them everytime. I just take a screen picture of my armature and load it into physicseditor. Then I create the fixture and then I just add it to my body and set armature as userdata of my body. I hope this will help you.

Thank you for your solution.

Let I try

Are you success with this solution, could you show me a image?