1 | #ifndef _OGREODEVEHICLE_H_ |
---|
2 | #define _OGREODEVEHICLE_H_ |
---|
3 | |
---|
4 | #include "OgreOde_Prefab.h" |
---|
5 | |
---|
6 | #include <vector> |
---|
7 | |
---|
8 | namespace OgreOde_Prefab |
---|
9 | { |
---|
10 | |
---|
11 | class _OgreOdeExport_Prefab Vehicle : public Object |
---|
12 | { |
---|
13 | public: |
---|
14 | class _OgreOdeExport_Prefab Engine |
---|
15 | { |
---|
16 | public: |
---|
17 | Engine(); |
---|
18 | ~Engine(); |
---|
19 | |
---|
20 | void setInputs(Ogre::Real throttle_position, Ogre::Real brake_position); |
---|
21 | void setInputs(Ogre::Real throttle_brake); |
---|
22 | void setInputs(bool throttle, bool brake); |
---|
23 | |
---|
24 | Ogre::Real getBrakeForce(); |
---|
25 | Ogre::Real getPowerAtRPM(Ogre::Real rpm); |
---|
26 | Ogre::Real getDesiredRPM(); |
---|
27 | |
---|
28 | void update(Ogre::Real time); |
---|
29 | |
---|
30 | // Manual gearbox |
---|
31 | void changeUp(); |
---|
32 | void changeDown(); |
---|
33 | void setGear(char code); |
---|
34 | void setGear(unsigned int i); |
---|
35 | |
---|
36 | // Information functions |
---|
37 | Ogre::Real getRevFactor(); |
---|
38 | char getGearCode(); |
---|
39 | |
---|
40 | // Engine parameters |
---|
41 | void setRevLimit(Ogre::Real rpm); |
---|
42 | void setTorque(Ogre::Real* torque, unsigned int samples); |
---|
43 | void setTorque(Ogre::Real constant); |
---|
44 | void setTorque(Ogre::Real minimum, Ogre::Real maximum); |
---|
45 | |
---|
46 | // Braking parameters |
---|
47 | void setBrakeForce(Ogre::Real force); |
---|
48 | |
---|
49 | // Drivetrain parameters |
---|
50 | void addGear(Ogre::Real ratio, char code); |
---|
51 | void setFinalDrive(Ogre::Real ratio); |
---|
52 | void setAutoShiftRevs(Ogre::Real up, Ogre::Real down); |
---|
53 | void setAutoBox(bool automatic); |
---|
54 | |
---|
55 | protected: |
---|
56 | std::vector<std::pair<char,Ogre::Real> > _gears; |
---|
57 | unsigned int _current_gear_num; |
---|
58 | std::pair<char,Ogre::Real> _current_gear; |
---|
59 | Ogre::Real* _torque_curve; |
---|
60 | unsigned int _curve_length; |
---|
61 | Ogre::Real _rev_limit,_throttle_position,_brake_position,_wheel_rpm,_inv_rev_limit; |
---|
62 | Ogre::Real _brake_force; |
---|
63 | Ogre::Real _final_drive; |
---|
64 | Ogre::Real _shift_up,_shift_down; |
---|
65 | bool _auto_box; |
---|
66 | }; |
---|
67 | |
---|
68 | class _OgreOdeExport_Prefab Wheel : public Object |
---|
69 | { |
---|
70 | public: |
---|
71 | static const Ogre::Real FudgeFactor; |
---|
72 | |
---|
73 | Wheel(Vehicle* vehicle, |
---|
74 | const Ogre::String& name, |
---|
75 | const Ogre::String& mesh, |
---|
76 | const Ogre::Vector3& position, |
---|
77 | Ogre::Real mass, |
---|
78 | OgreOde::World *world, |
---|
79 | OgreOde::Space* space); |
---|
80 | |
---|
81 | ~Wheel(); |
---|
82 | |
---|
83 | void setFactors(Ogre::Real steer, Ogre::Real power, Ogre::Real brake); |
---|
84 | void setContact(Ogre::Real bouncyness, Ogre::Real friction, Ogre::Real fds) |
---|
85 | { |
---|
86 | _bouncyness = bouncyness; |
---|
87 | _friction = friction; |
---|
88 | _fds = fds; |
---|
89 | } |
---|
90 | |
---|
91 | void setSteerFactor(Ogre::Real factor); |
---|
92 | void setSteerLimit(Ogre::Real limit); |
---|
93 | void setSteerForce(Ogre::Real force){_steer_force = force;} |
---|
94 | void setSteerSpeed(Ogre::Real speed){_steer_speed = speed;} |
---|
95 | void setSteer(Ogre::Real position); |
---|
96 | |
---|
97 | void setPowerFactor(Ogre::Real power){_power_factor = power;} |
---|
98 | void setBrakeFactor(Ogre::Real brake){_brake_factor = brake;} |
---|
99 | |
---|
100 | void setSuspension(Ogre::Real spring_constant, Ogre::Real damping_constant, Ogre::Real step_size); |
---|
101 | |
---|
102 | const Ogre::Vector3& getPosition(); |
---|
103 | const Ogre::Quaternion& getOrientation(); |
---|
104 | void setPosition(const Ogre::Vector3& position); |
---|
105 | |
---|
106 | Ogre::Real getRPM(); |
---|
107 | Ogre::Real getSteer(); |
---|
108 | |
---|
109 | void update(Ogre::Real power_force, Ogre::Real desired_rpm, Ogre::Real brake_force); |
---|
110 | |
---|
111 | Ogre::SceneNode* getSceneNode(){return _node;} |
---|
112 | Ogre::Entity* getEntity(){return _entity;} |
---|
113 | OgreOde::Body* getBody(){return _body;} |
---|
114 | |
---|
115 | OgreOde::Geometry* getGeometry(){return _geometry;} |
---|
116 | |
---|
117 | Ogre::Real getRadius()const{return _radius;} |
---|
118 | |
---|
119 | void snapOff(); |
---|
120 | |
---|
121 | void setupTyreContact(OgreOde::Contact* contact); |
---|
122 | |
---|
123 | protected: |
---|
124 | void adjustJointParameters(); |
---|
125 | |
---|
126 | Ogre::SceneNode* _node; |
---|
127 | Ogre::Entity* _entity; |
---|
128 | Ogre::Real _steer_factor,_steer_limit,_steer_force,_steer_speed; |
---|
129 | Ogre::Real _power_factor; |
---|
130 | Ogre::Real _brake_factor; |
---|
131 | Ogre::Real _spring,_damping,_step; |
---|
132 | Ogre::Real _bouncyness,_friction,_fds; |
---|
133 | Ogre::Real _radius; |
---|
134 | OgreOde::Body* _body; |
---|
135 | OgreOde::Geometry* _geometry; |
---|
136 | OgreOde::SuspensionJoint* _joint; |
---|
137 | Ogre::String _name; |
---|
138 | Vehicle* _owner; |
---|
139 | }; |
---|
140 | |
---|
141 | public: |
---|
142 | Vehicle(const Ogre::String& name,const Ogre::String& mesh, Ogre::Real mass, |
---|
143 | OgreOde::World *world, |
---|
144 | OgreOde::Space* space, |
---|
145 | const Ogre::Vector3& offset = Ogre::Vector3::ZERO, |
---|
146 | OgreOde::Geometry* geometry = 0); |
---|
147 | |
---|
148 | Vehicle(const Ogre::String& name, |
---|
149 | OgreOde::World *world, |
---|
150 | OgreOde::Space* space = 0, |
---|
151 | OgreOde::Geometry* geometry = 0); |
---|
152 | ~Vehicle(); |
---|
153 | |
---|
154 | Vehicle::Wheel* addWheel(const Ogre::String& mesh,const Ogre::Vector3& position, Ogre::Real mass); |
---|
155 | |
---|
156 | const Ogre::Vector3& getPosition(); |
---|
157 | const Ogre::Quaternion& getOrientation(); |
---|
158 | void setPosition(const Ogre::Vector3& position); |
---|
159 | |
---|
160 | void setSuspension(Ogre::Real spring_constant, Ogre::Real damping_constant, Ogre::Real step_size); |
---|
161 | |
---|
162 | void setInputs(Ogre::Real steering, Ogre::Real throttle,Ogre::Real brake); |
---|
163 | void setInputs(Ogre::Real steering, Ogre::Real throttle_brake); |
---|
164 | void setInputs(bool left, bool right, bool throttle, bool brake); |
---|
165 | |
---|
166 | void setSteering(Ogre::Real steering_position); |
---|
167 | |
---|
168 | const Ogre::String& getName(){return _name;} |
---|
169 | |
---|
170 | virtual void applyAntiSwayBarForces(); |
---|
171 | void update(Ogre::Real time); |
---|
172 | |
---|
173 | Ogre::Entity* getEntity(){return _entity;} |
---|
174 | void setEntity(Ogre::Entity *e){ _entity = e;} |
---|
175 | |
---|
176 | OgreOde::Body* getBody(){return _body;} |
---|
177 | |
---|
178 | Ogre::SceneNode* getSceneNode(){return _node;} |
---|
179 | |
---|
180 | Vehicle::Wheel* getWheel(unsigned int i){return _wheels[i];} |
---|
181 | size_t getWheelCount()const{return _wheels.size();} |
---|
182 | |
---|
183 | OgreOde::TransformGeometry* getTransformGeometry(){return _transform;} |
---|
184 | void setTransformGeometry(OgreOde::TransformGeometry*g){_transform = g;} |
---|
185 | |
---|
186 | void setTransNode(Ogre::SceneNode *s){_trans_node = s;} |
---|
187 | Ogre::SceneNode* getTransNode(){return _trans_node;} |
---|
188 | |
---|
189 | OgreOde::Geometry* getGeometry(){return _geometry;} |
---|
190 | void setGeometry(OgreOde::Geometry*g){_geometry = g;} |
---|
191 | |
---|
192 | Engine* getEngine(){return _engine;} |
---|
193 | |
---|
194 | Vehicle::Wheel* findWheel(OgreOde::Geometry* geometry); |
---|
195 | |
---|
196 | OgreOde::Space* getSpace(){return _space;} |
---|
197 | |
---|
198 | Ogre::Vector3 getOffset(){return _offset;} |
---|
199 | void setOffset(const Ogre::Vector3 &of) |
---|
200 | { |
---|
201 | _offset = of; |
---|
202 | if (_geometry) |
---|
203 | _geometry->setPosition(of); |
---|
204 | _trans_node->setPosition(of); |
---|
205 | } |
---|
206 | |
---|
207 | Ogre::Real getVelocity(); |
---|
208 | |
---|
209 | static bool handleTyreCollision(OgreOde::Contact* contact); |
---|
210 | |
---|
211 | void load(const Ogre::String &filename, const Ogre::String &definition_name = Ogre::StringUtil::BLANK); |
---|
212 | |
---|
213 | bool isAntiSwayEnabled()const {return _antisway;}; |
---|
214 | void enableAntiSway(bool enable){_antisway = enable;}; |
---|
215 | |
---|
216 | void setSwayForce(Ogre::Real swayForce){_swayForce = swayForce;}; |
---|
217 | void setSwayForceLimit(unsigned int swayForceLimit){_swayForceLimit = swayForceLimit;} |
---|
218 | void setSwayForceRate(Ogre::Real swayRate){_swayRate = swayRate;} |
---|
219 | |
---|
220 | Ogre::Real getSwayForce (Ogre::Real swayForce) const{return _swayForce;}; |
---|
221 | unsigned int getSwayForceLimit(unsigned int swayForceLimit) const{return _swayForceLimit;} |
---|
222 | Ogre::Real getSwayForceRate(Ogre::Real swayRate) const{return _swayRate;} |
---|
223 | |
---|
224 | protected: |
---|
225 | Ogre::SceneNode* _node; |
---|
226 | Ogre::SceneNode* _trans_node; |
---|
227 | Ogre::Entity* _entity; |
---|
228 | OgreOde::Body* _body; |
---|
229 | OgreOde::TransformGeometry* _transform; |
---|
230 | OgreOde::Geometry* _geometry; |
---|
231 | std::vector<Vehicle::Wheel*> _wheels; |
---|
232 | Ogre::String _name; |
---|
233 | OgreOde::SimpleSpace* _space; |
---|
234 | Ogre::Vector3 _offset; |
---|
235 | Ogre::Vector3 _lastPosition; |
---|
236 | Engine* _engine; |
---|
237 | |
---|
238 | bool _antisway; |
---|
239 | Ogre::Real _swayForce; |
---|
240 | unsigned int _swayForceLimit; |
---|
241 | Ogre::Real _swayRate; |
---|
242 | Ogre::Real _swayLastUpdate; |
---|
243 | }; |
---|
244 | } |
---|
245 | |
---|
246 | #endif |
---|
247 | |
---|