Project

General

Profile

SO3Engine
OgreNewt_World.cpp
Go to the documentation of this file.
7
8#pragma warning(disable:4355)
9
10namespace OgreNewt
11{
12 WorldTriggerManager::WorldTriggerManager(World* const world): CustomTriggerManager(world->getNewtonWorld())
13 {
14 }
15
16
21
26
28 {
29 m_ExitCallback = callback;
30 }
31
32
33// Constructor
34World::World(Ogre::Real desiredFps, int maxUpdatesPerFrames, Ogre::String name) :
35 m_bodyInAABBIterator(this)
36{
37 setUpdateFPS(desiredFps, maxUpdatesPerFrames);
38 m_world = NewtonCreate();
39 NewtonWorldSetUserData(m_world, this);
40 //NewtonSelectBroadphaseAlgorithm (m_world, 0);
41
42 // create the default ID.
43 m_defaultMatID = new OgreNewt::MaterialID( this, NewtonMaterialGetDefaultGroupID( m_world ) );
44 m_defaultAngularDamping = Ogre::Vector3(0.1f, 0.1f, 0.1f);
46 m_debugger = new Debugger(this);
48
49 // set the default solve mode to be iterative the fastest
51
52 // use the more advanced hardware in the system
53 //Ogre::String description;
54 //setPlatformArchitecture(0);
55 //getPlatformArchitecture(description);
56
57 //NewtonSetMultiThreadSolverOnSingleIsland(m_world, 1);
58}
59
60// Destructor
62{
63 if (m_debugger)
64 {
65 delete m_debugger;
66 m_debugger = NULL;
67 }
68
70 {
71 delete m_defaultMatID;
72 m_defaultMatID = NULL;
73 }
74
75 if (m_world)
76 {
77 NewtonDestroy( m_world );
78 m_world = NULL;
79 }
80}
81
82CustomTriggerController* World::CreateController(const dMatrix& matrix, NewtonCollision* const convexShape)
83{
85 return m_triggerManager->CreateController(matrix, convexShape);
86
87 return 0;
88}
89
90void World::DestroyController(CustomTriggerController* const controller)
91{
93}
94
95void World::setPreListener(std::string name, void* const listenerUserData, NewtonWorldUpdateListenerCallback update)
96{
97 NewtonWorldAddPreListener(m_world, name.c_str(), listenerUserData, update, NULL);
98}
99
100void World::setpostListener(std::string name, void* const listenerUserData, NewtonWorldUpdateListenerCallback update)
101{
102 NewtonWorldAddPostListener(m_world, name.c_str(), listenerUserData, update, NULL);
103}
104
105void World::setUpdateFPS(Ogre::Real desiredFps, int maxUpdatesPerFrames)
106{
107 if (maxUpdatesPerFrames < 1) {
108 maxUpdatesPerFrames = 1;
109 }
110
111 if (desiredFps < 120.0f)
112 desiredFps = 120.0f;
113 else if (desiredFps > 600.0f)
114 desiredFps = 600.0f;
115
116 m_maxTicksPerFrames = maxUpdatesPerFrames;
117 m_updateFPS = desiredFps;
118 m_timestep = 1.0f / desiredFps;
119 m_invTimestep = desiredFps;
120 m_timeAcumulator = 0.0f;
121}
122
123// Set allocators
124void World::setAllocators (NewtonAllocMemory newtonAlloc, NewtonFreeMemory newtonFree)
125{
126 NewtonSetMemorySystem (newtonAlloc, newtonFree);
127}
128
129void World::setThreadCount(int threads)
130{
131 NewtonSetThreadsCount(m_world, threads);
132}
133
134// update
135int World::update( Ogre::Real t_step )
136{
137 int realUpdates = 0;
138 bool sync = true;
139 float ntimeStep = t_step;
140
141 // clamp the step if necessary
142 ntimeStep = std::min(ntimeStep, m_timestep * m_maxTicksPerFrames);
143
144 // advance the accumulator;
145 m_timeAcumulator += ntimeStep;
146
147 while (m_timeAcumulator >= ntimeStep)
148 {
149 if (sync)
150 NewtonUpdate(m_world, m_timestep);
151 else
152 NewtonUpdateAsync(m_world, m_timestep);
153
155 realUpdates++;
156 }
157
158 /*
159 if (m_timeAcumulator >= ntimeStep)
160 {
161 while (m_timeAcumulator >= ntimeStep)
162 {
163 if (sync)
164 NewtonUpdate(m_world, ntimeStep);
165 else
166 NewtonUpdateAsync(m_world, ntimeStep);
167 m_timeAcumulator -= ntimeStep;
168 realUpdates++;
169 }
170 }
171 else
172 {
173 if (sync)
174 NewtonUpdate(m_world, t_step);
175 else
176 NewtonUpdateAsync(m_world, t_step);
177
178 m_timeAcumulator -= ntimeStep;
179 }*/
180
181 //Ogre::Real param = m_timeAcumulator * m_invTimestep;
182
183 for(Body* body = getFirstBody(); body; body = body->getNext())
184 {
185 body->updateNode(1.0f); // param : issue on vehicle wheels :/
186 }
187
188 return realUpdates;
189}
190
192{
193 NewtonBody* body = NewtonWorldGetFirstBody( m_world );
194 if( body )
195 return (Body*) NewtonBodyGetUserData(body);
196
197 return NULL;
198}
199
201{
202 // add +3 to get the previous behavior < newton 3.13
203 if (model <= 0)
204 model = 0;
205 //else if (model > 1)
206 model = model + 3;
207
208 NewtonSetSolverModel(m_world, model);
209 NewtonSetFrictionModel(m_world, model);
210}
211
212
213// -------------------------------------------------------------------------------------------------------------
214// -------------------------------------------------------------------------------------------------------------
215// -------------------------------------------------------------------------------------------------------------
216// -------------------------------------------------------------------------------------------------------------
217
218
219}
220
main class for all Rigid Bodies in the system.
Body * getNext() const
use this function to iterate through all bodies
For viewing the Newton rigid bodies visually.
represents a material
represents a physics world.
World(Ogre::Real desiredFps=100.0f, int maxUpdatesPerFrames=5, Ogre::String name="main")
Constructor, creates the world and gives it a name.
void setPreListener(std::string name, void *const listenerUserData, NewtonWorldUpdateListenerCallback update)
void DestroyController(CustomTriggerController *const controller)
WorldTriggerManager * m_triggerManager
NewtonWorld * m_world
static void setAllocators(NewtonAllocMemory newtonAlloc, NewtonFreeMemory newtonFree)
Set the newton world allocators,.
MaterialID * m_defaultMatID
void setpostListener(std::string name, void *const listenerUserData, NewtonWorldUpdateListenerCallback update)
Ogre::Vector3 m_defaultAngularDamping
int update(Ogre::Real t_step)
update the world by the specified time_step.
void setThreadCount(int threads)
set the number of threads for the physics simulation to use, don't do this while world update
void setSolverModel(int model)
set the physics solver model
Ogre::Real m_invTimestep
Ogre::Real m_timestep
CustomTriggerController * CreateController(const dMatrix &matrix, NewtonCollision *const convexShape)
Ogre::Real m_timeAcumulator
void setUpdateFPS(Ogre::Real desiredFps, int maxUpdatesPerFrames)
set simulation frames per and max updated per updates
Body * getFirstBody() const
to iterate through all bodies call this function and then use body->getNext()
Ogre::Real m_updateFPS
Debugger * m_debugger
~World()
Standard Destructor, destroys the world.
Ogre::Real m_defaultLinearDamping
void setTriggerInsideCallback(WorldTriggerInsideCallback callback)
void setTriggerExitCallback(WorldTriggerExitCallback callback)
void setTriggerEnterCallback(WorldTriggerEnterCallback callback)
CustomTriggerController * CreateController(const dMatrix &matrix, NewtonCollision *const convexShape)
void DestroyController(CustomTriggerController *const controller)
WorldTriggerManager(World *const world)
std::function< void(NewtonBody *const visitor)> WorldTriggerInsideCallback
std::function< void(NewtonBody *const visitor)> WorldTriggerEnterCallback
std::function< void(NewtonBody *const visitor)> WorldTriggerExitCallback