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…
@srinivas
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.,
@srinivas
Hi,
You can find other examples here https://www.iforce2d.net/rube/?panel=loaders
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
body->setUserData((void*)spriteBody);
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