synched with trunk at revision 36569
[blender.git] / source / gameengine / Ketsji / KX_ObstacleSimulation.h
1 /**
2 * Simulation for obstacle avoidance behavior 
3 * (based on Cane Project - http://code.google.com/p/cane  by Mikko Mononen (c) 2009)
4 *
5 *
6 * $Id$
7 *
8 * ***** BEGIN GPL LICENSE BLOCK *****
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version. The Blender
14 * Foundation also sells licenses for use in proprietary software under
15 * the Blender License.  See http://www.blender.org/BL/ for information
16 * about this.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26 *
27 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
28 * All rights reserved.
29 *
30 * The Original Code is: all of this file.
31 *
32 * Contributor(s): none yet.
33 *
34 * ***** END GPL LICENSE BLOCK *****
35 */
36
37 #ifndef __KX_OBSTACLESIMULATION
38 #define __KX_OBSTACLESIMULATION
39
40 #include <vector>
41 #include "MT_Point2.h"
42 #include "MT_Point3.h"
43
44 class KX_GameObject;
45 class KX_NavMeshObject;
46
47 enum KX_OBSTACLE_TYPE
48 {       
49         KX_OBSTACLE_OBJ, 
50         KX_OBSTACLE_NAV_MESH,
51 };
52
53 enum KX_OBSTACLE_SHAPE
54 {       
55         KX_OBSTACLE_CIRCLE, 
56         KX_OBSTACLE_SEGMENT,
57 };
58
59 #define VEL_HIST_SIZE 6
60 struct KX_Obstacle
61 {
62         KX_OBSTACLE_TYPE m_type;
63         KX_OBSTACLE_SHAPE m_shape;
64         MT_Point3 m_pos;
65         MT_Point3 m_pos2;
66         MT_Scalar m_rad;
67         
68         float vel[2];
69         float pvel[2];
70         float dvel[2];
71         float nvel[2];
72         float hvel[VEL_HIST_SIZE*2];
73         int hhead;
74
75         
76         KX_GameObject* m_gameObj;
77 };
78 typedef std::vector<KX_Obstacle*> KX_Obstacles;
79
80 class KX_ObstacleSimulation
81 {
82 protected:
83         KX_Obstacles m_obstacles;
84
85         MT_Scalar m_levelHeight;
86         bool m_enableVisualization;
87
88         KX_Obstacle* CreateObstacle(KX_GameObject* gameobj);
89 public:
90         KX_ObstacleSimulation(MT_Scalar levelHeight, bool enableVisualization);
91         virtual ~KX_ObstacleSimulation();
92
93         void DrawObstacles();
94         //void DebugDraw();
95
96         void AddObstacleForObj(KX_GameObject* gameobj);
97         void DestroyObstacleForObj(KX_GameObject* gameobj);
98         void AddObstaclesForNavMesh(KX_NavMeshObject* navmesh);
99         KX_Obstacle* GetObstacle(KX_GameObject* gameobj);
100         void UpdateObstacles(); 
101         virtual void AdjustObstacleVelocity(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
102                                                                 MT_Vector3& velocity, MT_Scalar maxDeltaSpeed,MT_Scalar maxDeltaAngle);
103
104 }; 
105 class KX_ObstacleSimulationTOI: public KX_ObstacleSimulation
106 {
107 protected:
108         int m_maxSamples;                               // Number of sample steps
109         float m_minToi;                                 // Min TOI
110         float m_maxToi;                                 // Max TOI
111         float m_velWeight;                              // Sample selection angle weight
112         float m_curVelWeight;                   // Sample selection current velocity weight
113         float m_toiWeight;                              // Sample selection TOI weight
114         float m_collisionWeight;                // Sample selection collision weight
115
116         virtual void sampleRVO(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
117                                                         const float maxDeltaAngle) = 0;
118 public:
119         KX_ObstacleSimulationTOI(MT_Scalar levelHeight, bool enableVisualization);
120         virtual void AdjustObstacleVelocity(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
121                 MT_Vector3& velocity, MT_Scalar maxDeltaSpeed,MT_Scalar maxDeltaAngle);
122 };
123
124 class KX_ObstacleSimulationTOI_rays: public KX_ObstacleSimulationTOI
125 {
126 protected:
127         virtual void sampleRVO(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
128                                                         const float maxDeltaAngle);
129 public:
130         KX_ObstacleSimulationTOI_rays(MT_Scalar levelHeight, bool enableVisualization);
131 };
132
133 class KX_ObstacleSimulationTOI_cells: public KX_ObstacleSimulationTOI
134 {
135 protected:
136         float m_bias;
137         bool m_adaptive;
138         int m_sampleRadius;
139         virtual void sampleRVO(KX_Obstacle* activeObst, KX_NavMeshObject* activeNavMeshObj, 
140                                                         const float maxDeltaAngle);
141 public:
142         KX_ObstacleSimulationTOI_cells(MT_Scalar levelHeight, bool enableVisualization);
143 };
144
145 #endif