Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/ogreode/src/OgreOdeWorld.cpp @ 62

Last change on this file since 62 was 21, checked in by nicolasc, 16 years ago

added ogreode and Colladaplugin

File size: 9.2 KB
Line 
1
2#include "OgreOdePrecompiledHeaders.h"
3
4#include "OgreOdeWorld.h"
5#include "OgreOdeBody.h"
6#include "OgreOdeJoint.h"
7#include "OgreOdeGeometry.h"
8#include "OgreOdeSpace.h"
9#include "OgreOdeDebugObject.h"
10
11using namespace OgreOde;
12using namespace Ogre;
13
14CollisionListener* World::_collision_listener = 0;
15//------------------------------------------------------------------------------------------------
16void World::setCollisionListener(CollisionListener* collision_listener)
17{
18        _collision_listener = collision_listener;
19}
20//------------------------------------------------------------------------------------------------
21CollisionListener* World::getCollisionListener()
22{
23        return _collision_listener;
24}
25//------------------------------------------------------------------------------------------------
26World::World(SceneManager* manager) :
27    _show_debug_geoms (false),
28    _show_debug_contact (false),
29    _manager (manager)
30{
31        _world = dWorldCreate();
32        _contacts = dJointGroupCreate(0);
33
34        _default_space = new HashTableSpace(this);
35        _default_space->setAutoCleanup(false);
36        _default_space->setInternalCollisions(true);
37
38        setDamping(0.0,0.0);
39        setHistorySize (1);
40}
41//------------------------------------------------------------------------------------------------
42void World::setHistorySize(size_t historySize)
43{
44        _history_size = historySize;
45
46        MaintainedItemIterator<Body> it = _body_list.getIterator();
47        while(!it.end ())
48        {
49                Body * const b = (Body *) (it.getNext ());
50                b->_historyResize(historySize);
51        }
52}
53//------------------------------------------------------------------------------------------------
54size_t World::getHistorySize() const 
55{
56        return _history_size;
57}
58//------------------------------------------------------------------------------------------------
59void World::setGravity(const Ogre::Vector3& gravity)
60{
61        dWorldSetGravity(_world,(dReal)gravity.x,(dReal)gravity.y,(dReal)gravity.z);
62}
63//------------------------------------------------------------------------------------------------
64const Ogre::Vector3& World::getGravity()
65{
66        dVector3 g;
67        dWorldGetGravity(_world,g);
68        _gravity.x = (Real)g[0];
69        _gravity.y = (Real)g[1];
70        _gravity.z = (Real)g[2];
71        return _gravity;
72}
73//------------------------------------------------------------------------------------------------
74void World::setERP(Real erp)
75{
76        dWorldSetERP(_world,(dReal)erp);
77}
78//------------------------------------------------------------------------------------------------
79Real World::getERP()
80{
81        return (Real)dWorldGetERP(_world);
82}
83//------------------------------------------------------------------------------------------------
84void World::setCFM(Real cfm)
85{
86        dWorldSetCFM(_world,(dReal)cfm);
87}
88//------------------------------------------------------------------------------------------------
89Real World::getCFM()
90{
91        return (Real)dWorldGetCFM(_world);
92}
93//------------------------------------------------------------------------------------------------
94void World::setAutoSleep(bool auto_sleep)
95{
96        dWorldSetAutoDisableFlag(_world,(auto_sleep)?1:0);
97}
98//------------------------------------------------------------------------------------------------
99bool World::getAutoSleep()
100{
101        return (dWorldGetAutoDisableFlag(_world))?true:false;
102}
103//------------------------------------------------------------------------------------------------
104void World::setAutoSleepLinearThreshold(Real linear_threshold)
105{
106        dWorldSetAutoDisableLinearThreshold(_world,(dReal)linear_threshold);
107}
108//------------------------------------------------------------------------------------------------
109Real World::getAutoSleepLinearThreshold()
110{
111        return (Real)dWorldGetAutoDisableLinearThreshold(_world);
112}
113//------------------------------------------------------------------------------------------------
114void World::setAutoSleepAngularThreshold(Real angular_threshold)
115{
116        dWorldSetAutoDisableAngularThreshold(_world,(dReal)angular_threshold);
117}
118//------------------------------------------------------------------------------------------------
119Real World::getAutoSleepAngularThreshold()
120{
121        return (Real)dWorldGetAutoDisableAngularThreshold(_world);
122}
123//------------------------------------------------------------------------------------------------
124void World::setAutoSleepSteps(int steps)
125{
126        dWorldSetAutoDisableSteps(_world,steps);
127}
128//------------------------------------------------------------------------------------------------
129int World::getAutoSleepSteps()
130{
131        return dWorldGetAutoDisableSteps(_world);
132}
133//------------------------------------------------------------------------------------------------
134void World::setAutoSleepTime(Real time)
135{
136        dWorldSetAutoDisableTime(_world,(dReal)time);
137}
138//------------------------------------------------------------------------------------------------
139Real World::getAutoSleepTime()
140{
141        return (Real)dWorldGetAutoDisableTime(_world);
142}
143//------------------------------------------------------------------------------------------------
144void World::setAutoSleepAverageSamplesCount(size_t time)
145{
146    dWorldSetAutoDisableAverageSamplesCount(_world, time);
147}
148//------------------------------------------------------------------------------------------------
149size_t World::getAutoSleepAverageSamplesCount()
150{
151    return (Real)dWorldGetAutoDisableAverageSamplesCount(_world);
152}
153//------------------------------------------------------------------------------------------------
154void World::setContactCorrectionVelocity(Real velocity)
155{
156        dWorldSetContactMaxCorrectingVel(_world,(dReal)velocity);
157}
158//------------------------------------------------------------------------------------------------
159void World::setContactSurfaceLayer(Real layer)
160{
161        dWorldSetContactSurfaceLayer(_world,(dReal)layer);
162}
163
164//------------------------------------------------------------------------------------------------
165void World::collisionCallback(void *data,dGeomID geom_a,dGeomID geom_b)
166{
167        const bool a_space = (dGeomIsSpace(geom_a))?true:false;
168        const bool b_space = (dGeomIsSpace(geom_b))?true:false;
169       
170        void* const ptr_a = dGeomGetData(geom_a);
171        void* const ptr_b = dGeomGetData(geom_b);
172
173        if(a_space  || b_space )
174        {
175                // Collide a space with a space
176                if(a_space && b_space) 
177                        static_cast<Space*>(ptr_a)->collide(static_cast<Space*>(ptr_b),data);
178                else if(a_space) 
179                        static_cast<Space*>(ptr_a)->collide(static_cast<Geometry*>(ptr_b),data);
180                else 
181                        static_cast<Space*>(ptr_b)->collide(static_cast<Geometry*>(ptr_a),data);
182
183                // Collide geometries internal to the spaces
184                if(a_space) 
185            static_cast<Space*>(ptr_a)->collide(data);
186
187                if(b_space) 
188            static_cast<Space*>(ptr_b)->collide(data);
189        }
190    else
191        {
192                // Collide a geom with a geom, i.e. generate contacts
193                static_cast<Geometry*>(ptr_a)->collide(static_cast<Geometry*>(ptr_b),_collision_listener);
194        }
195}
196//------------------------------------------------------------------------------------------------
197Body* World::findBody(SceneNode* node)
198{
199        Body* body = 0;
200        for(int i = 0;i < node->numAttachedObjects();i++)
201        {
202                MovableObject* obj = node->getAttachedObject(i);
203                if(obj)
204                {
205                        if(obj->getMovableType() == Ogre::String("OgreOde::Body"))
206                        {
207                                body = static_cast<Body*>(obj);
208                                break;
209                        }
210                }
211        }
212        return body;
213}
214//------------------------------------------------------------------------------------------------
215Body* World::findBody(const Ogre::String& name)
216{
217        Body *b = 0;
218        MaintainedItemIterator<Body> it = _body_list.getIterator();
219        while(!it.end ())
220        {
221                b = (Body*) (it.getNext ());
222                if(b->getName() == name) 
223                        return b;
224        }
225        return 0;
226}
227//------------------------------------------------------------------------------------------------
228void World::setDefaultSpace(Space* space)
229{
230        delete _default_space;
231        _default_space = space;
232}
233//------------------------------------------------------------------------------------------------
234void World::setQuickStepIterations(int iterations)
235{
236        dWorldSetQuickStepNumIterations(_world,iterations);
237}
238//------------------------------------------------------------------------------------------------
239int World::getQuickStepIterations()
240{
241        return dWorldGetQuickStepNumIterations(_world);
242}
243//------------------------------------------------------------------------------------------------
244void World::setShowDebugGeometries(bool show)
245{
246        _body_list.setDebug(show);
247        _geometry_list.setDebug(show);
248        _show_debug_geoms = show;
249}
250//------------------------------------------------------------------------------------------------
251void World::setShowDebugContact(bool show)
252{
253    _geometry_list.setDebugContact(show);
254    _show_debug_contact = show;
255}
256//------------------------------------------------------------------------------------------------
257void World::setDamping(Real linear_damping,Real angular_damping)
258{
259        _linear_damping = -(dReal)linear_damping;
260        _angular_damping = -(dReal)angular_damping;
261}
262//------------------------------------------------------------------------------------------------
263Real World::getLinearDamping()
264{
265        return -(Real)_linear_damping;
266}
267//------------------------------------------------------------------------------------------------
268Real World::getAngularDamping()
269{
270        return -(Real)_angular_damping;
271}
272//------------------------------------------------------------------------------------------------
273World::~World()
274{
275        delete _default_space;
276        dJointGroupDestroy(_contacts);
277    dWorldDestroy(_world);
278    dCloseODE();
279}
Note: See TracBrowser for help on using the repository browser.