00001
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "K3dIntersection.h"
00034
00035
00036 K3dIntersection::K3dIntersection(K3dGameData *_pGameData):
00037 K3dIntrLinSph(_pGameData)
00038 {
00039 m_pGameData = _pGameData;
00040
00041 m_pGameData->SetIntersection(this);
00042 m_pTrianglePlane = NULL;
00043 }
00044
00045 K3dIntersection::~K3dIntersection(void)
00046 {
00047 }
00048
00049
00050
00051
00052 bool K3dIntersection::RayPlane(const K3dRay &_rkRay, const K3dPlane &_rkPlane, K3dVector3Obj &_rkIPoint)
00053 {
00054 K3dVector3 kOrigin;
00055 float fOrigin;
00056 K3dVector3Obj kDirection;
00057 float fDirection;
00058 float fValue;
00059 K3dVector3Obj kRayDirection;
00060 K3dVector3 kPlaneDistance;
00061 K3dVector3 kSign;
00062 float fSign;
00063
00064
00065 int iDistOrigin = (int) PointPlane(_rkPlane, *_rkRay.GetOrigin());
00066
00067 int iDistDirection = (int) PointPlane(_rkPlane, *_rkRay.GetDirection());
00068
00069
00070
00071 if(K3dMath::Sign(iDistOrigin) == K3dMath::Sign(iDistDirection))
00072 {
00073 return false;
00074 }
00075
00076
00077
00078 fOrigin = m_pGameData->GetVector3Work()->Dot(*_rkPlane.GetNormal(), *_rkRay.GetOrigin());
00079 fOrigin -= _rkPlane.GetDistance();
00080
00081
00082 kRayDirection = *_rkRay.GetDirection() - *_rkRay.GetOrigin();
00083
00084 fDirection = m_pGameData->GetVector3Work()->Dot(*_rkPlane.GetNormal(), kRayDirection);
00085
00086
00087 if(fDirection)
00088 {
00089
00090
00091 fSign = m_pGameData->GetVector3Work()->Dot(*_rkPlane.GetNormal(), *_rkRay.GetDirection());
00092
00093 if(fSign < (float)0)
00094 {
00095 return false;
00096 }
00097
00098 fValue = -fOrigin/fDirection;
00099
00100 _rkIPoint = *_rkRay.GetOrigin() + (kRayDirection * fValue);
00101 return true;
00102 }
00103
00104 _rkIPoint.Reset();
00105 return false;
00106 }
00107
00108
00109
00110 bool K3dIntersection::LineTriangle(const K3dRay &_rkEdge, const K3dTriangle &_rkTriangle)
00111 {
00112 K3dVector3Obj kE, kF, kG, kNE;
00113 float fDotF = 0.0f;
00114 float fDotG = 0.0f;
00115 float fResult = -1.0f;
00116 bool bInter = false;
00117
00118 kE.Reset();
00119 kF.Reset();
00120 kG.Reset();
00121 kNE.Reset();
00122
00123
00124
00125 m_pGameData->GetPlaneWork()->CalcPlane(m_pTrianglePlane, *_rkTriangle.GetVertex(0), *_rkTriangle.GetNormal());
00126
00127 bInter = LinePlane(_rkEdge, *m_pTrianglePlane);
00128
00129
00130 if(!bInter)
00131 {
00132 return false;
00133 }
00134
00135
00136 for(int i=0; i<3; i++)
00137 {
00138
00139
00140
00141
00142
00143 kE = *_rkTriangle.GetVertex((i+1)%3) - *_rkTriangle.GetVertex(i);
00144 kF = *_rkTriangle.GetVertex((i+2)%3) - *_rkTriangle.GetVertex(i);
00145 kG = GetIntrPoint() - *_rkTriangle.GetVertex(i);
00146
00147
00148
00149 kNE[0] = -kE[1];
00150 kNE[1] = kE[0];
00151 kNE = m_pGameData->GetVector3Work()->Normalize(kNE);
00152
00153
00154
00155
00156 fDotF = m_pGameData->GetVector3Work()->Dot(kF, kNE);
00157 fDotG = m_pGameData->GetVector3Work()->Dot(kG, kNE);
00158 fResult = fDotF * fDotG;
00159 if(fResult < 0)
00160 {
00161 return false;
00162 }
00163 }
00164
00165 return true;
00166 }
00167
00168
00169 bool K3dIntersection::RayTriangle(const K3dRay &_rkRay, const K3dTriangle &_rkTriangle, K3dVector3Obj &_rkIPoint)
00170 {
00171 K3dVector3Obj kE, kF, kG, kNE;
00172 float fDotF = 0.0f;
00173 float fDotG = 0.0f;
00174 float fResult = -1.0f;
00175 bool bInter = false;
00176
00177
00178 kE.Reset();
00179 kF.Reset();
00180 kG.Reset();
00181 kNE.Reset();
00182
00183
00184 m_pGameData->GetPlaneWork()->CalcPlane(m_pTrianglePlane, *_rkTriangle.GetVertex(0), *_rkTriangle.GetNormal());
00185
00186 bInter = RayPlane(_rkRay, *m_pTrianglePlane, _rkIPoint);
00187
00188
00189
00190
00191
00192
00193 if(!bInter)
00194 {
00195 return false;
00196 }
00197
00198
00199 for(int i=0; i<3; i++)
00200 {
00201
00202 kE = *_rkTriangle.GetVertex((i+1)%3) - *_rkTriangle.GetVertex(i);
00203 kF = *_rkTriangle.GetVertex((i+2)%3) - *_rkTriangle.GetVertex(i);
00204 kG = _rkIPoint - *_rkTriangle.GetVertex(i);
00205
00206
00207
00208 kNE[0] = -kE[1];
00209 kNE[1] = kE[0];
00210 kNE = m_pGameData->GetVector3Work()->Normalize(kNE);
00211
00212
00213
00214
00215 fDotF = m_pGameData->GetVector3Work()->Dot(kF, kNE);
00216 fDotG = m_pGameData->GetVector3Work()->Dot(kG, kNE);
00217 fResult = fDotF * fDotG;
00218 if(fResult < 0)
00219 {
00220 return false;
00221 }
00222 }
00223
00224 return true;
00225 }