void CameraArcBallDemo::onTouchsMoved( const std::vector<Touch*> &touchs, Event *event ) { if (!touchs.empty()) { if(_operate == OperateCamType::RotateCamera) //arc ball rotate { Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 prelocation = touchs[0]->getPreviousLocationInView(); Vec2 location = touchs[0]->getLocationInView(); location.x = 2.0 * (location.x) / (visibleSize.width) - 1.0f; location.y = 2.0 * (visibleSize.height - location.y) / (visibleSize.height) - 1.0f; prelocation.x = 2.0 * (prelocation.x) / (visibleSize.width) - 1.0f; prelocation.y = 2.0 * (visibleSize.height - prelocation.y) / (visibleSize.height) - 1.0f; Vec3 axes; float angle; calculateArcBall(axes, angle, prelocation.x, prelocation.y, location.x, location.y); //calculate rotation quaternion parameters Quaternion quat(axes, angle); //get rotation quaternion _rotationQuat = quat * _rotationQuat; updateCameraTransform(); //update camera Transform } else if(_operate == OperateCamType::MoveCamera) //camera zoom { Point newPos = touchs[0]->getPreviousLocation() - touchs[0]->getLocation(); _distanceZ -= newPos.y*0.1f; updateCameraTransform(); } } }
void CameraArcBallDemo::switchTargetCallback(Ref* sender) { if(_target == 0) { _target = 1; _center = _sprite3D2->getPosition3D(); updateCameraTransform(); } else if(_target == 1) { _target = 0; _center = _sprite3D1->getPosition3D(); updateCameraTransform(); } }
// // Clear data // void CSpaceMouse::clearObjectTransform(float *mat, float *pos, float *quatRot) { oglMatrix.make_identity(); dxMatrix.make_identity(); oglMatrixAbs.make_identity(); dxMatrixAbs.make_identity(); tVec = nv::vec3f(0.0f); if(pos) { tVecAbs = nv::vec3f(pos); } else { tVecAbs = nv::vec3f(0.0f); } rAxis = nv::vec3f(0.0f); rAngle = 0; if(quatRot) { quatAbs = nv::quaternionf(quatRot); } else { quatAbs = nv::quaternionf(0.0f,0.0f,0.0f,1.0f); } quat = nv::quaternionf(0.0f,0.0f,0.0f,1.0f); updateObjectTransform(); updateCameraTransform(true); }
void CameraArcBallDemo::onEnter() { CameraBaseTest::onEnter(); _rotationQuat.set(0.0f, 0.0f, 0.0f, 1.0f); schedule(schedule_selector(CameraArcBallDemo::update), 0.0f); auto s = Director::getInstance()->getWinSize(); auto listener = EventListenerTouchAllAtOnce::create(); listener->onTouchesMoved = CC_CALLBACK_2(CameraArcBallDemo::onTouchsMoved, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); // swich camera MenuItemFont::setFontName("fonts/arial.ttf"); MenuItemFont::setFontSize(20); auto menuItem1 = MenuItemFont::create("Switch Operation", CC_CALLBACK_1(CameraArcBallDemo::switchOperateCallback,this)); menuItem1->setColor(Color3B(0,200,20)); auto menuItem2 = MenuItemFont::create("Switch Target", CC_CALLBACK_1(CameraArcBallDemo::switchTargetCallback,this)); menuItem2->setColor(Color3B(0,200,20)); auto menu = Menu::create(menuItem1,menuItem2,NULL); menu->setPosition(Vec2::ZERO); menuItem1->setPosition(VisibleRect::left().x + 80, VisibleRect::top().y -70); menuItem2->setPosition(VisibleRect::left().x + 80, VisibleRect::top().y -100); addChild(menu, 1); auto layer3D=Layer::create(); addChild(layer3D,0); _layer3D=layer3D; if (_camera == nullptr) { _camera=Camera::createPerspective(60, (GLfloat)s.width/s.height, 1, 1000); _camera->setCameraFlag(CameraFlag::USER1); _camera->setPosition3D(Vec3(0, 10, 50)); _camera->lookAt(Vec3(0, 0, 0), Vec3(0, 1, 0)); _camera->retain(); _layer3D->addChild(_camera); } _sprite3D1 = Sprite3D::create("Sprite3DTest/orc.c3b"); _sprite3D1->setScale(0.5); _sprite3D1->setRotation3D(Vec3(0,180,0)); _sprite3D1->setPosition3D(Vec3(0,0,0)); _layer3D->addChild(_sprite3D1); _sprite3D2 = Sprite3D::create("Sprite3DTest/boss.c3b"); _sprite3D2->setScale(0.6f); _sprite3D2->setRotation3D(Vec3(-90,0,0)); _sprite3D2->setPosition3D(Vec3(20,0,0)); _layer3D->addChild(_sprite3D2); _drawGrid =DrawNode3D::create(); //draw x for( int j =-20; j<=20 ;j++) { _drawGrid->drawLine(Vec3(-100, 0, 5*j),Vec3(100,0,5*j),Color4F(1,0,0,1)); } //draw z for( int j =-20; j<=20 ;j++) { _drawGrid->drawLine(Vec3(5*j, 0, -100),Vec3(5*j,0,100),Color4F(0,0,1,1)); } //draw y _drawGrid->drawLine(Vec3(0, 0, 0),Vec3(0,50,0),Color4F(0,1,0,1)); _layer3D->addChild(_drawGrid); _layer3D->setCameraMask(2); updateCameraTransform(); }