I was reading the documentation about the physics engine, it seems like there are a lot of wrappers around box2d so no need to include it and instead work with the classes provided by cocos, so that’s what i did:
bool HelloWorld::init()
{
if( !Scene::initWithPhysics() )
{
return false;
}
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onKeyPressed, this);
listener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
auto physicsBody = PhysicsBody::createBox(Size(37.0f, 32.0f), PhysicsMaterial(0.1f,
1.0f, 0.0f));
physicsBody->setDynamic(true);
this->sprite = Sprite::create();
sprite->addComponent(physicsBody);
this->addChild(sprite);
return true;
}
void HelloWorld::onKeyPressed(EventKeyboard::KeyCode keyCode, Event * event)
{
switch(keyCode)
{
case EventKeyboard::KeyCode::KEY_D:
this->sprite->getPhysicsBody()->applyForce(Vec2(200.0f, 200.0f));
break;
default:
break;
}
}
I applied a force to the sprite but when i pressed D it didn’t apply it, any tips?