我如何将这两个(Car and Wheel)作为cocos2d-x 3.3中的单个物理体联合起来? 如何将这个联合体添加到物理世界?
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.
答案 0 :(得分:0)
我怀疑这个错误是由于你的car_body没有任何形状,尝试添加一个形状到你的car_body,DO这样的东西 编辑: -
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);
在上面的代码中,您的汽车Physics Body将有一个Sprite的Bounding Box 你在这里也有点浪费: -
PhysicsBody *wheel_bd = PhysicsBody::create();
wheel_bd=PhysicsBody::createCircle(69.0/64.0);
你应该这样做: -
PhysicsBody *wheel_bd = PhysicsBody::create();
wheel_bd ->addShape(PhysicsShapeCircle::create(69.0/64.0));