SO3Engine
OgreNewt_World.h
Go to the documentation of this file.
1/*
2 OgreNewt Library
3
4 Ogre implementation of Newton Game Dynamics SDK
5
6 OgreNewt basically has no license, you may use any or all of the library however you desire... I hope it can help you in any way.
7
8 by Walaber
9 some changes by melven
10*/
11#ifndef _INCLUDE_OGRENEWT_WORLD
12#define _INCLUDE_OGRENEWT_WORLD
13
16#include "OgreNewt_Debugger.h"
17#include "CustomTriggerManager.h"
18
19#include <map>
20
22
25namespace OgreNewt
26{
27
28class World;
29class _OgreNewtExport WorldTriggerManager: public CustomTriggerManager
30{
31 public:
32 typedef std::function<void(NewtonBody* const visitor)> WorldTriggerEnterCallback;
33 typedef std::function<void(NewtonBody* const visitor)> WorldTriggerInsideCallback;
34 typedef std::function<void(NewtonBody* const visitor)> WorldTriggerExitCallback;
35
37 {
38 public:
39 TriggerCallback(CustomTriggerController* const controller) :m_controller(controller)
40 , m_EnterCallback(0)
41 , m_InsideCallback(0)
42 , m_ExitCallback(0)
43 {
44 }
45
47 {
48 }
49
50 virtual void OnEnter(NewtonBody* const visitor)
51 {
52 if (m_EnterCallback)
53 m_EnterCallback(visitor);
54 }
55
56 virtual void OnInside(NewtonBody* const visitor)
57 {
58 if (m_InsideCallback)
59 m_InsideCallback(visitor);
60 }
61
62 virtual void OnExit(NewtonBody* const visitor)
63 {
64 if (m_ExitCallback)
65 m_ExitCallback(visitor);
66 }
67
68 void setTriggerEnterCallback(WorldTriggerEnterCallback callback);
69 template<class c> void setTriggerEnterCallback( std::function<void(c*, NewtonBody* const)> callback, c *instancedClassPointer )
70 {
71 setTriggerEnterCallback(std::bind(callback, instancedClassPointer, std::placeholders::_1));
72 }
73
74 void setTriggerInsideCallback(WorldTriggerInsideCallback callback);
75 template<class c> void setTriggerInsideCallback( std::function<void(c*, NewtonBody* const)> callback, c *instancedClassPointer )
76 {
77 setTriggerInsideCallback(std::bind(callback, instancedClassPointer, std::placeholders::_1));
78 }
79
80 void setTriggerExitCallback(WorldTriggerExitCallback callback);
81 template<class c> void setTriggerExitCallback( std::function<void(c*, NewtonBody* const)> callback, c *instancedClassPointer )
82 {
83 setTriggerExitCallback(std::bind(callback, instancedClassPointer, std::placeholders::_1));
84 }
85
86 CustomTriggerController* m_controller;
87
88 private:
89 WorldTriggerEnterCallback m_EnterCallback;
90 WorldTriggerInsideCallback m_InsideCallback;
91 WorldTriggerExitCallback m_ExitCallback;
92 };
93
94 WorldTriggerManager(World* const world);
95
96 CustomTriggerController* CreateController(const dMatrix& matrix, NewtonCollision* const convexShape)
97 {
98 CustomTriggerController* const controller = CreateTrigger (matrix, convexShape, NULL);
99 TriggerCallback* const forceCallback = new TriggerCallback (controller);
100 controller->SetUserData(forceCallback);
101 return controller;
102 }
103
104 void DestroyController(CustomTriggerController* const controller)
105 {
106 TriggerCallback* const userData = (TriggerCallback*) controller->GetUserData();
107 delete userData;
108 CustomTriggerManager::DestroyController(controller);
109 }
110
111 virtual void EventCallback(const CustomTriggerController* const me, TriggerEventType event, NewtonBody* const visitor) const
112 {
113 TriggerCallback* const callback = (TriggerCallback*) me->GetUserData();
114 switch (event)
115 {
116 case m_enterTrigger:
117 {
118 callback->OnEnter(visitor);
119 break;
120 }
121
122 case m_exitTrigger:
123 {
124 callback->OnExit(visitor);
125 break;
126 }
127
128 case m_inTrigger:
129 {
130 callback->OnInside(visitor);
131 break;
132 }
133 }
134 }
135};
136
138
141class _OgreNewtExport World
142{
143
144public:
145
147
151 {
152 SM_EXACT = 0,
153 SM_FASTEST = 1,
154 SM_MEDIUM = 4,
155 SM_SLOW = 8,
156 SM_RIDICULUS = 16
157 };
158
160
164 {
165 FM_EXACT = 0,
166 FM_ADAPTIVE = 1
167 };
168
170
180 {
181 PF_DETERMINISTIC_UNOPTIMIZED = 0,
182 PF_COMMON_OPTIMIZED = 1,
183 PF_BEST_POSSIBLE = 2
184 };
185
187
193 typedef std::function<void(OgreNewt::Body*, int threadIndex)> LeaveWorldCallback;
194
195public:
197
200 World(Ogre::Real desiredFps = 100.0f, int maxUpdatesPerFrames = 5, Ogre::String name = "main");
201
203 ~World();
204
205 CustomTriggerController* CreateController(const dMatrix& matrix, NewtonCollision* const convexShape);
206 void DestroyController(CustomTriggerController* const controller);
207
208 void setPreListener(std::string name, void* const listenerUserData, NewtonWorldUpdateListenerCallback update);
209 void setpostListener(std::string name, void* const listenerUserData, NewtonWorldUpdateListenerCallback update);
210
212
216 static void setAllocators (NewtonAllocMemory newtonAlloc, NewtonFreeMemory newtonFree);
217
219
225 int update( Ogre::Real t_step );
226
228
232 void invalidateCache() { NewtonInvalidateCache( m_world ); }
233
235
239 void setUpdateFPS(Ogre::Real desiredFps, int maxUpdatesPerFrames);
240
242
245 Ogre::Real getUpdateFPS() const { return m_updateFPS; }
246
248
255 void setDefaultLinearDamping(Ogre::Real defaultLinearDamping) { m_defaultLinearDamping = defaultLinearDamping; }
256
258
261 Ogre::Real getDefaultLinearDamping() const { return m_defaultLinearDamping; }
262
264
271 void setDefaultAngularDamping(Ogre::Vector3 defaultAngularDamping) { m_defaultAngularDamping = defaultAngularDamping; }
272
274
277 Ogre::Vector3 getDefaultAngularDamping() const { return m_defaultAngularDamping; }
278
280
284 NewtonWorld* getNewtonWorld() const { return m_world; }
285
287
291 const MaterialID* getDefaultMaterialID() const { return m_defaultMatID; } // get pointer to default material ID object.
292
294
297 void destroyAllBodies() { NewtonDestroyAllBodies( m_world ); }
298
300
305 void setSolverModel(int model);
306
308
313 void setFrictionModel( int model) { NewtonSetFrictionModel( m_world, model ); }
314
316
320 void setPlatformArchitecture( int mode )
321 {
322 NewtonSetCurrentDevice (m_world, mode); //int platformsCount = NewtonEnumrateDevices (m_world);
323 //char platform[256];
324 //NewtonGetDeviceString (m_world, mode, platform, sizeof (platform));
325 }
326
328
332 int getPlatformArchitecture(Ogre::String& description) { char desc[265]; int mode = NewtonGetCurrentDevice(m_world); NewtonGetDeviceString(m_world, mode, desc, sizeof(desc)); description = desc; return mode;}
333
335
338 int getBodyCount() const { return NewtonWorldGetBodyCount( m_world ); }
339
341 int getConstraintCount() const {return NewtonWorldGetConstraintCount( m_world ); }
342
344 void setMultithreadSolverOnSingleIsland( int mode ) { NewtonSetMultiThreadSolverOnSingleIsland( m_world, mode ); }
345
347 int getMultithreadSolverOnSingleIsland( ) const { return NewtonGetMultiThreadSolverOnSingleIsland( m_world ); }
348
350 void setThreadCount(int threads);
351
353 int getThreadCount() const { return NewtonGetThreadsCount( m_world ); }
354
356 void criticalSectionLock() const { NewtonWorldCriticalSectionLock( m_world, 0); }
357
359 void criticalSectionUnlock() const { NewtonWorldCriticalSectionUnlock( m_world ); }
360
361 void waitForUpdateToFinish() const { NewtonWaitForUpdateToFinish(m_world); }
362
364 void setMinimumFrameRate( Ogre::Real frame ) { NewtonSetMinimumFrameRate( m_world, frame ); }
365
367 // Newton >= 2.30 remove m_world
368 int getVersion() const { return NewtonWorldGetVersion(); }
369
371 Body* getFirstBody() const;
372
373 /*
375 MaterialGroupID* getFirstMaterialGroupID();
376 */
377
379 const BodyInAABBIterator& getBodyInAABBIterator() const {return m_bodyInAABBIterator;}
380
382
385 Debugger& getDebugger() const {return *m_debugger;}
386
387 /*
389 void addBodyUpdateNodeRequest( int threadIndex, OgreNewt::Body* body ) const ;
390 */
391
392protected:
393
395 Ogre::Real m_timestep;
396 Ogre::Real m_invTimestep;
398 Ogre::Real m_updateFPS;
399
402
403 NewtonWorld* m_world;
405
407
408 typedef std::vector< OgreNewt::Body* > BodyVector;
409 typedef std::vector< BodyVector > BodyVectorVector;
410//mutable BodyVectorVector m_bodyUpdateNodeRequests;
413
414};
415
416}
417
418
419#endif
420// _INCLUDE_OGRENEWT_WORLD
421
422
SCOL_EXPORT void SCOL_PTR_TYPE event
Definition SO3SCOL.cpp:5128
main class for all Rigid Bodies in the system.
Iterate through all bodies in a specific AABB in the world.
For viewing the Newton rigid bodies visually.
represents a material
represents a physics world.
PlatformArchitecture
platform mode
void setPlatformArchitecture(int mode)
specify a specific architecture to use for physics calculations.
void setDefaultAngularDamping(Ogre::Vector3 defaultAngularDamping)
sets default angular damping
WorldTriggerManager * m_triggerManager
void setDefaultLinearDamping(Ogre::Real defaultLinearDamping)
sets default linear damping
FrictionModelMode
friction solver mode.
NewtonWorld * m_world
void criticalSectionUnlock() const
notify the exit of a critical section of code.
const MaterialID * getDefaultMaterialID() const
get the default materialID object.
MaterialID * m_defaultMatID
void setFrictionModel(int model)
set the physics friction model
Ogre::Vector3 m_defaultAngularDamping
void setMultithreadSolverOnSingleIsland(int mode)
multithread settings
Ogre::Real getUpdateFPS() const
returns desired update frames per second
BodyInAABBIterator m_bodyInAABBIterator
std::vector< BodyVector > BodyVectorVector
int getBodyCount() const
get the number of bodies in the simulation.
Debugger & getDebugger() const
get the debugger for this world
std::vector< OgreNewt::Body * > BodyVector
int getThreadCount() const
get the number of threads the simulation is using.
NewtonWorld * getNewtonWorld() const
retrieves a pointer to the NewtonWorld
SolverModelMode
physics solver mode.
Ogre::Real m_invTimestep
Ogre::Real m_timestep
void criticalSectionLock() const
notify an entrance to a critical section of code.
Ogre::Real m_timeAcumulator
std::function< void(OgreNewt::Body *, int threadIndex)> LeaveWorldCallback
leave world callback.
void destroyAllBodies()
remove all bodies from the world.
int getVersion() const
get the Newton SDK version.
Ogre::Vector3 getDefaultAngularDamping() const
return default angular damping
Ogre::Real getDefaultLinearDamping() const
return default linear damping
void setMinimumFrameRate(Ogre::Real frame)
set minimum framerate
int getMultithreadSolverOnSingleIsland() const
get multithread settings
int getConstraintCount() const
get the number of constaints in the simulation
void waitForUpdateToFinish() const
Ogre::Real m_updateFPS
Debugger * m_debugger
int getPlatformArchitecture(Ogre::String &description)
get the architecture used for physics calculations.
void invalidateCache()
invalidate internal cache
Ogre::Real m_defaultLinearDamping
const BodyInAABBIterator & getBodyInAABBIterator() const
get a bodyInAABBIterator for this world
virtual void OnEnter(NewtonBody *const visitor)
void setTriggerEnterCallback(std::function< void(c *, NewtonBody *const)> callback, c *instancedClassPointer)
virtual void OnExit(NewtonBody *const visitor)
void setTriggerExitCallback(std::function< void(c *, NewtonBody *const)> callback, c *instancedClassPointer)
void setTriggerInsideCallback(std::function< void(c *, NewtonBody *const)> callback, c *instancedClassPointer)
TriggerCallback(CustomTriggerController *const controller)
virtual void OnInside(NewtonBody *const visitor)
CustomTriggerController * CreateController(const dMatrix &matrix, NewtonCollision *const convexShape)
void DestroyController(CustomTriggerController *const controller)
std::function< void(NewtonBody *const visitor)> WorldTriggerInsideCallback
std::function< void(NewtonBody *const visitor)> WorldTriggerEnterCallback
std::function< void(NewtonBody *const visitor)> WorldTriggerExitCallback
virtual void EventCallback(const CustomTriggerController *const me, TriggerEventType event, NewtonBody *const visitor) const