I'll break this commit into two sections in the moto files
[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 {
116         int constraintid = 0;
117         return constraintid;
118 }
119
120 void SumoPhysicsEnvironment::removeConstraint(int constraintid)
121 {
122         if (constraintid)
123         {
124         }
125 }
126
127 PHY_IPhysicsController* SumoPhysicsEnvironment::rayTest(PHY_IPhysicsController* ignoreClientCtrl, 
128         float fromX,float fromY,float fromZ, 
129         float toX,float toY,float toZ, 
130         float& hitX,float& hitY,float& hitZ,
131         float& normalX,float& normalY,float& normalZ)
132 {
133         SumoPhysicsController* ignoreCtr = static_cast<SumoPhysicsController*> (ignoreClientCtrl);
134
135         //collision detection / raytesting
136         MT_Point3 hit, normal;
137         PHY_IPhysicsController *ret = 0;
138
139         SM_Object* sm_ignore = 0;
140         if (ignoreCtr)
141                 sm_ignore = ignoreCtr->GetSumoObject();
142
143
144         SM_Object* smOb = m_sumoScene->rayTest(sm_ignore,MT_Point3(fromX, fromY, fromZ),MT_Point3(toX, toY, toZ), hit, normal);
145         if (smOb)
146         {
147                 ret = (PHY_IPhysicsController *) smOb->getPhysicsClientObject();
148         }
149         hitX = hit[0];
150         hitY = hit[1];
151         hitZ = hit[2];
152
153         normalX = normal[0];
154         normalY = normal[1];
155         normalZ = normal[2];
156         
157         return ret;
158 }
159 //gamelogic callbacks
160 void SumoPhysicsEnvironment::addSensor(PHY_IPhysicsController* ctrl)
161 {
162         SumoPhysicsController* smctrl = dynamic_cast<SumoPhysicsController*>(ctrl);
163         SM_Object* smObject = smctrl->GetSumoObject();
164         assert(smObject);
165         if (smObject)
166         {
167                 m_sumoScene->addSensor(*smObject);
168         }
169 }
170 void SumoPhysicsEnvironment::removeSensor(PHY_IPhysicsController* ctrl)
171 {
172         SumoPhysicsController* smctrl = dynamic_cast<SumoPhysicsController*>(ctrl);
173         SM_Object* smObject = smctrl->GetSumoObject();
174         assert(smObject);
175         if (smObject)
176         {
177                 m_sumoScene->remove(*smObject);
178         }
179 }
180
181
182 void SumoPhysicsEnvironment::addTouchCallback(int response_class, PHY_ResponseCallback callback, void *user)
183 {
184         
185         int sumoRespClass = 0;
186
187         //map PHY_ convention into SM_ convention
188         switch (response_class)
189         {
190         case    PHY_FH_RESPONSE:
191                         sumoRespClass = FH_RESPONSE; 
192                 break;
193         case PHY_SENSOR_RESPONSE:
194                 sumoRespClass = SENSOR_RESPONSE;
195                 break;
196         case PHY_CAMERA_RESPONSE:
197                 sumoRespClass =CAMERA_RESPONSE;
198                 break;
199         case PHY_OBJECT_RESPONSE:
200                 sumoRespClass = OBJECT_RESPONSE;
201                 break;
202         case PHY_STATIC_RESPONSE:
203                 sumoRespClass = PHY_STATIC_RESPONSE;
204                 break;
205         default:
206                 assert(0);
207                 return;
208         }
209
210         SumoPHYCallbackBridge* bridge = new SumoPHYCallbackBridge(user,callback);
211
212         m_sumoScene->addTouchCallback(sumoRespClass,SumoPHYCallbackBridge::StaticSolidToPHYCallback,bridge);
213 }
214 void SumoPhysicsEnvironment::requestCollisionCallback(PHY_IPhysicsController* ctrl)
215 {
216         SumoPhysicsController* smctrl = dynamic_cast<SumoPhysicsController*>(ctrl);
217         MT_assert(smctrl);
218         SM_Object* smObject = smctrl->GetSumoObject();
219         MT_assert(smObject);
220         if (smObject)
221         {
222                 //assert(smObject->getPhysicsClientObject() == ctrl);
223                 smObject->setPhysicsClientObject(ctrl);
224         
225                 m_sumoScene->requestCollisionCallback(*smObject);
226         }
227 }
228 PHY_IPhysicsController* SumoPhysicsEnvironment::CreateSphereController(float radius,const PHY__Vector3& position)
229 {
230         DT_ShapeHandle shape    =       DT_NewSphere(0.0);
231         SM_Object* ob = new SM_Object(shape,0,0,0);     
232         ob->setPosition(MT_Point3(position));
233         //testing
234         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));              
235         ob->setOrientation(rotquatje);
236
237         PHY_IPhysicsController* ctrl = new SumoPhysicsController(m_sumoScene,ob,0,false);
238         ctrl->SetMargin(radius);
239         return ctrl;
240 }
241 PHY_IPhysicsController* SumoPhysicsEnvironment::CreateConeController(float coneradius,float coneheight)
242 {
243         DT_ShapeHandle shape    =       DT_NewCone(coneradius,coneheight);
244         SM_Object* ob = new SM_Object(shape,0,0,0);     
245         ob->setPosition(MT_Point3(0.f,0.f,0.f));
246         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));              
247         ob->setOrientation(rotquatje);
248
249         PHY_IPhysicsController* ctrl = new SumoPhysicsController(m_sumoScene,ob,0,false);
250
251         return ctrl;
252 }
253