What is the correct way to reposition a sprite with a physics body attached? – #8 by chumbyfuzz – C++

If it Cocos2dx yesh just do a sprite->setPostion(0,0);
if it’s Box2d then you need to use the tranform function of box2d. The sprite’s are moved normly to the center of box2d physics witch is 0,0 = center of box2d object and not Cocos 0,0 witch is top left
cocos2dx defult anchor point. (0.5,0.5) center.
Box2d has bodys’ so somting like this.

box2dBody* body;
body->SetTransform(b2Vec2( postion.x , postion.y ), body->GetAngle());

//My Box2d Updade is like this. It move’s the sprite to the center of the box2d body and rotates it…
void PhysicsClass::UpdateWorld(void)
world->Step(UPDATE_INTERVAL, velocityIterations, positionIterations);

for (b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
    UserData *Object = (UserData *)b->GetUserData();
    if (Object) {
        if( Object->sprite ){
            Object->sprite->setPosition(Vec2(b->GetPosition().x * PTM_RATIO ,b->GetPosition().y * PTM_RATIO));
            Object->sprite->setRotation(-1 * CC_RADIANS_TO_DEGREES( b->GetAngle() ));


Source link

More To Explore

Share on facebook
Share on twitter
Share on linkedin
Share on email