Big patches:
[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 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43
44 SumoPhysicsEnvironment::SumoPhysicsEnvironment()
45 {
46         m_fixedTimeStep = 1.f/60.f;
47         m_useFixedTimeStep = true;
48         m_currentTime = 0.f;
49
50         m_sumoScene = new SM_Scene();
51 }
52
53
54
55 SumoPhysicsEnvironment::~SumoPhysicsEnvironment()
56 {
57         delete m_sumoScene;
58 }
59
60
61
62 void SumoPhysicsEnvironment::beginFrame()
63 {
64         m_sumoScene->beginFrame();
65 }
66
67 void SumoPhysicsEnvironment::endFrame()
68 {
69         m_sumoScene->endFrame();
70 }
71
72 void            SumoPhysicsEnvironment::setFixedTimeStep(bool useFixedTimeStep,float fixedTimeStep)
73 {
74         m_useFixedTimeStep = useFixedTimeStep;
75         if (m_useFixedTimeStep)
76         {
77                 m_fixedTimeStep = fixedTimeStep;
78         } else
79         {
80                 m_fixedTimeStep  = 0.f;
81         }
82         //reset current time ?
83         m_currentTime = 0.f;
84 }
85 float           SumoPhysicsEnvironment::getFixedTimeStep()
86 {
87         return m_fixedTimeStep;
88 }
89
90
91 bool            SumoPhysicsEnvironment::proceedDeltaTime(double  curTime,float timeStep)
92 {
93         
94         bool result = false;
95         if (m_useFixedTimeStep)
96         {
97                 m_currentTime += timeStep;
98                 float ticrate = 1.f/m_fixedTimeStep;
99
100                 result = m_sumoScene->proceed(curTime, ticrate);
101         } else
102         {
103                 m_currentTime += timeStep;
104                 float ticrate = 1.f/timeStep;
105                 result = m_sumoScene->proceed(m_currentTime, timeStep);
106         }
107         return result;
108 }
109
110 void SumoPhysicsEnvironment::setGravity(float x,float y,float z)
111 {
112         m_sumoScene->setForceField(MT_Vector3(x,y,z));
113 }
114
115 int SumoPhysicsEnvironment::createConstraint(
116         class PHY_IPhysicsController* ctrl,
117         class PHY_IPhysicsController* ctrl2,
118         PHY_ConstraintType type,
119         float pivotX,float pivotY,float pivotZ,
120         float axisX,float axisY,float axisZ)
121 {
122         int constraintid = 0;
123         return constraintid;
124 }
125
126 void SumoPhysicsEnvironment::removeConstraint(int constraintid)
127 {
128         if (constraintid)
129         {
130         }
131 }
132
133 PHY_IPhysicsController* SumoPhysicsEnvironment::rayTest(PHY_IPhysicsController* ignoreClientCtrl, 
134         float fromX,float fromY,float fromZ, 
135         float toX,float toY,float toZ, 
136         float& hitX,float& hitY,float& hitZ,
137         float& normalX,float& normalY,float& normalZ)
138 {
139         SumoPhysicsController* ignoreCtr = static_cast<SumoPhysicsController*> (ignoreClientCtrl);
140
141         //collision detection / raytesting
142         MT_Point3 hit, normal;
143         PHY_IPhysicsController *ret = 0;
144
145         SM_Object* sm_ignore = 0;
146         if (ignoreCtr)
147                 sm_ignore = ignoreCtr->GetSumoObject();
148
149
150         SM_Object* smOb = m_sumoScene->rayTest(sm_ignore,MT_Point3(fromX, fromY, fromZ),MT_Point3(toX, toY, toZ), hit, normal);
151         if (smOb)
152         {
153                 ret = (PHY_IPhysicsController *) smOb->getPhysicsClientObject();
154         }
155         hitX = hit[0];
156         hitY = hit[1];
157         hitZ = hit[2];
158
159         normalX = normal[0];
160         normalY = normal[1];
161         normalZ = normal[2];
162         
163         return ret;
164 }
165 //gamelogic callbacks
166 void SumoPhysicsEnvironment::addSensor(PHY_IPhysicsController* ctrl)
167 {
168         SumoPhysicsController* smctrl = dynamic_cast<SumoPhysicsController*>(ctrl);
169         SM_Object* smObject = smctrl->GetSumoObject();
170         assert(smObject);
171         if (smObject)
172         {
173                 m_sumoScene->addSensor(*smObject);
174         }
175 }
176 void SumoPhysicsEnvironment::removeSensor(PHY_IPhysicsController* ctrl)
177 {
178         SumoPhysicsController* smctrl = dynamic_cast<SumoPhysicsController*>(ctrl);
179         SM_Object* smObject = smctrl->GetSumoObject();
180         assert(smObject);
181         if (smObject)
182         {
183                 m_sumoScene->remove(*smObject);
184         }
185 }
186
187
188 void SumoPhysicsEnvironment::addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user)
189 {
190         
191         int sumoRespClass = 0;
192
193         //map PHY_ convention into SM_ convention
194         switch (response_class)
195         {
196         case    PHY_FH_RESPONSE:
197                         sumoRespClass = FH_RESPONSE; 
198                 break;
199         case PHY_SENSOR_RESPONSE:
200                 sumoRespClass = SENSOR_RESPONSE;
201                 break;
202         case PHY_CAMERA_RESPONSE:
203                 sumoRespClass =CAMERA_RESPONSE;
204                 break;
205         case PHY_OBJECT_RESPONSE:
206                 sumoRespClass = OBJECT_RESPONSE;
207                 break;
208         case PHY_STATIC_RESPONSE:
209                 sumoRespClass = PHY_STATIC_RESPONSE;
210                 break;
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
260