|
Blender
V2.59
|
00001 /* 00002 * $Id: KX_RayCast.h 35063 2011-02-22 10:33:14Z jesterking $ 00003 * 00004 * ***** BEGIN GPL LICENSE BLOCK ***** 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software Foundation, 00018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00021 * All rights reserved. 00022 * 00023 * The Original Code is: all of this file. 00024 * 00025 * Contributor(s): none yet. 00026 * 00027 * ***** END GPL LICENSE BLOCK ***** 00028 */ 00029 00034 #ifndef __KX_RAYCAST_H__ 00035 #define __KX_RAYCAST_H__ 00036 00037 #include "PHY_IPhysicsEnvironment.h" 00038 #include "PHY_IPhysicsController.h" 00039 #include "MT_Vector2.h" 00040 #include "MT_Point3.h" 00041 #include "MT_Vector3.h" 00042 00043 class RAS_MeshObject; 00044 struct KX_ClientObjectInfo; 00045 class KX_IPhysicsController; 00046 00059 class KX_RayCast : public PHY_IRayCastFilterCallback 00060 { 00061 public: 00062 bool m_hitFound; 00063 MT_Point3 m_hitPoint; 00064 MT_Vector3 m_hitNormal; 00065 const RAS_MeshObject* m_hitMesh; 00066 int m_hitPolygon; 00067 int m_hitUVOK; // !=0 if UV coordinate in m_hitUV is valid 00068 MT_Vector2 m_hitUV; 00069 00070 KX_RayCast(KX_IPhysicsController* ignoreController, bool faceNormal, bool faceUV); 00071 virtual ~KX_RayCast() {} 00072 00076 virtual void reportHit(PHY_RayCastResult* result); 00077 00081 virtual bool RayHit(KX_ClientObjectInfo* client) = 0; 00082 00089 template<class T> class Callback; 00090 00093 static bool RayTest( 00094 PHY_IPhysicsEnvironment* physics_environment, 00095 const MT_Point3& frompoint, 00096 const MT_Point3& topoint, 00097 KX_RayCast& callback); 00098 00099 00100 #ifdef WITH_CXX_GUARDEDALLOC 00101 public: 00102 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:KX_RayCast"); } 00103 void operator delete( void *mem ) { MEM_freeN(mem); } 00104 #endif 00105 }; 00106 00107 template<class T> class KX_RayCast::Callback : public KX_RayCast 00108 { 00109 T *self; 00110 void *data; 00111 public: 00112 Callback(T *_self, KX_IPhysicsController* controller=NULL, void *_data = NULL, bool faceNormal=false, bool faceUV=false) 00113 : KX_RayCast(controller, faceNormal, faceUV), 00114 self(_self), 00115 data(_data) 00116 { 00117 } 00118 00119 ~Callback() {} 00120 00121 virtual bool RayHit(KX_ClientObjectInfo* client) 00122 { 00123 return self->RayHit(client, this, data); 00124 } 00125 00126 virtual bool needBroadphaseRayCast(PHY_IPhysicsController* controller) 00127 { 00128 KX_ClientObjectInfo* info = static_cast<KX_ClientObjectInfo*>(controller->getNewClientInfo()); 00129 00130 if (!info) 00131 { 00132 MT_assert(info && "Physics controller with no client object info"); 00133 return false; 00134 } 00135 return self->NeedRayCast(info); 00136 } 00137 00138 00139 #ifdef WITH_CXX_GUARDEDALLOC 00140 public: 00141 void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:KX_RayCast::Callback"); } 00142 void operator delete( void *mem ) { MEM_freeN(mem); } 00143 #endif 00144 }; 00145 00146 00147 #endif