1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of the OGRE Reference Application, a layer built |
---|
4 | on top of OGRE(Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | |
---|
24 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | #ifndef __REFAPP_APPLICATIONOBJECT_H__ |
---|
30 | #define __REFAPP_APPLICATIONOBJECT_H__ |
---|
31 | |
---|
32 | #include "OgreRefAppPrerequisites.h" |
---|
33 | |
---|
34 | namespace OgreRefApp { |
---|
35 | |
---|
36 | /** This object is the base class for all discrete objects in the application. |
---|
37 | @remarks |
---|
38 | This object holds a reference to the underlying OGRE entity / entities which |
---|
39 | comprise it, plus links to the additional properties required to make it |
---|
40 | work in the application world. |
---|
41 | @remarks |
---|
42 | It extends the OGRE UserDefinedObject to allow reverse links from Ogre::Entity. |
---|
43 | Note that this class does not override the UserDefinedObject's getTypeId method |
---|
44 | because this class is abstract. |
---|
45 | */ |
---|
46 | class _OgreRefAppExport ApplicationObject : public UserDefinedObject |
---|
47 | { |
---|
48 | protected: |
---|
49 | // Visual component |
---|
50 | SceneNode* mSceneNode; |
---|
51 | Entity* mEntity; |
---|
52 | |
---|
53 | /// Dynamics properties, must be set up by subclasses if dynamics enabled |
---|
54 | dBody* mOdeBody; |
---|
55 | /// Mass parameters |
---|
56 | dMass mMass; |
---|
57 | |
---|
58 | |
---|
59 | /// Collision proxies, must be set up if collision enabled |
---|
60 | typedef std::list<dGeom*> CollisionProxyList; |
---|
61 | CollisionProxyList mCollisionProxies; |
---|
62 | |
---|
63 | |
---|
64 | bool mDynamicsEnabled; |
---|
65 | bool mReenableIfInteractedWith; |
---|
66 | bool mCollisionEnabled; |
---|
67 | |
---|
68 | Real mBounceCoeffRestitution; |
---|
69 | Real mBounceVelocityThreshold; |
---|
70 | Real mSoftness; |
---|
71 | Real mFriction; |
---|
72 | Real mLinearVelDisableThreshold; |
---|
73 | Real mAngularVelDisableThreshold; |
---|
74 | Real mDisableTime; |
---|
75 | Real mDisableTimeEnd; |
---|
76 | |
---|
77 | // Set up method, must override |
---|
78 | virtual void setUp(const String& name) = 0; |
---|
79 | /** Internal method for updating the state of the collision proxies. */ |
---|
80 | virtual void updateCollisionProxies(void); |
---|
81 | |
---|
82 | /** Internal method for testing the plane bounded region WorldFragment type. */ |
---|
83 | virtual bool testCollidePlaneBounds(SceneQuery::WorldFragment* wf); |
---|
84 | |
---|
85 | /// Internal method for updating the query mask |
---|
86 | virtual void setEntityQueryFlags(void); |
---|
87 | |
---|
88 | public: |
---|
89 | ApplicationObject(const String& name); |
---|
90 | virtual ~ApplicationObject(); |
---|
91 | |
---|
92 | /** Sets the position of this object. */ |
---|
93 | virtual void setPosition(const Vector3& vec); |
---|
94 | /** Sets the position of this object. */ |
---|
95 | virtual void setPosition(Real x, Real y, Real z); |
---|
96 | /** Sets the orientation of this object. */ |
---|
97 | virtual void setOrientation(const Quaternion& orientation); |
---|
98 | /** Gets the current position of this object. */ |
---|
99 | virtual const Vector3& getPosition(void); |
---|
100 | /** Gets the current orientation of this object. */ |
---|
101 | virtual const Quaternion& getOrientation(void); |
---|
102 | |
---|
103 | /// Updates the position of this game object from the simulation |
---|
104 | virtual void _updateFromDynamics(void); |
---|
105 | |
---|
106 | /// Returns whether or not this object is considered for collision. |
---|
107 | virtual bool isCollisionEnabled(void); |
---|
108 | /** Returns whether or not this object is physically simulated. |
---|
109 | @remarks |
---|
110 | Objects which are not physically simulated only move when their |
---|
111 | SceneNode is manually altered. |
---|
112 | */ |
---|
113 | virtual bool isDynamicsEnabled(void); |
---|
114 | /** Sets whether or not this object is considered for collision. |
---|
115 | @remarks |
---|
116 | Objects which have collision enabled must set up an ODE |
---|
117 | collision proxy as part of their setUp method. |
---|
118 | */ |
---|
119 | |
---|
120 | /** Sets the linear and angular velocity thresholds, below which the |
---|
121 | object will have it's dynamics automatically disabled for performance. |
---|
122 | @remarks |
---|
123 | These thresholds are used to speed up the simulation and to make it more |
---|
124 | stable, by turning off dynamics for objects that appear to be at rest. |
---|
125 | Otherwise, objects which are supposedly stationary can jitter when involved |
---|
126 | in large stacks, and can consume unnecessary CPU time. Note that if another |
---|
127 | object interacts with the disabled object, it will automatically reenable itself. |
---|
128 | @par |
---|
129 | If you never want to disable dynamics automatically for this object, just |
---|
130 | set all the values to 0. |
---|
131 | @param linearSq The squared linear velocity magnitude threshold |
---|
132 | @param angularSq The squared angular velocity magnitude threshold |
---|
133 | @param overTime The number of seconds over which the values must continue to be under |
---|
134 | this threshold for the dynamics to be disabled. This is to catch cases |
---|
135 | where the object almost stops moving because of a boundary condition, but |
---|
136 | would speed up again later (e.g. box teetering on an edge). |
---|
137 | |
---|
138 | */ |
---|
139 | virtual void setDynamicsDisableThreshold(Real linearSq, Real angularSq, Real overTime); |
---|
140 | |
---|
141 | virtual void setCollisionEnabled(bool enabled); |
---|
142 | /** Sets whether or not this object is physically simulated at this time. |
---|
143 | @remarks |
---|
144 | Objects which are not physically simulated only move when their |
---|
145 | SceneNode is manually altered. Objects which are physically |
---|
146 | simulated must set up an ODE body as part of their setUp method. |
---|
147 | @par |
---|
148 | You can also use this to temporarily turn off simulation on an object, |
---|
149 | such that it is not simulated until some other object which IS simulated |
---|
150 | comes in contact with it, or is attached to it with a joint. |
---|
151 | @param enabled Specifies whether dynamics is enabled |
---|
152 | @param reEnableOnInteraction If set to true, this object will reenable if some |
---|
153 | other dynamically simulated object interacts with it |
---|
154 | */ |
---|
155 | virtual void setDynamicsEnabled(bool enabled, bool reEnableOnInteraction = false); |
---|
156 | |
---|
157 | /** Sets the 'bounciness' of this object. |
---|
158 | * @remarks |
---|
159 | * Only applies if this object has both collision and dynamics enabled. |
---|
160 | * When 2 movable objects collide, the greatest bounce parameters |
---|
161 | * from both objects apply, so even a non-bouncy object can |
---|
162 | * bounce if it hits a bouncy surface. |
---|
163 | * @param restitutionValue Coeeficient of restitution |
---|
164 | * (0 for no bounce, 1 for perfect bounciness) |
---|
165 | * @param velocityThreshold Velocity below which no bounce will occur; |
---|
166 | * this is a dampening value to ensure small velocities do not |
---|
167 | * cause bounce. |
---|
168 | */ |
---|
169 | virtual void setBounceParameters(Real restitutionValue, Real velocityThreshold); |
---|
170 | /** Gets the cefficient of restitution (bounciness) for this object. */ |
---|
171 | virtual Real getBounceRestitutionValue(void); |
---|
172 | /** Gets the bounce velocity threshold for this object. */ |
---|
173 | virtual Real getBounceVelocityThreshold(void); |
---|
174 | |
---|
175 | /** Sets the softness of this object, which determines how much it is allowed to |
---|
176 | * penetrate other objects. |
---|
177 | * @remarks |
---|
178 | * This parameter only has meaning if collision and dynamics are enabled for this object. |
---|
179 | * @param softness Softness factor (0 is completely hard). Softness will be combined from |
---|
180 | * both objects involved in a collision to determine how much they will penetrate. |
---|
181 | */ |
---|
182 | virtual void setSoftness(Real softness); |
---|
183 | /** Gets the softness factor of this object. */ |
---|
184 | virtual Real getSoftness(void); |
---|
185 | |
---|
186 | /** Sets the Coulomb frictional coefficient for this object. |
---|
187 | @remarks |
---|
188 | This coefficient affects how much an object will slip when it comes |
---|
189 | into contact with another object. |
---|
190 | @param friction The Coulomb friction coefficient, valid from 0 to Math::POS_INFINITY. |
---|
191 | 0 means no friction, Math::POS_INFINITY means infinite friction ie no slippage. |
---|
192 | Note that friction between these 2 bounds is more CPU intensive so use with caution. |
---|
193 | */ |
---|
194 | virtual void setFriction(Real friction); |
---|
195 | /** Gets the Coulomb frictional coefficient for this object. */ |
---|
196 | virtual Real getFriction(void); |
---|
197 | /** Adds a linear force to this object, in object space, at the position indicated. |
---|
198 | @remarks |
---|
199 | All forces are applied, then reset after World::applyDynamics is called. |
---|
200 | @param direction The force direction in object coordinates. |
---|
201 | @param atPosition The position at which the force is to be applied, in object coordinates. |
---|
202 | */ |
---|
203 | virtual void addForce(const Vector3& direction, const Vector3& atPosition = Vector3::ZERO); |
---|
204 | /** Adds a linear force to this object, in object space, at the position indicated. |
---|
205 | @remarks |
---|
206 | All forces are applied, then reset after World::applyDynamics is called. |
---|
207 | @param dir_x, dir_y, dir_z The force direction in object coordinates. |
---|
208 | @param pos_x, pos_y, pos_z The position at which the force is to be applied, in object coordinates. |
---|
209 | */ |
---|
210 | virtual void addForce(Real dir_x, Real dir_y, Real dir_z, |
---|
211 | Real pos_x = 0, Real pos_y = 0, Real pos_z = 0); |
---|
212 | /** Adds a linear force to this object, in world space, at the position indicated. |
---|
213 | @remarks |
---|
214 | All forces are applied, then reset after World::applyDynamics is called. |
---|
215 | @param direction The force direction in world coordinates. |
---|
216 | @param atPosition The position at which the force is to be applied, in world coordinates. |
---|
217 | */ |
---|
218 | virtual void addForceWorldSpace(const Vector3& direction, const Vector3& atPosition = Vector3::ZERO); |
---|
219 | /** Adds a linear force to this object, in world space, at the position indicated. |
---|
220 | @remarks |
---|
221 | All forces are applied, then reset after World::applyDynamics is called. |
---|
222 | @param dir_x, dir_y, dir_z The force direction in world coordinates. |
---|
223 | @param pos_x, pos_y, pos_z The position at which the force is to be applied, in world coordinates. |
---|
224 | */ |
---|
225 | virtual void addForceWorldSpace(Real dir_x, Real dir_y, Real dir_z, |
---|
226 | Real pos_x, Real pos_y, Real pos_z); |
---|
227 | /** Adds rotational force to this object, in object space. |
---|
228 | @remarks |
---|
229 | All forces are applied, then reset after World::applyDynamics is called. |
---|
230 | @param direction The direction of the torque to apply, in object space. */ |
---|
231 | virtual void addTorque(const Vector3& direction); |
---|
232 | /** Adds rotational force to this object, in object space. |
---|
233 | @remarks |
---|
234 | All forces are applied, then reset after World::applyDynamics is called. |
---|
235 | @param x, y, z The direction of the torque to apply, in object space. */ |
---|
236 | virtual void addTorque(Real x, Real y, Real z); |
---|
237 | /** Adds rotational force to this object, in world space. |
---|
238 | @remarks |
---|
239 | All forces are applied, then reset after World::applyDynamics is called. |
---|
240 | @param direction The direction of the torque to apply, in world space. */ |
---|
241 | virtual void addTorqueWorldSpace(const Vector3& direction); |
---|
242 | /** Adds rotational force to this object, in world space. |
---|
243 | @remarks |
---|
244 | All forces are applied, then reset after World::applyDynamics is called. |
---|
245 | @param x, y, z The direction of the torque to apply, in world space. */ |
---|
246 | virtual void addTorqueWorldSpace(Real x, Real y, Real z); |
---|
247 | |
---|
248 | /** Tests to see if there is a detailed collision between this object and the object passed in. |
---|
249 | @remarks |
---|
250 | If there is a collision, both objects will be notified and if dynamics are enabled |
---|
251 | on these objects, physics will be applied automatically. |
---|
252 | @returns true if collision occurred |
---|
253 | |
---|
254 | */ |
---|
255 | virtual bool testCollide(ApplicationObject* otherObj); |
---|
256 | |
---|
257 | /** Tests to see if there is a detailed collision between this object and the |
---|
258 | world fragment passed in. |
---|
259 | @remarks |
---|
260 | If there is a collision, the object will be notified and if dynamics are enabled |
---|
261 | on this object, physics will be applied automatically. |
---|
262 | @returns true if collision occurred |
---|
263 | */ |
---|
264 | virtual bool testCollide(SceneQuery::WorldFragment* wf); |
---|
265 | |
---|
266 | /** Contains information about a collision; used in the _notifyCollided call. */ |
---|
267 | struct CollisionInfo |
---|
268 | { |
---|
269 | /// The position in world coordinates at which the collision occurred |
---|
270 | Vector3 position; |
---|
271 | /// The normal in world coordinates of the collision surface |
---|
272 | Vector3 normal; |
---|
273 | /// Penetration depth |
---|
274 | Real penetrationDepth; |
---|
275 | }; |
---|
276 | /** This method is called automatically if testCollide indicates a real collision. |
---|
277 | */ |
---|
278 | virtual void _notifyCollided(ApplicationObject* otherObj, const CollisionInfo& info); |
---|
279 | /** This method is called automatically if testCollide indicates a real collision. |
---|
280 | */ |
---|
281 | virtual void _notifyCollided(SceneQuery::WorldFragment* wf, const CollisionInfo& info); |
---|
282 | /** Gets the SceneNode which is being used to represent this object's position in |
---|
283 | the OGRE world. */ |
---|
284 | SceneNode* getSceneNode(void); |
---|
285 | /** Gets the Entity which is being used to represent this object in the OGRE world. */ |
---|
286 | Entity* getEntity(void); |
---|
287 | /** Gets the ODE body used to represent this object's mass and current velocity. */ |
---|
288 | dBody* getOdeBody(void); |
---|
289 | |
---|
290 | /** Set the mass parameters of this object to represent a sphere. |
---|
291 | @remarks |
---|
292 | This method sets the mass and inertia properties of this object such |
---|
293 | that it is like a sphere, ie center of gravity at the origin and |
---|
294 | an even distribution of mass in all directions. |
---|
295 | @param density Density of the sphere in Kg/m^3 |
---|
296 | @param radius of the sphere mass |
---|
297 | */ |
---|
298 | void setMassSphere(Real density, Real radius); |
---|
299 | |
---|
300 | /** Set the mass parameters of this object to represent a box. |
---|
301 | @remarks |
---|
302 | This method sets the mass and inertia properties of this object such |
---|
303 | that it is like a box. |
---|
304 | @param density Density of the box in Kg/m^3 |
---|
305 | @param dimensions Width, height and depth of the box. |
---|
306 | @param orientation Optional orientation of the box. |
---|
307 | */ |
---|
308 | void setMassBox(Real density, const Vector3& dimensions, |
---|
309 | const Quaternion& orientation = Quaternion::IDENTITY); |
---|
310 | |
---|
311 | /** Set the mass parameters of this object to represent a capped cylinder. |
---|
312 | @remarks |
---|
313 | This method sets the mass and inertia properties of this object such |
---|
314 | that it is like a capped cylinder, by default lying along the Z-axis. |
---|
315 | @param density Density of the cylinder in Kg/m^3 |
---|
316 | @param length Length of the cylinder |
---|
317 | @param width Width of the cylinder |
---|
318 | @param orientation Optional orientation if you wish the cylinder to lay |
---|
319 | along a different axis from Z. |
---|
320 | */ |
---|
321 | void setMassCappedCylinder(Real density, Real length, Real width, |
---|
322 | const Quaternion& orientation = Quaternion::IDENTITY); |
---|
323 | |
---|
324 | /** Sets the mass parameters manually, use only if you know how! |
---|
325 | @param mass Mass in Kg |
---|
326 | @param center The center of gravity |
---|
327 | @param inertia The inertia matrix describing distribution of the mass around the body. |
---|
328 | */ |
---|
329 | void setMassExpert(Real mass, const Vector3 center, const Matrix3 inertia); |
---|
330 | |
---|
331 | /** Gets the ODE mass parameters for this object. */ |
---|
332 | const dMass* getOdeMass(void); |
---|
333 | |
---|
334 | /** Sets the current linear velocity of this object. |
---|
335 | @remarks |
---|
336 | Only applicable if dynamics are enabled for this object. This method is useful |
---|
337 | for starting an object off at a particular speed rather than applying forces to get |
---|
338 | it there. |
---|
339 | */ |
---|
340 | void setLinearVelocity(const Vector3& vel); |
---|
341 | /** Sets the current linear velocity of this object. |
---|
342 | @remarks |
---|
343 | Only applicable if dynamics are enabled for this object. This method is useful |
---|
344 | for starting an object off at a particular speed rather than applying forces to get |
---|
345 | it there. |
---|
346 | */ |
---|
347 | void setLinearVelocity(Real x, Real y, Real z); |
---|
348 | /** Gets the current linear velocity of this object. |
---|
349 | @remarks |
---|
350 | Only applicable if dynamics are enabled for this object. |
---|
351 | @returns Vector3 representing the velocity in units per second. |
---|
352 | */ |
---|
353 | const Vector3& getLinearVelocity(void); |
---|
354 | |
---|
355 | /** Gets the current angular velocity of this object. |
---|
356 | @remarks |
---|
357 | Only applicable if dynamics are enabled for this object. |
---|
358 | @returns Vector3 representing the angular velocity in units per second around each axis. |
---|
359 | */ |
---|
360 | const Vector3& getAngularVelocity(void); |
---|
361 | |
---|
362 | /** Sets the current angular velocity of this object. |
---|
363 | @remarks |
---|
364 | Only applicable if dynamics are enabled for this object. This method is useful |
---|
365 | for starting an object off rather than applying forces to get |
---|
366 | it there. |
---|
367 | */ |
---|
368 | void setAngularVelocity(const Vector3& vel); |
---|
369 | /** Sets the current angular velocity of this object. |
---|
370 | @remarks |
---|
371 | Only applicable if dynamics are enabled for this object. This method is useful |
---|
372 | for starting an object off rather than applying forces to get |
---|
373 | it there. |
---|
374 | */ |
---|
375 | void setAngularVelocity(Real x, Real y, Real z); |
---|
376 | |
---|
377 | /** Moves the object along it's local axes. |
---|
378 | @par |
---|
379 | This method moves the object by the supplied vector along the |
---|
380 | local axes of the obect. |
---|
381 | @param |
---|
382 | d Vector with x,y,z values representing the translation. |
---|
383 | */ |
---|
384 | virtual void translate(const Vector3& d); |
---|
385 | /** Moves the object along it's local axes. |
---|
386 | @par |
---|
387 | This method moves the object by the supplied vector along the |
---|
388 | local axes of the obect. |
---|
389 | @param x, y z Real x, y and z values representing the translation. |
---|
390 | */ |
---|
391 | virtual void translate(Real x, Real y, Real z); |
---|
392 | |
---|
393 | /** Moves the object along the world axes. |
---|
394 | @par |
---|
395 | This method moves the object by the supplied vector along the |
---|
396 | world axes. |
---|
397 | @param |
---|
398 | d Vector with x,y,z values representing the translation. |
---|
399 | */ |
---|
400 | virtual void translateWorldSpace(const Vector3& d); |
---|
401 | /** Moves the object along the world axes. |
---|
402 | @par |
---|
403 | This method moves the object by the supplied vector along the |
---|
404 | local axes of the obect. |
---|
405 | @param x, y z Real x, y and z values representing the translation. |
---|
406 | */ |
---|
407 | virtual void translateWorldSpace(Real x, Real y, Real z); |
---|
408 | |
---|
409 | /** Rotate the object around the local Z-axis. |
---|
410 | */ |
---|
411 | virtual void roll(const Radian& angle); |
---|
412 | #ifndef OGRE_FORCE_ANGLE_TYPES |
---|
413 | inline void roll(Real angleunits) { |
---|
414 | roll ( Angle(angleunits) ); |
---|
415 | } |
---|
416 | #endif//OGRE_FORCE_ANGLE_TYPES |
---|
417 | |
---|
418 | /** Rotate the object around the local X-axis. |
---|
419 | */ |
---|
420 | virtual void pitch(const Radian& angle); |
---|
421 | #ifndef OGRE_FORCE_ANGLE_TYPES |
---|
422 | inline void pitch(Real angleunits) { |
---|
423 | pitch ( Angle(angleunits) ); |
---|
424 | } |
---|
425 | #endif//OGRE_FORCE_ANGLE_TYPES |
---|
426 | |
---|
427 | /** Rotate the object around the local Y-axis. |
---|
428 | */ |
---|
429 | virtual void yaw(const Radian& angle); |
---|
430 | #ifndef OGRE_FORCE_ANGLE_TYPES |
---|
431 | inline void yaw(Real angleunits) { |
---|
432 | yaw ( Angle(angleunits) ); |
---|
433 | } |
---|
434 | #endif//OGRE_FORCE_ANGLE_TYPES |
---|
435 | |
---|
436 | /** Rotate the object around an arbitrary axis. |
---|
437 | */ |
---|
438 | virtual void rotate(const Vector3& axis, const Radian& angle); |
---|
439 | #ifndef OGRE_FORCE_ANGLE_TYPES |
---|
440 | inline void rotate(const Vector3& axis, Real angleunits) { |
---|
441 | rotate ( axis, Angle(angleunits) ); |
---|
442 | } |
---|
443 | #endif//OGRE_FORCE_ANGLE_TYPES |
---|
444 | |
---|
445 | /** Rotate the object around an aritrary axis using a Quarternion. |
---|
446 | */ |
---|
447 | virtual void rotate(const Quaternion& q); |
---|
448 | |
---|
449 | |
---|
450 | }; |
---|
451 | |
---|
452 | |
---|
453 | } // namespace |
---|
454 | |
---|
455 | #endif |
---|
456 | |
---|
457 | |
---|