Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

# PS2PadCameraController.cpp

Go to the documentation of this file.

    00001 //------------------------------------------------------------------------------
    00002 // Lamp : Open source game middleware
    00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
    00004 //
    00005 // This library is free software; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public
    00007 // License as published by the Free Software Foundation; either
    00008 // version 2.1 of the License, or (at your option) any later version.
    00009 //
    00010 // This library is distributed in the hope that it will be useful,
    00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013 // Lesser General Public License for more details.
    00014 //
    00015 // You should have received a copy of the GNU Lesser General Public
    00016 // License along with this library; if not, write to the Free Software
    00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00018 //------------------------------------------------------------------------------
    00019
    00020 /** @file
    00021  * PS2パッドカメラコントローラ実装
    00022  * @author Junpee
    00023  */
    00024
    00025 #include "LampBasic.h"
    00026 #include "Framework/Utility/PS2PadCameraController.h"
    00027 #include "Graphics/Camera/Camera.h"
    00028 #include "Input/System/LampInput.h"
    00029 #include "Input/Pad/PS2Pad.h"
    00030 // アスペクト比
    00031 //#include "Graphics/System/LampGraphics.h"
    00032 //#include "Graphics/System/GraphicsDeviceCapacity.h"
    00033
    00034 namespace Lamp{
    00035
    00036 //------------------------------------------------------------------------------
    00037 // コンストラクタ
    00038 PS2PadCameraController::PS2PadCameraController() :
    00039     camera_(NULL), pad_(NULL), moveMode_(true){
    00040     rotationSensibility_ = 0.025f;
    00041     translationSensibility_ = 0.5f;
    00042     fovYSensibility_ = 0.01f;
    00043 }
    00044 //------------------------------------------------------------------------------
    00045 // デストラクタ
    00046 PS2PadCameraController::~PS2PadCameraController(){
    00047     SafeDelete(pad_);
    00048 }
    00049 //------------------------------------------------------------------------------
    00050 // コントロール
    00051 void PS2PadCameraController::control(){
    00052     if((camera_ == NULL) || (pad_ == NULL)){ return; }
    00053     // XY回転
    00054     Vector3 rotation = camera_->getRotation();
    00055     Point2f rightAxis = pad_->getRightAxis();
    00056     rightAxis *= rotationSensibility_;
    00057     Quaternion rotationQuaternion;
    00058     rotationQuaternion.setRotationXYZ(rotation);
    00059     Quaternion addQuaternion;
    00060     addQuaternion.setRotationAxis(Vector3::unitY, -rightAxis.x);
    00061     rotationQuaternion = addQuaternion * rotationQuaternion;
    00062     addQuaternion.setRotationAxis(Vector3::unitX, -rightAxis.y);
    00063     rotationQuaternion = rotationQuaternion * addQuaternion;
    00064     rotationQuaternion.getRotationXYZ(&rotation);
    00065
    00066     // 位置
    00067     Vector3 position = camera_->getPosition();
    00068     Point2f leftAxis = pad_->getLeftAxis();
    00069     leftAxis *= translationSensibility_;
    00070     Vector3 addPosition;
    00071     if(pad_->upKeyDown()){
    00072         translationSensibility_ *= 2.0f;
    00073     }
    00074     if(pad_->downKeyDown()){ translationSensibility_ *= 0.5f; }
    00075     if(moveMode_){
    00076         addPosition.x = leftAxis.x;
    00077         addPosition.y = -leftAxis.y;
    00078         addPosition.z = 0.f;
    00079         if(pad_->buttonPressed(PS2Pad::buttonL1)){
    00080             addPosition.z -= translationSensibility_;
    00081         }
    00082         if(pad_->buttonPressed(PS2Pad::buttonL2)){
    00083             addPosition.z += translationSensibility_;
    00084         }
    00085     }else{
    00086         addPosition.x = leftAxis.x;
    00087         addPosition.y = 0.f;
    00088         if(pad_->buttonPressed(PS2Pad::buttonL1)){
    00089             addPosition.y += translationSensibility_;
    00090         }
    00091         if(pad_->buttonPressed(PS2Pad::buttonL2)){
    00092             addPosition.y -= translationSensibility_;
    00093         }
    00094         addPosition.z = leftAxis.y;
    00095     }
    00096     addPosition = rotationQuaternion * addPosition;
    00097     position += addPosition;
    00098     camera_->setTransformation(rotation, position);
    00099
    00100     // 移動モード切替
    00101     if(pad_->buttonDown(PS2Pad::buttonL3)){
    00102         moveMode_ = !moveMode_;
    00103     }
    00104
    00105     // 画角
    00106     float fovY = camera_->getFovY();
    00107     float aspect = camera_->getAspect();
    00108 //  aspect = LampGraphics::getDeviceCapacity()->getBackBufferAspectRate();
    00109     float nearClip = camera_->getNearClip();
    00110     float farClip = camera_->getFarClip();
    00111     if(pad_->buttonPressed(PS2Pad::buttonR1)){
    00112         fovY -= fovYSensibility_;
    00113         if(fovY < Math::toRadian(1.f)){ fovY = Math::toRadian(1.f); }
    00114     }
    00115     if(pad_->buttonPressed(PS2Pad::buttonR2)){
    00116         fovY += fovYSensibility_;
    00117         if(fovY > Math::toRadian(179.f)){ fovY = Math::toRadian(179.f); }
    00118     }
    00119     camera_->setPerspectiveFovY(fovY, aspect, nearClip, farClip);
    00120 }
    00121 //------------------------------------------------------------------------------
    00122 // ジョイスティックの設定
    00123 bool PS2PadCameraController::setJoystick(Joystick* joystick){
    00124     if(!PS2Pad::checkCompatibility(joystick)){ return false; }
    00125     pad_ = new PS2Pad(joystick);
    00126     return true;
    00127 }
    00128 //------------------------------------------------------------------------------
    00129 // ジョイスティックの取得
    00130 Joystick* PS2PadCameraController::getJoystick(){
    00131     if(pad_ == NULL){ return NULL; }
    00132     return pad_->getJoystick();
    00133 }
    00134 //------------------------------------------------------------------------------
    00135 // ジョイスティックの検索
    00136 Joystick* PS2PadCameraController::searchJoystick(){
    00137     int joystickCount = LampInput::getJoystickCount();
    00138     for(int i = 0; i < joystickCount; i++){
    00139         Joystick* joystick = LampInput::getJoystick(i);
    00140         if(setJoystick(joystick)){ return joystick; }
    00141     }
    00142     return NULL;
    00143 }
    00144 //------------------------------------------------------------------------------
    00145 } // End of namespace Lamp
    00146 //------------------------------------------------------------------------------