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 #include "K3dIntrLinePlane.h" 00034 00035 K3dIntrLinePlane::K3dIntrLinePlane(K3dGameData *_pGameData): 00036 K3dDistance(_pGameData) 00037 { 00038 m_pGameData = _pGameData; 00039 } 00040 00041 K3dIntrLinePlane::~K3dIntrLinePlane() 00042 { 00043 } 00044 00047 bool K3dIntrLinePlane::LinePlane(const K3dRay &_rkLine, const K3dPlane &_rkPlane) 00048 { 00049 m_kIntrPoint.Reset(); 00050 // Calculate distance plane and line origin point 00051 m_fDistanceOrigin = PointPlane(_rkPlane, *_rkLine.GetOrigin()); 00052 // Calculate distance plane and line direction point 00053 m_fDistanceDirection = PointPlane(_rkPlane, *_rkLine.GetDirection()); 00054 // If sign of origin and direction is equal then return false 00055 int iSignOrigin = K3dMath::Sign(m_fDistanceOrigin); 00056 int iSignDistance = K3dMath::Sign(m_fDistanceDirection); 00057 if(iSignOrigin == iSignDistance) 00058 { 00059 switch (iSignOrigin) 00060 { 00061 case -1: 00062 m_eLinePos = K3D_BACK_LINE; 00063 break; 00064 case 0: 00065 m_eLinePos = K3D_ON_PLANE_LINE; 00066 break; 00067 case 1: 00068 m_eLinePos = K3D_FRONT_LINE; 00069 break; 00070 default: 00071 break; 00072 } 00073 return false; 00074 } 00075 // Calculate origin 00076 float fOrigin = m_pGameData->GetVector3Work()->Dot( *_rkPlane.GetNormal(), *_rkLine.GetOrigin()); 00077 fOrigin -= _rkPlane.GetDistance(); 00078 // Calculate direction 00079 K3dVector3Obj kLineDirection = *_rkLine.GetDirection() - *_rkLine.GetOrigin(); 00080 float fDirection = m_pGameData->GetVector3Work()->Dot( *_rkPlane.GetNormal(), kLineDirection ); 00081 // If direction is not zero then the line intersect the plane 00082 if(fDirection!=(float)0) 00083 { 00084 // Calculate t value 00085 float fValue = 1/fDirection; 00086 fValue = -fOrigin * fValue; 00087 // Calculate result point 00088 m_kIntrPoint = *_rkLine.GetOrigin() + (kLineDirection * fValue); 00089 // Set line position oposit the plane 00090 m_eLinePos = K3D_LINE_INTR_PLANE; 00091 return true; 00092 } 00093 // Set line position oposit the plane 00094 if(iSignOrigin == -1) 00095 { 00096 m_eLinePos = K3D_BACK_LINE; 00097 } 00098 else 00099 { 00100 m_eLinePos = K3D_FRONT_LINE; 00101 } 00102 return false; 00103 }