Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Cocos2d-x 實作:讓茶壺在觸摸手勢下旋轉的三種思路

編輯:關於 Android 程式設計

在 3D 場景中讓模型「轉起來」,本質上有三種截然不同的思路:

一是旋轉攝影機,讓鏡頭繞著茶壺轉,從視覺上營造出茶壺自轉的效果;二是旋轉環境光,藉由光影變化達到同樣的觀感;三是直接旋轉物體自身,讓茶壺真正自轉。本文以下方的範例採用的正是第三種——直接對模型套用旋轉。

先來看看頭檔的定義:

#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" USING_NS_CC; class HelloWorld : public cocos2d::Layer { public: static cocos2d::Scene* createScene(); virtual bool init(); virtual void onEnter(); virtual void onExit(); CREATE_FUNC(HelloWorld); Sprite3D * pSprite3D; Camera* pCamera; Camera* pCamera01; void onTouchesBegan(const std::vector& touches, cocos2d::Event *event); void onTouchesMoved(const std::vector& touches, cocos2d::Event *event); void onTouchesEnded(const std::vector& touches, cocos2d::Event *event); float m_fAngle; }; #endif // __HELLOWORLD_SCENE_H__ 

接著是實作檔:

#include "HelloWorldScene.h" #include "DrawNode3D.h" Scene* HelloWorld::createScene() { auto scene = Scene::create(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if (!Layer::init()) { return false; } Size size = Director::getInstance()->getWinSize(); pCamera01 = Camera::createPerspective(30.0f, size.width / size.height, 1.0f, 1000); pCamera01->setCameraFlag(CameraFlag::USER1); pCamera01->setPosition3D(Vec3(size.width / 2, size.height / 2, 0.0f)); pCamera01->lookAt(Vec3(0,0, 0), Vec3(0.0f, 1.0f, 0.0f)); this->addChild(pCamera01); //創建茶壺模型 pSprite3D = Sprite3D::create("teapot.c3b"); pSprite3D->setPosition3D(Vec3(0, 0, 0)); pSprite3D->setRotation3D(Vec3(-90, 0,0)); pSprite3D->setScale(12.0f); pSprite3D->setCameraMask(2); this->addChild(pSprite3D); m_fAngle = 1.0f;//一定要賦初始值,否則旋轉不了! auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this); listener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved, this); listener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); return true; } void HelloWorld::onEnter() { Layer::onEnter(); auto pGLProgram = GLProgram::createWithFilenames("Diffuse.vsh", "Diffuse.fsh"); auto state = GLProgramState::create(pGLProgram); long offset = 0; auto attributeCount = pSprite3D->getMesh()->getMeshVertexAttribCount(); for (int k = 0; k < attributeCount; k++) { auto meshattribute = pSprite3D->getMesh()->getMeshVertexAttribute(k); state->setVertexAttribPointer( s_attributeNames[meshattribute.vertexAttrib], meshattribute.size, meshattribute.type, GL_FALSE, pSprite3D->getMesh()->getVertexSizeInBytes(), (GLvoid*)offset ); offset += meshattribute.attribSizeBytes; } Camera* pCamera = Director::getInstance()->getRunningScene()->getDefaultCamera(); Vec3 pos = pCamera->getPosition3D(); Vec3 target = Vec3(-10, -10, -10); state->setUniformVec3("v_eyepos", pos - target); pSprite3D->setGLProgramState(state); } void HelloWorld::onTouchesBegan(const std::vector& touches, cocos2d::Event *event) { } void HelloWorld::onTouchesMoved(const std::vector& touches, cocos2d::Event *event) { if (touches.size()) { auto touch = touches[0]; auto delta = touch->getDelta(); m_fAngle -= CC_DEGREES_TO_RADIANS(delta.x); pCamera01->lookAt(Vec3(0, 0, 0), Vec3(0.0f,1.0f, 0.0f)); pSprite3D->setRotation3D(Vec3(-90.0f, 100.0f * m_fAngle,0.0f )); } } void HelloWorld::onTouchesEnded(const std::vector& touches, cocos2d::Event *event) { } void HelloWorld::onExit() { Layer::onExit(); }

程式碼中有兩個細節特別值得留意。其一,m_fAngle 一定要賦初始值,否則茶壺根本不會旋轉——這是很多人第一次實作時踩過的坑。其二,旋轉的核心邏輯集中在 onTouchesMoved 中:我們透過 touch->getDelta() 取得手指的橫向位移,換算成角度增量後累加到 m_fAngle,再用 setRotation3D 把它套用到茶壺的 Y 軸上,如此一來,手指滑動的幅度就能對應到模型旋轉的角度,操作起來相當直覺順手。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved