Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics_new/src/ogreode/OgreOdeStepper.h @ 2119

Last change on this file since 2119 was 2119, checked in by rgrieder, 15 years ago

Merged physics branch into physics_new branch.

  • Property svn:eol-style set to native
File size: 6.1 KB
Line 
1#ifndef _OGREODESTEPPER_H_
2#define _OGREODESTEPPER_H_
3
4#include "OgreOdePreReqs.h"
5
6#include <OgreFrameListener.h>
7#include "OgreOdeWorld.h"
8
9namespace OgreOde
10{
11    //------------------------------------------------------------------------------------------------
12    class _OgreOdeExport StepMode
13    {
14    public:
15        StepMode(World *world) : _world(world){};
16        virtual ~StepMode(){};
17
18        virtual void step(const Ogre::Real time) = 0;
19
20    protected:
21        World* _world;
22    };
23    //------------------------------------------------------------------------------------------------
24    class _OgreOdeExport BasicStepMode : public StepMode
25    {
26    public:
27        BasicStepMode(World *world) : StepMode(world) {};
28        virtual ~BasicStepMode(){};
29
30        inline void step(const Ogre::Real time){ dWorldStep(_world->getWorldID(), (dReal)time);};
31    };
32    //------------------------------------------------------------------------------------------------
33    class _OgreOdeExport QuickStepMode : public StepMode
34    {
35    public:
36        QuickStepMode(World *world) : StepMode(world) {};
37        virtual ~QuickStepMode(){};
38
39        inline void step(const Ogre::Real time){ dWorldQuickStep(_world->getWorldID(), (dReal)time);};
40    };
41    //------------------------------------------------------------------------------------------------
42    class _OgreOdeExport FastStepMode : public StepMode
43    {
44    public:
45        FastStepMode(World *world, int max_iteration = 40) : StepMode(world), _max_iteration(max_iteration) {};
46        virtual ~FastStepMode(){};
47
48        inline void step(const Ogre::Real time){ dWorldStepFast1(_world->getWorldID(), (dReal)time, _max_iteration);};
49    private:
50        int _max_iteration;
51    };
52    //------------------------------------------------------------------------------------------------
53        class _OgreOdeExport StepListener
54        {
55        public:
56                StepListener(){};
57                virtual ~StepListener(){};
58
59        virtual bool preStep(Ogre::Real time) { return true; };
60        virtual bool postStep(Ogre::Real time)  { return true; };
61        virtual bool middleStep(Ogre::Real time)  { return true; };
62    };
63    //------------------------------------------------------------------------------------------------
64        class _OgreOdeExport StepHandler:public Ogre::FrameListener
65    {
66        public:
67                enum AutoMode
68                {
69                        AutoMode_NotAutomatic,
70                        AutoMode_PreFrame,
71                        AutoMode_PostFrame
72        };
73        enum StepModeType
74        {
75            BasicStep = 0,
76            QuickStep,
77            FastStep,
78            StepModeTypeCount
79        };
80
81        public:
82        StepHandler(World *world, 
83            StepModeType stepModeType = QuickStep,
84            const Ogre::Real step_size = Ogre::Real (0.01), 
85            const Ogre::Real max_interval = Ogre::Real(1.0 / 4), 
86            const Ogre::Real time_scale = Ogre::Real(1.0));
87                virtual ~StepHandler();
88
89                virtual bool step(const Ogre::Real time);
90
91                Ogre::Real getStepSize() const {return _step_size;}
92                void setStepSize (const Ogre::Real step_size){_step_size = step_size;}
93
94                void pause(bool pause);
95                bool isPaused();
96
97                void setStepListener(StepListener* listener);
98                void setAutomatic(StepHandler::AutoMode mode, Ogre::Root* root = 0);
99
100                bool frameStarted(const Ogre::FrameEvent& evt);
101                bool frameEnded(const Ogre::FrameEvent& evt);
102
103    protected:
104        bool isRunning(const Ogre::Real time);
105        bool prepareSteppingTime(const Ogre::Real time);
106        bool basicStep(const Ogre::Real time);
107
108        protected:
109                World* _world;
110                bool _paused,_auto_pre,_auto_post;
111                StepListener* _listener;
112                Ogre::Root* _root;
113                Ogre::Real _step_size;
114        Ogre::Real _total_time;
115        Ogre::Real _max_interval;
116        Ogre::Real _time_scale;
117
118        StepMode *_current_stepper;
119    };
120
121    //------------------------------------------------------------------------------------------------
122        class _OgreOdeExport ForwardFixedStepHandler:public StepHandler
123        {
124        public:
125        ForwardFixedStepHandler(World *world, 
126            StepModeType stepModeType = QuickStep,
127            const Ogre::Real step_size = Ogre::Real (0.01), 
128            const Ogre::Real max_interval = Ogre::Real(1.0 / 4), 
129            const Ogre::Real time_scale = Ogre::Real(1.0));
130                virtual ~ForwardFixedStepHandler();
131
132                virtual bool step(const Ogre::Real time);
133    };
134    //------------------------------------------------------------------------------------------------
135        class _OgreOdeExport ExactVariableStepHandler:public StepHandler
136        {
137        public:
138        ExactVariableStepHandler(World *world ,
139            StepModeType stepModeType = QuickStep,
140            const Ogre::Real step_size = Ogre::Real (0.01), 
141            const Ogre::Real max_interval = Ogre::Real(1.0 / 4), 
142            const Ogre::Real time_scale = Ogre::Real(1.0));
143                virtual ~ExactVariableStepHandler();
144
145                virtual bool step(const Ogre::Real time);
146    };
147    //------------------------------------------------------------------------------------------------
148        // Fix your timestep Gaffer implementation.
149        // http://www.gaffer.org/articles/Timestep.html
150        // Gaffer :"If you implement this interpolation technique you ensure that there will
151        // not be any visual stuttering when your display and physics framerates
152        // are out of sync. It will also perfectly handle the undersampling case so
153        // your objects will move smoothly when you view your simulation in slow
154        // motion or ten years from now on that Sexium."
155        class _OgreOdeExport ForwardFixedInterpolatedStepHandler :public StepHandler
156        {
157        public:
158        ForwardFixedInterpolatedStepHandler(World *world, 
159            StepModeType stepModeType = QuickStep,
160            const Ogre::Real step_size = Ogre::Real (0.01), 
161            const Ogre::Real frame_rate = Ogre::Real (1.0 / 60),
162            const Ogre::Real max_interval = Ogre::Real(1.0 / 4), 
163            const Ogre::Real time_scale = Ogre::Real(1.0));
164                virtual ~ForwardFixedInterpolatedStepHandler();
165
166                virtual bool step(const Ogre::Real time);
167    private :
168        Ogre::Real _dbl_step_size;
169        Ogre::Real _inv_step_size;
170
171        Ogre::Real _next_total_time;
172        unsigned int _next_frame_step_count;
173        Ogre::Real _inv_next_total_time;
174        bool _fixed_frame_rate;
175        Ogre::Real _frame_rate;
176        };
177
178}
179
180#endif
Note: See TracBrowser for help on using the repository browser.