car moving body in box2d

car moving body in box2d
0.0 0



i have car body seprate and 2 tiers . how to fix as box2d body and car moving animation.(i need to rotate tier) ?

in normal cocos2d-x i will do something like this…

    car = CCSprite::spriteWithFile("car.png");
    car->setPosition(ccp(car->getContentSize().width/2+30, car->getContentSize().height-19));
    this->addChild(car, 10);
    tire = CCSprite::spriteWithFile("tire.png");
    tire->setPosition(ccp(tire->getContentSize().width/2+43, tire->getContentSize().height+8));
    this->addChild(tire, 10);
    tire1 = CCSprite::spriteWithFile("tire.png");
    tire1->setPosition(ccp(tire->getContentSize().width/2+136, tire->getContentSize().height+8));
    this->addChild(tire1, 10);

    CCRotateBy *Rot = CCRotateBy::actionWithDuration (1.0f, 360);
    CCRepeatForever *rep = CCRepeatForever::actionWithAction(Rot);
    CCRotateBy *Rot1 = CCRotateBy::actionWithDuration(1.0f, 360);
    CCRepeatForever *rep1 = CCRepeatForever::actionWithAction(Rot1);

but in box2d how to do ?


hey where is the box2d part of the code??? anyway if you are doing this using box2d… then you need to apply angular velocity to the tire for it to rotate and the sprite attached to the tire will be updated… you cant use rotate by…


In Box2d you would create your car body and wheels, and join the wheels to the body, then add the whole thing to the box2d World.

Then replace the code you have - where you set the position of the various parts, instead you query the position of the box2d objects, translate them to screen coordinates, then set your sprite’s position.

The easiest way to start is to create your box2d world and simple vehicle, make sure you are displaying debug data, so you can see the box2d objects.

Next, ‘attach’ one of your sprites to the box2d object - i.e. set the sprite’s position and rotation to that of the box2d object (corrected for physics world vs screen coordinates)

Once you have that, you can start making more complex Box2d objects, and attaching more sprites to them.,


if you provide some gud example,it will more helpful for me, as am new to box2d.


will give you enough to be getting on with I would think


You can find other examples here
In any case, you have to do the following steps
1.- Create your physics objects
2.- Create your sprites and attach them to the physic objects. In order to do that, all Box2D bodies have a UserData parameter. You just assign your sprites to the body by doing
3.- On your update function you need to add something like this

for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) { if (b->GetUserData() != NULL) { //Synchronize the AtlasSprites position and rotation with the corresponding body ObstacleUserData* myActor = (ObstacleUserData*)b->GetUserData(); myActor->sprite->setPosition( CCPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO) ); myActor->sprite->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()) ); } }

That will move the sprites together with the bodies

Hope this helps


@GiZmo Good examples there too, I just thought a bit bewildering for a newbie - the Ray Wenderlich site has some great real beginner level stuff.

Mind you., I’m a beginner & I almost live at iForce2d!! :slight_smile: