Radar/Near sensor performance problem fixed
[blender-staging.git] / source / gameengine / Physics / Sumo / SumoPhysicsEnvironment.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. The Blender
10  * Foundation also sells licenses for use in proprietary software under
11  * the Blender License.  See http://www.blender.org/BL/ for information
12  * about this.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32 #include "SumoPhysicsEnvironment.h"
33 #include "PHY_IMotionState.h"
34 #include "SumoPhysicsController.h"
35 #include "SM_Scene.h"
36 #include "SumoPHYCallbackBridge.h"
37 #include <SOLID/SOLID.h>
38
39 SumoPhysicsEnvironment::SumoPhysicsEnvironment()
40 {
41         m_fixedTimeStep = 1.f/60.f;
42         m_useFixedTimeStep = true;
43         m_currentTime = 0.f;
44
45         m_sumoScene = new SM_Scene();
46 }
47
48
49
50 SumoPhysicsEnvironment::~SumoPhysicsEnvironment()
51 {
52         delete m_sumoScene;
53 }
54
55
56
57 void SumoPhysicsEnvironment::beginFrame()
58 {
59         m_sumoScene->beginFrame();
60 }
61
62 void SumoPhysicsEnvironment::endFrame()
63 {
64         m_sumoScene->endFrame();
65 }
66
67 void            SumoPhysicsEnvironment::setFixedTimeStep(bool useFixedTimeStep,float fixedTimeStep)
68 {
69         m_useFixedTimeStep = useFixedTimeStep;
70         if (m_useFixedTimeStep)
71         {
72                 m_fixedTimeStep = fixedTimeStep;
73         } else
74         {
75                 m_fixedTimeStep  = 0.f;
76         }
77         //reset current time ?
78         m_currentTime = 0.f;
79 }
80 float           SumoPhysicsEnvironment::getFixedTimeStep()
81 {
82         return m_fixedTimeStep;
83 }
84
85
86 bool            SumoPhysicsEnvironment::proceedDeltaTime(double  curTime,float timeStep)
87 {
88         
89         bool result = false;
90         if (m_useFixedTimeStep)
91         {
92                 m_currentTime += timeStep;
93                 float ticrate = 1.f/m_fixedTimeStep;
94
95                 result = m_sumoScene->proceed(curTime, ticrate);
96         } else
97         {
98                 m_currentTime += timeStep;
99                 result = m_sumoScene->proceed(m_currentTime, timeStep);
100         }
101         return result;
102 }
103
104 void SumoPhysicsEnvironment::setGravity(float x,float y,float z)
105 {
106         m_sumoScene->setForceField(MT_Vector3(x,y,z));
107 }
108
109 int SumoPhysicsEnvironment::createConstraint(
110         class PHY_IPhysicsController* ctrl,
111         class PHY_IPhysicsController* ctrl2,
112         PHY_ConstraintType type,
113         float pivotX,float pivotY,float pivotZ,
114         float axisX,float axisY,float axisZ,
115         float axis1X,float axis1Y,float axis1Z,
116         float axis2X,float axis2Y,float axis2Z
117
118         )
119 {
120         int constraintid = 0;
121         return constraintid;
122 }
123
124 void SumoPhysicsEnvironment::removeConstraint(int       constraintid)
125 {
126         if (constraintid)
127         {
128         }
129 }
130
131 PHY_IPhysicsController* SumoPhysicsEnvironment::rayTest(PHY_IPhysicsController* ignoreClientCtrl, 
132         float fromX,float fromY,float fromZ, 
133         float toX,float toY,float toZ, 
134         float& hitX,float& hitY,float& hitZ,
135         float& normalX,float& normalY,float& normalZ)
136 {
137         SumoPhysicsController* ignoreCtr = static_cast<SumoPhysicsController*> (ignoreClientCtrl);
138
139         //collision detection / raytesting
140         MT_Point3 hit, normal;
141         PHY_IPhysicsController *ret = 0;
142
143         SM_Object* sm_ignore = 0;
144         if (ignoreCtr)
145                 sm_ignore = ignoreCtr->GetSumoObject();
146
147
148         SM_Object* smOb = m_sumoScene->rayTest(sm_ignore,MT_Point3(fromX, fromY, fromZ),MT_Point3(toX, toY, toZ), hit, normal);
149         if (smOb)
150         {
151                 ret = (PHY_IPhysicsController *) smOb->getPhysicsClientObject();
152         }
153         hitX = hit[0];
154         hitY = hit[1];
155         hitZ = hit[2];
156
157         normalX = normal[0];
158         normalY = normal[1];
159         normalZ = normal[2];
160         
161         return ret;
162 }
163 //gamelogic callbacks
164 void SumoPhysicsEnvironment::addSensor(PHY_IPhysicsController* ctrl)
165 {
166         SumoPhysicsController* smctrl = dynamic_cast<SumoPhysicsController*>(ctrl);
167         SM_Object* smObject = smctrl->GetSumoObject();
168         assert(smObject);
169         if (smObject)
170         {
171                 m_sumoScene->addSensor(*smObject);
172         }
173 }
174 void SumoPhysicsEnvironment::removeSensor(PHY_IPhysicsController* ctrl)
175 {
176         SumoPhysicsController* smctrl = dynamic_cast<SumoPhysicsController*>(ctrl);
177         SM_Object* smObject = smctrl->GetSumoObject();
178         assert(smObject);
179         if (smObject)
180         {
181                 m_sumoScene->remove(*smObject);
182         }
183 }
184
185
186 void SumoPhysicsEnvironment::addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user)
187 {
188         
189         int sumoRespClass = 0;
190
191         //map PHY_ convention into SM_ convention
192         switch (response_class)
193         {
194         case    PHY_FH_RESPONSE:
195                         sumoRespClass = FH_RESPONSE; 
196                 break;
197         case PHY_SENSOR_RESPONSE:
198                 sumoRespClass = SENSOR_RESPONSE;
199                 break;
200         case PHY_CAMERA_RESPONSE:
201                 sumoRespClass =CAMERA_RESPONSE;
202                 break;
203         case PHY_OBJECT_RESPONSE:
204                 sumoRespClass = OBJECT_RESPONSE;
205                 break;
206         case PHY_STATIC_RESPONSE:
207                 sumoRespClass = PHY_STATIC_RESPONSE;
208                 break;
209         case PHY_BROADPH_RESPONSE:
210                 return;
211         default:
212                 assert(0);
213                 return;
214         }
215
216         SumoPHYCallbackBridge* bridge = new SumoPHYCallbackBridge(user,callback);
217
218         m_sumoScene->addTouchCallback(sumoRespClass,SumoPHYCallbackBridge::StaticSolidToPHYCallback,bridge);
219 }
220 void SumoPhysicsEnvironment::requestCollisionCallback(PHY_IPhysicsController* ctrl)
221 {
222         SumoPhysicsController* smctrl = dynamic_cast<SumoPhysicsController*>(ctrl);
223         MT_assert(smctrl);
224         SM_Object* smObject = smctrl->GetSumoObject();
225         MT_assert(smObject);
226         if (smObject)
227         {
228                 //assert(smObject->getPhysicsClientObject() == ctrl);
229                 smObject->setPhysicsClientObject(ctrl);
230         
231                 m_sumoScene->requestCollisionCallback(*smObject);
232         }
233 }
234 PHY_IPhysicsController* SumoPhysicsEnvironment::CreateSphereController(float radius,const PHY__Vector3& position)
235 {
236         DT_ShapeHandle shape    =       DT_NewSphere(0.0);
237         SM_Object* ob = new SM_Object(shape,0,0,0);     
238         ob->setPosition(MT_Point3(position));
239         //testing
240         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));              
241         ob->setOrientation(rotquatje);
242
243         PHY_IPhysicsController* ctrl = new SumoPhysicsController(m_sumoScene,ob,0,false);
244         ctrl->SetMargin(radius);
245         return ctrl;
246 }
247 PHY_IPhysicsController* SumoPhysicsEnvironment::CreateConeController(float coneradius,float coneheight)
248 {
249         DT_ShapeHandle shape    =       DT_NewCone(coneradius,coneheight);
250         SM_Object* ob = new SM_Object(shape,0,0,0);     
251         ob->setPosition(MT_Point3(0.f,0.f,0.f));
252         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));              
253         ob->setOrientation(rotquatje);
254
255         PHY_IPhysicsController* ctrl = new SumoPhysicsController(m_sumoScene,ob,0,false);
256
257         return ctrl;
258 }
259