Last active
September 12, 2015 10:50
-
-
Save t-takasaka/d51ab509cb520d4730c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//※SCREEN_WIDTHは1280、SCREEN_HEIGHTは720 | |
//実機の画面サイズ | |
Size frameSize = Director::sharedDirector()->getOpenGLView()->getFrameSize(); | |
//setDesignResolutionSize()で指定した画面サイズ(アス比の違う実機では多少変わる) | |
Size windowSize = Director::getInstance()->getWinSize(); | |
//Live2Dのキャンバスサイズ | |
float canvasWidth = m_live2DModel->getCanvasWidth(); | |
float canvasHeight = m_live2DModel->getCanvasHeight(); | |
float logicalWidth = abs(VIEW_LOGICAL_LEFT - VIEW_LOGICAL_RIGHT); | |
float aspect = ((float)SCREEN_WIDTH / SCREEN_HEIGHT); | |
//16:9を想定しているので、例えば5:3の場合は16*x:9=5:3でx=0.9375を補正する | |
aspect = aspect * ((SCREEN_HEIGHT * frameSize.width) / (SCREEN_WIDTH * frameSize.height)); | |
float invScale = 1 / scale; | |
L2DMatrix44 *canvasToWindow = new L2DMatrix44(); | |
//縮拡するために原点に持っていく(原点はキャンバスの中心) | |
canvasToWindow->multTranslate(-canvasWidth * 0.5f, -canvasHeight * 0.5f); | |
//画面を-2~2の範囲にマッピング | |
canvasToWindow->multScale(logicalWidth / frameSize.width, -logicalWidth / frameSize.width); | |
//モデルを縮拡する | |
canvasToWindow->multScale(scale, scale * aspect); | |
//cocos2d-xは左下が原点なので合わせて移動する | |
float shiftX = canvasToWindow->transformX(-frameSize.width * 0.5f * invScale); | |
float shiftY = canvasToWindow->transformY(frameSize.height * 0.5f * invScale); | |
canvasToWindow->translateX(shiftX); | |
canvasToWindow->translateY(shiftY); | |
//フレーム内のローカル座標分を移動 | |
shiftX = canvasToWindow->transformX(localX * invScale); | |
shiftY = canvasToWindow->transformY(localY * invScale); | |
canvasToWindow->translateX(shiftX); | |
canvasToWindow->translateY(shiftY); | |
float scX = canvasToWindow->getScaleX(); | |
float scY = canvasToWindow->getScaleY(); | |
float mvX = canvasToWindow->getTranslateX(); | |
float mvY = canvasToWindow->getTranslateY(); | |
float matrix []= { | |
scX, 0, 0, 0, | |
0, scY, 0, 0, | |
0, 0, 1, 0, | |
mvX, mvY, 0, 1 | |
}; | |
m_live2DModel->setMatrix(matrix); //位置を設定 | |
this->setPosition(worldX, worldY); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment