Showing posts with label cpp. Show all posts
Showing posts with label cpp. Show all posts

Saturday, May 23, 2015

Implement Camera (former: CCCamera) v2 in cocos2d-x v3.3

Reference: Implement Camera (former: CCCamera) v2 in cocos2d-x v3.3

we want to port camera code from cocos2d-x version 2 to version 3.3 with the goal to imitate the behaviour of a CCCamera in cocos2d-x version 2 exactly.

You can assume that values for "eye" and "up" are calculated correctly.

Old code operating on CCCamera:
camera->setEyeXYZ(eye.x, eye.y, 0);
camera->setCenterXYZ(eye.x, eye.y, -1);
camera->setUpXYZ(up.x, up.y, 0);
New code operating on Camera:
camera->setPosition3D(Vec3(eye.x, eye.y, 0.0f));
camera->lookAt(Vec3(eye.x, eye.y, -1), Vec3(up.x, up.y, 0.0f));
The setPosition3D part works fine and this is also a good indicator that I've set up the camera correctly in terms of setCameraFlag and Mask, correct layers, etc. but for some reason the tilting (modification of the up vector) doesn't work fine, it's kind of hard to describe, but it doesn't seem that the view matrix is calculated as intended.

The examples only contain code with fixed up vectors, e.g. Vec3(0, 1, 0), but I really need a dynamic one.

Thanks in advance, Chris

 Cocos2d-x v3 C++ Tutorial 36 - Touch Began Using onTouchBegan - 學習筆記

資料來源: Cocos2d-x v3 C++ Tutorial 36 - Touch Began Using onTouchBegan

* 如何在 Cocos2d-x v3.6 C++ 收 onTouchBegan 事件
** 增加下列代碼在 cpp init 函數
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

** 增加下列代碼(函數)在 cpp 檔
bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event)
{
    CCLOG("onTouchBegan x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
    return true;
}

** 增加下列代碼(函數宣告) 在 header 檔
bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event);