80e4dc4044e7cffcf136ada52605c85a2b874a1a
[blender.git] / source / gameengine / Physics / Sumo / SumoPhysicsEnvironment.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL 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.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29 #include <string.h>             // memset
30 #include "SumoPhysicsEnvironment.h"
31 #include "PHY_IMotionState.h"
32 #include "SumoPhysicsController.h"
33 #include "SM_Scene.h"
34 #include "SumoPHYCallbackBridge.h"
35 #include <SOLID/SOLID.h>
36
37 SumoPhysicsEnvironment::SumoPhysicsEnvironment()
38 {
39         m_fixedTimeStep = 1.f/60.f;
40         m_useFixedTimeStep = true;
41         m_currentTime = 0.f;
42
43         m_sumoScene = new SM_Scene();
44 }
45
46
47
48 SumoPhysicsEnvironment::~SumoPhysicsEnvironment()
49 {
50         delete m_sumoScene;
51 }
52
53
54
55 void SumoPhysicsEnvironment::beginFrame()
56 {
57         m_sumoScene->beginFrame();
58 }
59
60 void SumoPhysicsEnvironment::endFrame()
61 {
62         m_sumoScene->endFrame();
63 }
64
65 void            SumoPhysicsEnvironment::setFixedTimeStep(bool useFixedTimeStep,float fixedTimeStep)
66 {
67         m_useFixedTimeStep = useFixedTimeStep;
68         if (m_useFixedTimeStep)
69         {
70                 m_fixedTimeStep = fixedTimeStep;
71         } else
72         {
73                 m_fixedTimeStep  = 0.f;
74         }
75         //reset current time ?
76         m_currentTime = 0.f;
77 }
78 float           SumoPhysicsEnvironment::getFixedTimeStep()
79 {
80         return m_fixedTimeStep;
81 }
82
83
84 bool            SumoPhysicsEnvironment::proceedDeltaTime(double  curTime,float timeStep)
85 {
86         
87         bool result = false;
88         if (m_useFixedTimeStep)
89         {
90                 m_currentTime += timeStep;
91                 float ticrate = 1.f/m_fixedTimeStep;
92
93                 result = m_sumoScene->proceed(curTime, ticrate);
94         } else
95         {
96                 m_currentTime += timeStep;
97                 result = m_sumoScene->proceed(m_currentTime, timeStep);
98         }
99         return result;
100 }
101
102 void SumoPhysicsEnvironment::setGravity(float x,float y,float z)
103 {
104         m_sumoScene->setForceField(MT_Vector3(x,y,z));
105 }
106
107 int SumoPhysicsEnvironment::createConstraint(
108         class PHY_IPhysicsController* ctrl,
109         class PHY_IPhysicsController* ctrl2,
110         PHY_ConstraintType type,
111         float pivotX,float pivotY,float pivotZ,
112         float axisX,float axisY,float axisZ,
113         float axis1X,float axis1Y,float axis1Z,
114         float axis2X,float axis2Y,float axis2Z
115
116         )
117 {
118         int constraintid = 0;
119         return constraintid;
120 }
121
122 void SumoPhysicsEnvironment::removeConstraint(int       constraintid)
123 {
124         if (constraintid)
125         {
126         }
127 }
128
129 PHY_IPhysicsController* SumoPhysicsEnvironment::rayTest(PHY_IRayCastFilterCallback &filterCallback, 
130         float fromX,float fromY,float fromZ, 
131         float toX,float toY,float toZ)
132 {
133         SumoPhysicsController* ignoreCtr = static_cast<SumoPhysicsController*> (filterCallback.m_ignoreController);
134
135         //collision detection / raytesting
136         MT_Point3 hit, normal;
137         PHY_RayCastResult result;
138
139         SM_Object* sm_ignore = 0;
140         if (ignoreCtr)
141                 sm_ignore = ignoreCtr->GetSumoObject();
142
143         memset(&result, 0, sizeof(result));
144
145         SM_Object* smOb = m_sumoScene->rayTest(sm_ignore,MT_Point3(fromX, fromY, fromZ),MT_Point3(toX, toY, toZ), hit, normal);
146         if (smOb)
147         {
148                 result.m_controller = (PHY_IPhysicsController *) smOb->getPhysicsClientObject();
149                 result.m_hitPoint[0] = hit[0];
150                 result.m_hitPoint[1] = hit[1];
151                 result.m_hitPoint[2] = hit[2];
152                 result.m_hitNormal[0] = normal[0];
153                 result.m_hitNormal[1] = normal[1];
154                 result.m_hitNormal[2] = normal[2];
155                 filterCallback.reportHit(&result);
156         }
157         return result.m_controller;
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         case PHY_BROADPH_RESPONSE:
206                 return;
207         default:
208                 assert(0);
209                 return;
210         }
211
212         SumoPHYCallbackBridge* bridge = new SumoPHYCallbackBridge(user,callback);
213
214         m_sumoScene->addTouchCallback(sumoRespClass,SumoPHYCallbackBridge::StaticSolidToPHYCallback,bridge);
215 }
216 void SumoPhysicsEnvironment::requestCollisionCallback(PHY_IPhysicsController* ctrl)
217 {
218         SumoPhysicsController* smctrl = dynamic_cast<SumoPhysicsController*>(ctrl);
219         MT_assert(smctrl);
220         SM_Object* smObject = smctrl->GetSumoObject();
221         MT_assert(smObject);
222         if (smObject)
223         {
224                 //assert(smObject->getPhysicsClientObject() == ctrl);
225                 smObject->setPhysicsClientObject(ctrl);
226         
227                 m_sumoScene->requestCollisionCallback(*smObject);
228         }
229 }
230
231 void SumoPhysicsEnvironment::removeCollisionCallback(PHY_IPhysicsController* ctrl)
232 {
233         // intentionally empty
234 }
235
236 PHY_IPhysicsController* SumoPhysicsEnvironment::CreateSphereController(float radius,const PHY__Vector3& position)
237 {
238         DT_ShapeHandle shape    =       DT_NewSphere(0.0);
239         SM_Object* ob = new SM_Object(shape,0,0,0);     
240         ob->setPosition(MT_Point3(position));
241         //testing
242         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));              
243         ob->setOrientation(rotquatje);
244
245         PHY_IPhysicsController* ctrl = new SumoPhysicsController(m_sumoScene,ob,0,false);
246         ctrl->SetMargin(radius);
247         return ctrl;
248 }
249 PHY_IPhysicsController* SumoPhysicsEnvironment::CreateConeController(float coneradius,float coneheight)
250 {
251         DT_ShapeHandle shape    =       DT_NewCone(coneradius,coneheight);
252         SM_Object* ob = new SM_Object(shape,0,0,0);     
253         ob->setPosition(MT_Point3(0.f,0.f,0.f));
254         MT_Quaternion rotquatje(MT_Vector3(0,0,1),MT_radians(90));              
255         ob->setOrientation(rotquatje);
256
257         PHY_IPhysicsController* ctrl = new SumoPhysicsController(m_sumoScene,ob,0,false);
258
259         return ctrl;
260 }
261