Blender V2.61 - r43446

KX_ObstacleSimulation.h

Go to the documentation of this file.
00001 /*
00002  * Simulation for obstacle avoidance behavior 
00003  * (based on Cane Project - http://code.google.com/p/cane  by Mikko Mononen (c) 2009)
00004  *
00005  * ***** BEGIN GPL LICENSE BLOCK *****
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software Foundation,
00019  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  * Contributor(s): none yet.
00022  *
00023  * ***** END GPL LICENSE BLOCK *****
00024  */
00025 
00026 #ifndef __KX_OBSTACLESIMULATION
00027 #define __KX_OBSTACLESIMULATION
00028 
00029 #include <vector>
00030 #include "MT_Point2.h"
00031 #include "MT_Point3.h"
00032 
00033 class KX_GameObject;
00034 class KX_NavMeshObject;
00035 
00036 enum KX_OBSTACLE_TYPE
00037 {   
00038     KX_OBSTACLE_OBJ, 
00039     KX_OBSTACLE_NAV_MESH,
00040 };
00041 
00042 enum KX_OBSTACLE_SHAPE
00043 {   
00044     KX_OBSTACLE_CIRCLE, 
00045     KX_OBSTACLE_SEGMENT,
00046 };
00047 
00048 #define VEL_HIST_SIZE 6
00049 struct KX_Obstacle
00050 {
00051     KX_OBSTACLE_TYPE m_type;
00052     KX_OBSTACLE_SHAPE m_shape;
00053     MT_Point3 m_pos;
00054     MT_Point3 m_pos2;
00055     MT_Scalar m_rad;
00056     
00057     float vel[2];
00058     float pvel[2];
00059     float dvel[2];
00060     float nvel[2];
00061     float hvel[VEL_HIST_SIZE*2];
00062     int hhead;
00063 
00064     
00065     KX_GameObject* m_gameObj;
00066 };
00067 typedef std::vector<KX_Obstacle*> KX_Obstacles;
00068 
00069 class KX_ObstacleSimulation
00070 {
00071 protected:
00072     KX_Obstacles m_obstacles;
00073 
00074     MT_Scalar m_levelHeight;
00075     bool m_enableVisualization;
00076 
00077     KX_Obstacle* CreateObstacle(KX_GameObject* gameobj);
00078 public:
00079     KX_ObstacleSimulation(MT_Scalar levelHeight, bool enableVisualization);
00080     virtual ~KX_ObstacleSimulation();
00081 
00082     void DrawObstacles();
00083     //void DebugDraw();
00084 
00085     void AddObstacleForObj(KX_GameObject* gameobj);
00086     void DestroyObstacleForObj(KX_GameObject* gameobj);
00087     void AddObstaclesForNavMesh(KX_NavMeshObject* navmesh);
00088     KX_Obstacle* GetObstacle(KX_GameObject* gameobj);
00089     void UpdateObstacles(); 
00090     virtual void AdjustObstacleVelocity(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
00091                                 MT_Vector3& velocity, MT_Scalar maxDeltaSpeed,MT_Scalar maxDeltaAngle);
00092 
00093 }; 
00094 class KX_ObstacleSimulationTOI: public KX_ObstacleSimulation
00095 {
00096 protected:
00097     int m_maxSamples;               // Number of sample steps
00098     float m_minToi;                 // Min TOI
00099     float m_maxToi;                 // Max TOI
00100     float m_velWeight;              // Sample selection angle weight
00101     float m_curVelWeight;           // Sample selection current velocity weight
00102     float m_toiWeight;              // Sample selection TOI weight
00103     float m_collisionWeight;        // Sample selection collision weight
00104 
00105     virtual void sampleRVO(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
00106                             const float maxDeltaAngle) = 0;
00107 public:
00108     KX_ObstacleSimulationTOI(MT_Scalar levelHeight, bool enableVisualization);
00109     virtual void AdjustObstacleVelocity(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
00110         MT_Vector3& velocity, MT_Scalar maxDeltaSpeed,MT_Scalar maxDeltaAngle);
00111 };
00112 
00113 class KX_ObstacleSimulationTOI_rays: public KX_ObstacleSimulationTOI
00114 {
00115 protected:
00116     virtual void sampleRVO(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
00117                             const float maxDeltaAngle);
00118 public:
00119     KX_ObstacleSimulationTOI_rays(MT_Scalar levelHeight, bool enableVisualization);
00120 };
00121 
00122 class KX_ObstacleSimulationTOI_cells: public KX_ObstacleSimulationTOI
00123 {
00124 protected:
00125     float m_bias;
00126     bool m_adaptive;
00127     int m_sampleRadius;
00128     virtual void sampleRVO(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
00129                             const float maxDeltaAngle);
00130 public:
00131     KX_ObstacleSimulationTOI_cells(MT_Scalar levelHeight, bool enableVisualization);
00132 };
00133 
00134 #endif