00001 00012 /*************************************************************************** 00013 * Copyright (C) 2007 by Jan Koci * 00014 * honza.koci@email.cz * 00015 * http://kengine.sourceforge.net/tutorial/ 00016 * * 00017 * This program is free software; you can redistribute it and/or modify * 00018 * it under the terms of the GNU General Public License as published by * 00019 * the Free Software Foundation; either version 2 of the License, or * 00020 * (at your option) any later version. * 00021 * * 00022 * This program is distributed in the hope that it will be useful, * 00023 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00024 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00025 * GNU General Public License for more details. * 00026 * * 00027 * You should have received a copy of the GNU General Public License * 00028 * along with this program; if not, write to the * 00029 * Free Software Foundation, Inc., * 00030 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00031 ***************************************************************************/ 00032 00033 00034 #include "K3dSphereWork.h" 00035 00036 K3dSphereWork::K3dSphereWork(K3dGameData *_pGameData) 00037 { 00038 m_iSphereId = -1; 00039 m_pGameData = _pGameData; 00040 m_eRotAxis = K3D_ROT_AXIS_X; 00041 m_bIsMouseRightUp = true; 00042 m_pPlane = NULL; 00043 m_pIntersection = m_pGameData->GetIntersection(); 00044 m_pPlaneBuild = m_pGameData->GetPlaneBuild(); 00045 InitSphereWork(); 00046 } 00047 00048 K3dSphereWork::~K3dSphereWork() 00049 { 00050 } 00051 00053 void K3dSphereWork::InitSphereWork() 00054 { 00055 if(m_pGameData->GetPlaneObjSP().FindPointer(m_pPlane) == false) 00056 { 00057 m_pPlane = m_pPlaneBuild->CreateNewPlaneObj(); 00058 } 00059 } 00060 00063 void K3dSphereWork::MoveByMouse(const int _iSphereId) 00064 { 00065 // Get sphere 00066 K3dSphereObj *pSphere = m_pGameData->GetSphereObjSP().Get(_iSphereId); 00067 if(pSphere) 00068 { 00069 // Get active camera index 00070 int iCamId = m_pGameData->GetActiveCamId(); 00071 if(iCamId>=0) 00072 { 00073 K3dMouse *pMouse = m_pGameData->GetMouse(); 00074 00075 // Get active camera 00076 K3dCameraObj *pCamera = m_pGameData->GetCameraObjSP().Get(iCamId); 00077 K3dVector3Obj kPos = *pSphere->GetPosition(); 00078 K3dVector3Obj kNormal = kPos - pCamera->GetPosition(); 00079 K3dVector3Obj kIntrPoint; 00080 if(pMouse->MouseButtonLeft()) 00081 { 00082 kNormal = m_pGameData->GetVector3Work()->Normalize(kNormal); 00083 // Create aiming plane by sphere position and camera view vector 00084 m_pPlaneBuild->CalcPlane(m_pPlane, kPos, kNormal); 00085 // Get aiming ray 00086 K3dRayObj *pRay = m_pGameData->GetRayObjSP().Get(m_pGameData->GetAimRayId()); 00087 // Calculate ray plane intersection 00088 if(m_pIntersection->LinePlane(*pRay, *m_pPlane)) 00089 { 00090 *pSphere->GetPosition() = m_pIntersection->GetIntrPoint(); 00091 } 00092 } 00093 if(pMouse->MouseButtonRight()) 00094 { 00095 if (m_bIsMouseRightUp) 00096 { 00097 m_bIsMouseRightUp = false; 00098 switch (m_eRotAxis) 00099 { 00100 case K3D_ROT_AXIS_X: 00101 m_eRotAxis = K3D_ROT_AXIS_Y; 00102 break; 00103 case K3D_ROT_AXIS_Y: 00104 m_eRotAxis = K3D_ROT_AXIS_Z; 00105 break; 00106 case K3D_ROT_AXIS_Z: 00107 m_eRotAxis = K3D_ROT_AXIS_X; 00108 break; 00109 default: 00110 break; 00111 } 00112 } 00113 } 00114 else 00115 { 00116 m_bIsMouseRightUp = true; 00117 } 00118 // Set rotation 00119 if(pMouse->MouseWheelButtonDown()) 00120 { 00121 switch (m_eRotAxis) 00122 { 00123 case K3D_ROT_AXIS_X: 00124 pSphere->GetRotation()->GetX() += 10; 00125 break; 00126 case K3D_ROT_AXIS_Y: 00127 pSphere->GetRotation()->GetY() += 10; 00128 break; 00129 case K3D_ROT_AXIS_Z: 00130 pSphere->GetRotation()->GetZ() += 10; 00131 break; 00132 default: 00133 break; 00134 } 00135 } 00136 if(pMouse->MouseWheelButtonUp()) 00137 { 00138 switch (m_eRotAxis) 00139 { 00140 case K3D_ROT_AXIS_X: 00141 pSphere->GetRotation()->GetX() -= 10; 00142 break; 00143 case K3D_ROT_AXIS_Y: 00144 pSphere->GetRotation()->GetY() -= 10; 00145 break; 00146 case K3D_ROT_AXIS_Z: 00147 pSphere->GetRotation()->GetZ() -= 10; 00148 break; 00149 default: 00150 break; 00151 } 00152 } 00153 pSphere->Move(*pSphere->GetPosition(), *pSphere->GetRotation()); 00154 } 00155 } 00156 else 00157 { 00158 cerr << "Error in void K3dSphereWork::MoveByMouse() -- pSphere = " << pSphere << endl; 00159 } 00160 } 00161 00163 void K3dSphereWork::UpdateSpheres() 00164 { 00165 for(int iSphereId=0; iSphereId < m_pGameData->GetSphereObjSP().GetNum(); iSphereId++) 00166 { 00167 // Get sphere 00168 K3dSphereObj *pSphere = m_pGameData->GetSphereObjSP().Get(iSphereId); 00169 for(TIntIntMap::iterator i = pSphere->GetLinkObjMap().begin(); i!=pSphere->GetLinkObjMap().end(); ++i) 00170 { 00171 // Get object index 00172 int iObjId = i->first; 00173 // Get object type 00174 int iObjType = i->second; 00175 K3dPlaneObj *pPlane; 00176 K3dLineObj *pLine; 00177 switch (iObjType) 00178 { 00179 case K3D_IMAGE_OBJ: 00180 break; 00181 case K3D_VERTEX_OBJ: 00182 break; 00183 case K3D_SPHERE_OBJ: 00184 break; 00185 case K3D_CAMERA_OBJ: 00186 break; 00187 case K3D_LINE_OBJ: 00188 // Get line 00189 pLine = m_pGameData->GetLineObjSP().Get( iObjId); 00190 // Change line matrix by sphere matrix 00191 pLine->SetMatrix(pSphere->GetMatrix()); 00192 break; 00193 case K3D_RAY_OBJ: 00194 break; 00195 case K3D_PLANE_OBJ: 00196 // Get plane 00197 pPlane = m_pGameData->GetPlaneObjSP().Get( iObjId); 00198 // Change plane matrix by sphere matrix 00199 pPlane->SetMatrix(pSphere->GetMatrix()); 00200 break; 00201 default: 00202 break; 00203 } 00204 } 00205 } 00206 } 00207 00209 void K3dSphereWork::LinkObject(const int _iSphereId, const int _iObjId, const int _iObjType) 00210 { 00211 K3dSphereObj *pSphere = m_pGameData->GetSphereObjSP().Get(_iSphereId); 00212 if(pSphere) 00213 { 00214 pSphere->GetLinkObjMap().insert(TIntIntPair(_iObjId, _iObjType)); 00215 } 00216 else 00217 { 00218 cerr << "Error in void K3dSphereWork::LinkObject() -- pSphere = " << pSphere << endl; 00219 } 00220 } 00221 00223 int K3dSphereWork::FindSphereId(const char* _strName) 00224 { 00225 K3dString strName(_strName); 00226 for(int i=0; i<m_pGameData->GetSphereObjSP().GetNum(); i++) 00227 { 00228 K3dSphereObj *pSphere = m_pGameData->GetSphereObjSP().Get(i); 00229 if(strName == pSphere->GetName()) 00230 { 00231 return i; 00232 } 00233 } 00234 return -1; 00235 } 00236