change camera operation

This commit is contained in:
yangxiao 2014-10-16 17:31:18 +08:00
parent 41d2929060
commit 8e6728d8c0
2 changed files with 334 additions and 102 deletions

View File

@ -1,27 +1,27 @@
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "Camera3DTest.h"
#include <algorithm>
@ -179,6 +179,10 @@ Camera3DTestDemo::Camera3DTestDemo(void)
, _camera(nullptr)
, _incRot(nullptr)
, _decRot(nullptr)
,_bZoomOut(false)
,_bZoomIn(false)
,_bRotateLeft(false)
,_bRotateRight(false)
{
}
Camera3DTestDemo::~Camera3DTestDemo(void)
@ -225,8 +229,11 @@ void Camera3DTestDemo::SwitchViewCallback(Ref* sender, CameraType cameraType)
{
_camera->setPosition3D(Vec3(0, 130, 130) + _sprite3D->getPosition3D());
_camera->lookAt(_sprite3D->getPosition3D(), Vec3(0,1,0));
_incRot->setEnabled(true);
_decRot->setEnabled(true);
_RotateRightlabel->setColor(Color3B::WHITE);
_RotateLeftlabel->setColor(Color3B::WHITE);
_ZoomInlabel->setColor(Color3B::WHITE);
_ZoomOutlabel->setColor(Color3B::WHITE);
}
else if(_cameraType==CameraType::FirstCamera)
{
@ -235,21 +242,28 @@ void Camera3DTestDemo::SwitchViewCallback(Ref* sender, CameraType cameraType)
newFaceDir.normalize();
_camera->setPosition3D(Vec3(0,35,0) + _sprite3D->getPosition3D());
_camera->lookAt(_sprite3D->getPosition3D() + newFaceDir*50, Vec3(0, 1, 0));
_incRot->setEnabled(true);
_decRot->setEnabled(true);
_RotateRightlabel->setColor(Color3B::WHITE);
_RotateLeftlabel->setColor(Color3B::WHITE);
_ZoomInlabel->setColor(Color3B::GRAY);
_ZoomOutlabel->setColor(Color3B::GRAY);
}
else if(_cameraType==CameraType::ThirdCamera)
{
_camera->setPosition3D(Vec3(0, 130, 130) + _sprite3D->getPosition3D());
_camera->lookAt(_sprite3D->getPosition3D(), Vec3(0,1,0));
_incRot->setEnabled(false);
_decRot->setEnabled(false);
_RotateRightlabel->setColor(Color3B::GRAY);
_RotateLeftlabel->setColor(Color3B::GRAY);
_ZoomInlabel->setColor(Color3B::WHITE);
_ZoomOutlabel->setColor(Color3B::WHITE);
}
}
void Camera3DTestDemo::onEnter()
{
BaseTest::onEnter();
_sprite3D=nullptr;
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto s = Director::getInstance()->getWinSize();
auto listener = EventListenerTouchAllAtOnce::create();
listener->onTouchesBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesBegan, this);
@ -262,32 +276,76 @@ void Camera3DTestDemo::onEnter()
_curState=State_None;
addNewSpriteWithCoords( Vec3(0,0,0),"Sprite3DTest/girl.c3b",true,0.2f,true);
TTFConfig ttfConfig("fonts/arial.ttf", 20);
auto label1 = Label::createWithTTF(ttfConfig,"zoom out");
auto menuItem1 = MenuItemLabel::create(label1, CC_CALLBACK_1(Camera3DTestDemo::scaleCameraCallback,this,1));
auto label2 = Label::createWithTTF(ttfConfig,"zoom in");
auto menuItem2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Camera3DTestDemo::scaleCameraCallback,this,-1));
auto label3 = Label::createWithTTF(ttfConfig,"rotate+");
auto menuItem3 = MenuItemLabel::create(label3, CC_CALLBACK_1(Camera3DTestDemo::rotateCameraCallback,this,10));
_incRot = menuItem3;
auto label4 = Label::createWithTTF(ttfConfig,"rotate-");
auto menuItem4 = MenuItemLabel::create(label4, CC_CALLBACK_1(Camera3DTestDemo::rotateCameraCallback,this,-10));
_decRot = menuItem4;
auto label5 = Label::createWithTTF(ttfConfig,"free ");
auto menuItem5 = MenuItemLabel::create(label5, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback,this,CameraType::FreeCamera));
auto label6 = Label::createWithTTF(ttfConfig,"third person");
auto menuItem6 = MenuItemLabel::create(label6, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback,this,CameraType::ThirdCamera));
auto label7 = Label::createWithTTF(ttfConfig,"first person");
auto menuItem7 = MenuItemLabel::create(label7, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback,this,CameraType::FirstCamera));
auto menu = Menu::create(menuItem1, menuItem2, menuItem3, menuItem4, menuItem5, menuItem6, menuItem7, nullptr);
auto containerForLabel1 = Node::create();
_ZoomOutlabel = Label::createWithTTF(ttfConfig,"zoom out");
_ZoomOutlabel->setPosition(s.width-50, VisibleRect::top().y-30);
containerForLabel1->addChild(_ZoomOutlabel);
addChild(containerForLabel1, 10);
auto listener1 = EventListenerTouchOneByOne::create();
listener1->setSwallowTouches(true);
listener1->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomOut, this);
listener1->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomOutEnd, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, _ZoomOutlabel);
auto containerForLabel2 = Node::create();
_ZoomInlabel = Label::createWithTTF(ttfConfig,"zoom in");
_ZoomInlabel->setPosition(s.width-50, VisibleRect::top().y-100);
containerForLabel2->addChild(_ZoomInlabel);
addChild(containerForLabel2, 10);
auto listener2 = EventListenerTouchOneByOne::create();
listener2->setSwallowTouches(true);
listener2->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomIn, this);
listener2->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesZoomInEnd, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener2, _ZoomInlabel);
auto containerForLabel3 = Node::create();
_RotateLeftlabel = Label::createWithTTF(ttfConfig,"rotate left");
_RotateLeftlabel->setPosition(s.width-50, VisibleRect::top().y-170);
containerForLabel3->addChild(_RotateLeftlabel);
addChild(containerForLabel3, 10);
auto listener3 = EventListenerTouchOneByOne::create();
listener3->setSwallowTouches(true);
listener3->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateLeft, this);
listener3->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateLeftEnd, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener3, _RotateLeftlabel);
auto containerForLabel4 = Node::create();
_RotateRightlabel = Label::createWithTTF(ttfConfig,"rotate right");
_RotateRightlabel->setPosition(s.width-50, VisibleRect::top().y-240);
containerForLabel4->addChild(_RotateRightlabel);
addChild(containerForLabel4, 10);
auto listener4 = EventListenerTouchOneByOne::create();
listener4->setSwallowTouches(true);
listener4->onTouchBegan = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateRight, this);
listener4->onTouchEnded = CC_CALLBACK_2(Camera3DTestDemo::onTouchesRotateRightEnd, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener4, _RotateRightlabel);
auto label1 = Label::createWithTTF(ttfConfig,"free ");
auto menuItem1 = MenuItemLabel::create(label1, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback,this,CameraType::FreeCamera));
auto label2 = Label::createWithTTF(ttfConfig,"third person");
auto menuItem2 = MenuItemLabel::create(label2, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback,this,CameraType::ThirdCamera));
auto label3 = Label::createWithTTF(ttfConfig,"first person");
auto menuItem3 = MenuItemLabel::create(label3, CC_CALLBACK_1(Camera3DTestDemo::SwitchViewCallback,this,CameraType::FirstCamera));
auto menu = Menu::create(menuItem1, menuItem2, menuItem3, nullptr);
menu->setPosition(Vec2::ZERO);
menuItem1->setPosition(s.width-50, VisibleRect::top().y-50 );
menuItem2->setPosition(s.width-50, VisibleRect::top().y-100);
menuItem3->setPosition(s.width-50, VisibleRect::top().y-150);
menuItem4->setPosition(s.width-50, VisibleRect::top().y-200);
menuItem5->setPosition(VisibleRect::left().x+100, VisibleRect::top().y-50);
menuItem6->setPosition(VisibleRect::left().x+100, VisibleRect::top().y -100);
menuItem7->setPosition(VisibleRect::left().x+100, VisibleRect::top().y -150);
menuItem1->setPosition(VisibleRect::left().x+100, VisibleRect::top().y-50);
menuItem2->setPosition(VisibleRect::left().x+100, VisibleRect::top().y -100);
menuItem3->setPosition(VisibleRect::left().x+100, VisibleRect::top().y -150);
addChild(menu, 0);
schedule(CC_SCHEDULE_SELECTOR(Camera3DTestDemo::updateCamera), 0.0f);
if (_camera == nullptr)
@ -313,6 +371,8 @@ void Camera3DTestDemo::onEnter()
line->drawLine(Vec3(0, 0, 0),Vec3(0,50,0),Color4F(0,1,0,1));
_layer3D->addChild(line);
_layer3D->setCameraMask(2);
}
void Camera3DTestDemo::onExit()
{
@ -392,15 +452,15 @@ void Camera3DTestDemo::onTouchesMoved(const std::vector<Touch*>& touches, cocos2
Vec3 cameraRightDir;
_camera->getNodeToWorldTransform().getForwardVector(&cameraDir);
cameraDir.normalize();
cameraDir.y = 0;
cameraDir.y=0;
_camera->getNodeToWorldTransform().getRightVector(&cameraRightDir);
cameraRightDir.normalize();
cameraRightDir.y = 0;
Vec3 cameraPos = _camera->getPosition3D();
cameraPos += cameraDir*newPos.y * 0.1f;
cameraPos += cameraRightDir * newPos.x * 0.1f;
cameraRightDir.y=0;
Vec3 cameraPos= _camera->getPosition3D();
cameraPos+=cameraDir*newPos.y*0.1f;
cameraPos+=cameraRightDir*newPos.x*0.1f;
_camera->setPosition3D(cameraPos);
if(_sprite3D && _cameraType == CameraType::FirstCamera)
if(_sprite3D && _cameraType==CameraType::FirstCamera)
{
_sprite3D->setPosition3D(Vec3(_camera->getPositionX(),0,_camera->getPositionZ()));
_targetPos=_sprite3D->getPosition3D();
@ -468,7 +528,7 @@ void Camera3DTestDemo::onTouchesEnded(const std::vector<Touch*>& touches, cocos2
auto location = touch->getLocationInView();
if(_camera)
{
if(_sprite3D && _cameraType==CameraType::ThirdCamera)
if(_sprite3D && _cameraType==CameraType::ThirdCamera && _bZoomOut == false && _bZoomIn == false && _bRotateLeft == false && _bRotateRight == false)
{
Vec3 nearP(location.x, location.y, -1.0f), farP(location.x, location.y, 1.0f);
@ -483,6 +543,16 @@ void Camera3DTestDemo::onTouchesEnded(const std::vector<Touch*>& touches, cocos2
float ndo = Vec3::dot(Vec3(0,1,0),nearP);
dist= (0 - ndo) / ndd;
Vec3 p = nearP + dist * dir;
if( p.x > 100)
p.x = 100;
if( p.x < -100)
p.x = -100;
if( p.z > 100)
p.z = 100;
if( p.z < -100)
p.z = -100;
_targetPos=p;
}
}
@ -539,12 +609,156 @@ void Camera3DTestDemo::updateCamera(float fDelta)
}
}
}
if(_bZoomOut == true)
{
if(_camera)
{
if(_cameraType == CameraType::ThirdCamera)
{
Vec3 lookDir = _camera->getPosition3D() - _sprite3D->getPosition3D();
Vec3 cameraPos = _camera->getPosition3D();
if(lookDir.length() <= 300)
{
cameraPos += lookDir.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
else if(_cameraType == CameraType::FreeCamera)
{
Vec3 cameraPos = _camera->getPosition3D();
if(cameraPos.length() <= 300)
{
cameraPos += cameraPos.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
}
}
if(_bZoomIn == true)
{
if(_camera)
{
if(_cameraType == CameraType::ThirdCamera)
{
Vec3 lookDir = _camera->getPosition3D() - _sprite3D->getPosition3D();
Vec3 cameraPos = _camera->getPosition3D();
if(lookDir.length() >= 50)
{
cameraPos -= lookDir.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
else if(_cameraType == CameraType::FreeCamera)
{
Vec3 cameraPos = _camera->getPosition3D();
if(cameraPos.length() >= 50)
{
cameraPos -= cameraPos.getNormalized();
_camera->setPosition3D(cameraPos);
}
}
}
}
if(_bRotateLeft == true)
{
if(_cameraType==CameraType::FreeCamera || _cameraType==CameraType::FirstCamera)
{
Vec3 rotation3D= _camera->getRotation3D();
rotation3D.y+= 1;
_camera->setRotation3D(rotation3D);
}
}
if(_bRotateRight == true)
{
if(_cameraType==CameraType::FreeCamera || _cameraType==CameraType::FirstCamera)
{
Vec3 rotation3D= _camera->getRotation3D();
rotation3D.y-= 1;
_camera->setRotation3D(rotation3D);
}
}
}
}
bool Camera3DTestDemo::isState(unsigned int state,unsigned int bit) const
{
return (state & bit) == bit;
}
bool Camera3DTestDemo::onTouchesZoomOut(Touch* touch, Event* event)
{
auto target = static_cast<Label*>(event->getCurrentTarget());
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
if (rect.containsPoint(locationInNode))
{
_bZoomOut = true;
return true;
}
return false;
}
void Camera3DTestDemo::onTouchesZoomOutEnd(Touch* touch, Event* event)
{
_bZoomOut = false;
}
bool Camera3DTestDemo::onTouchesZoomIn(Touch* touch, Event* event)
{
auto target = static_cast<Label*>(event->getCurrentTarget());
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
if (rect.containsPoint(locationInNode))
{
_bZoomIn = true;
return true;
}
return false;
}
void Camera3DTestDemo::onTouchesZoomInEnd(Touch* touch, Event* event)
{
_bZoomIn = false;
}
bool Camera3DTestDemo::onTouchesRotateLeft(Touch* touch, Event* event)
{
auto target = static_cast<Label*>(event->getCurrentTarget());
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
if (rect.containsPoint(locationInNode))
{
_bRotateLeft = true;
return true;
}
return false;
}
void Camera3DTestDemo::onTouchesRotateLeftEnd(Touch* touch, Event* event)
{
_bRotateLeft = false;
}
bool Camera3DTestDemo::onTouchesRotateRight(Touch* touch, Event* event)
{
auto target = static_cast<Label*>(event->getCurrentTarget());
Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height);
if (rect.containsPoint(locationInNode))
{
_bRotateRight = true;
return true;
}
return false;
}
void Camera3DTestDemo::onTouchesRotateRightEnd(Touch* touch, Event* event)
{
_bRotateRight = false;
}
void Camera3DTestScene::runThisTest()
{
auto layer = nextSpriteTestAction();

View File

@ -1,26 +1,26 @@
/****************************************************************************
Copyright (c) 2013 cocos2d-x.org
Copyright (c) 2013 cocos2d-x.org
http://www.cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef _CAMERA3D_TEST_H_
#define _CAMERA3D_TEST_H_
@ -77,6 +77,16 @@ public:
void updateState(float elapsedTime);
bool isState(unsigned int state,unsigned int bit) const;
void reachEndCallBack();
bool onTouchesZoomOut(Touch* touch, Event* event);
void onTouchesZoomOutEnd(Touch* touch, Event* event);
bool onTouchesZoomIn(Touch* touch, Event* event);
void onTouchesZoomInEnd(Touch* touch, Event* event);
bool onTouchesRotateLeft(Touch* touch, Event* event);
void onTouchesRotateLeftEnd(Touch* touch, Event* event);
bool onTouchesRotateRight(Touch* touch, Event* event);
void onTouchesRotateRightEnd(Touch* touch, Event* event);
protected:
std::string _title;
Layer* _layer3D;
@ -88,6 +98,14 @@ protected:
unsigned int _curState;
Camera* _camera;
MoveTo* _moveAction;
bool _bZoomOut;
bool _bZoomIn;
bool _bRotateLeft;
bool _bRotateRight;
Label* _RotateRightlabel;
Label* _RotateLeftlabel;
Label* _ZoomInlabel;
Label* _ZoomOutlabel;
};
class Camera3DTestScene : public TestScene
{