1
votes

I am creating an app in which i am trying to Join two bodies in such away that when i move one body, the second body should be moved within the first body.

Here's my code to create a body:

    - (b2Body *)addBoxBodyForDynamicSprite:(CCSprite *)sprite {

    b2BodyDef spriteBodyDef;
    spriteBodyDef.type = b2_dynamicBody;
    //spriteBodyDef.position.Set(sprite.position.x/PTM_RATIO, sprite.position.y/PTM_RATIO);
    CGPoint asolutePoint = [sprite.parent convertToWorldSpace:sprite.position];
    spriteBodyDef.position.Set(asolutePoint.x/PTM_RATIO, asolutePoint.y/PTM_RATIO);
    spriteBodyDef.userData = (__bridge void*)sprite;
    b2Body *spriteBody = world->CreateBody(&spriteBodyDef);

    b2PolygonShape spriteShape;
    spriteShape.SetAsBox(sprite.contentSize.width/PTM_RATIO/2,
                         sprite.contentSize.height/PTM_RATIO/2);
    b2FixtureDef spriteShapeDef;
    spriteShapeDef.shape = &spriteShape;
    spriteShapeDef.density = 0.3;
    spriteShapeDef.isSensor = true;
    spriteBody->CreateFixture(&spriteShapeDef);

    return spriteBody;
}

One body is kinematic and other body is Dynamic. I am moving these bodies by using:

theBody->SetTransform(locationWorld, theBody->GetAngle());

If i apply linear force here, the bodies are not moving & the Joint which i used to fix them is b2WeldJoint.

b2JointDef jointDef;
    jointDef.bodyA = another;
    jointDef.bodyB = leftHandFixBody;
    aJoint = (b2Joint *)world->CreateJoint(&jointDef);

It moves the dynamic body but the kinematic body remains on its position. I want to move both the bodies together. Any help will be really appreciated. Thanks!

1
There are a lot of pointers here. Avoid the use of pointers and your life as C++ programmer would be easy. - Manu343726
Kinematic bodies have infinite momentum, so they do not react to forces or impulses. You can use SetLinearVelocity to move them. (@Manu that is totally irrelevant and not even true.) - iforce2d
@iforce2d Thanks for your reply. I have already tried setLinearVelocity, but its not moving any body. theBody->SetLinearVelocity(locationWorld); - EXC_BAD_ACCESS
Well, it should. Make sure you are also calling Step() to update the world, and your rendering is all correct. You could check the value of GetLinearVelocity() to see if the velocity was set as expected. - iforce2d

1 Answers

1
votes

Also, according to the manual, kinematic bodies are moved by setting their velocity, not by applying forces.

A kinematic body moves under simulation according to its velocity. Kinematic bodies do not respond to forces. They can be moved manually by the user, but normally a kinematic body is moved by setting its velocity. A kinematic body behaves as if it has infinite mass, however, Box2D stores zero for the mass and the inverse mass. Kinematic bodies do not collide with other kinematic or static bodies.

Also, I have found that using SetTransform(...) to move bodies is less than effective. I created a portal with it to jump a body from one place to another, and that worked. But if I updated it every simulation cycle, the body stopped colliding with other bodies. This is just a word of caution.

Was this helpful?