How can i do Physics Joint of bodies in cocos2d-x 3.3?

1.9k views Asked by At

How can i Joint these two(Car and Wheel)as a single Physics body in a cocos2d-x 3.3? how to add this Joint body into Physics world??

Sprite *car=Sprite::create("abc.png");
car->setPosition(100,400);
car->setScale(1.5, 1.5);
this->addChild(car);
PhysicsBody *car_body=PhysicsBody::create();
car->setPhysicsBody(car_body);
car_body->setGravityEnable(false);

Sprite *wheel=Sprite::create("abc.png");
wheel->setPosition(40,350);
this->addChild(wheel);

PhysicsBody *wheel_bd=PhysicsBody::create(); 
wheel_bd=PhysicsBody::createCircle(69.0/64.0);
wheel->setPhysicsBody(wheel_bd);
wheel_bd->setGravityEnable(false);
PhysicsJoint *co =PhysicsJointGroove::construct(wheel->getPhysicsBody(),
car->getPhysicsBody(),Vec2(80,250),Vec2(160,250),Vec2(1.0,1.0));
scene->getPhysicsWorld()->addJoint(co); //this line creating error into my code.
1

There are 1 answers

4
Lazy Gamer On

I Suspect this Error is Due to your car_body Not having any Shape, Try adding a Shape to ur car_body, DO Something like this Edit:-

        Sprite *car=Sprite::create("abc.png");
        car->setPosition(100,400);
        car->setScale(1.5, 1.5);

        PhysicsBody *car_Body = PhysicsBody::create();
        car_Body->addShape(PhysicsShapeBox::create(car->getBoundingBox().size);
        car_Body->setGravityEnable(false);
        car->setPhysicsBody(car_body);
        this->addChild(car);

In above code your car Physics Body will have a Bounding Box of your Sprite Also u are a little waste full here:-

         PhysicsBody *wheel_bd = PhysicsBody::create(); 
         wheel_bd=PhysicsBody::createCircle(69.0/64.0);

u should do something like this:-

         PhysicsBody *wheel_bd = PhysicsBody::create();
         wheel_bd ->addShape(PhysicsShapeCircle::create(69.0/64.0));