Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/ogreode/include/OgreOdeStepper.h @ 21

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

added ogreode and Colladaplugin

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