Blender V2.61 - r43446

KX_RayCast.h

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