English 中文(简体)
How to set the position of a sprite within a box2d body?
原标题:

Basically I have 2 polygons for my body. When I add a sprite for userData, the position of the texture isn t where I want it to be. What I want to do is adjust the position of the texture within the body. Here s the code sample of where I am setting this:

CCSpriteSheet *sheet = (CCSpriteSheet*) [self getChildByTag:kTagSpriteSheet];
CCSprite *pigeonSprite = [CCSprite spriteWithSpriteSheet:sheet rect:CGRectMake(0,0,40,32)];
[sheet addChild:pigeonSprite z:0 tag:kPigeonSprite];

pigeonSprite.position = ccp( p.x, p.y);

bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
bodyDef.userData = sprite;
b2Body *body = world->CreateBody(&bodyDef);

b2CircleShape dynamicCircle;
dynamicCircle.m_radius = .25f;
dynamicCircle.m_p.Set(0.0f, 1.0f);

        // Define the dynamic body fixture.
b2FixtureDef circleDef;
circleDef.shape = &dynamicCircle;   
circleDef.density = 1.0f;
circleDef.friction = 0.3f;

body->CreateFixture(&circleDef);

b2Vec2 vertices[3];
vertices[0].Set(-0.5f, 0.0f);
vertices[1].Set(0.5f, 0.0f);
vertices[2].Set(0.0f, 1.0f);
b2PolygonShape triangle;
triangle.Set(vertices, 3);

b2FixtureDef triangleDef1;
triangleDef1.shape = ▵ 
triangleDef1.density = 1.0f;
triangleDef1.friction = 0.3f;

body->CreateFixture(&triangleDef1);
问题回答

I m not that familiar with objective-c but I ll give it a try.

All I can see is that you are storing a pointer to the sprite object in the body s user data and then leaving it there. If you want the body s position to be transferred to the sprite you need to update it every frame.

In C++ this would look something like this.

// To be called each time physics should be updated.
void physicsStep(float32 timeStep, int32 velocityIterations, int32 positionIterations) {
    // This is the usual update routine.
    world.Step(timeStep, velocityIterations, positionIterations);
    world.ClearForces();

    // SpriteClass can be replaced with any class you favor.
    // Assume there is a known pointer to the b2Body. Otherwise you ll have to get that,
    // or iterate over all bodies in the world.
    SpriteClass *sprite = (SpriteClass*)body->GetUserData();

    // Once you have the pointer you can transfer all the data.
    sprite.position = body->GetPosition();
    sprite.angle = body->GetAngle();
    // ... and so on
}

User data is just an arbitrary storage space in the b2Body and Box2D has no idea about what you decide to store there.

To move sprite with body then You have to set the sprite position accotring to body

in your update method

like this in COCOS2D-X Here

sprite = static_cast<CCSprite*>body->GetUserData();
sprite->setPosition(vec2(body->GetPosition().x*PTM_RATIO, body->GetPosition().y*PTM_RATIO));
sprite->setRotation(-CC_Radian_to_Degree(body->GetAngle));

PTM_RATIO = 32;





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签