PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: Body.h * 00003 * 00004 * Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/) 00005 * 00006 * This file is part of PixelLight. 00007 * 00008 * PixelLight is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * PixelLight is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with PixelLight. If not, see <http://www.gnu.org/licenses/>. 00020 \*********************************************************/ 00021 00022 00023 #ifndef __PLPHYSICS_BODY_H__ 00024 #define __PLPHYSICS_BODY_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include <PLCore/Base/Event/Event.h> 00032 #include "PLPhysics/Element.h" 00033 00034 00035 //[-------------------------------------------------------] 00036 //[ Forward declarations ] 00037 //[-------------------------------------------------------] 00038 namespace PLMath { 00039 class Vector3; 00040 class Matrix3x4; 00041 class Quaternion; 00042 class AABoundingBox; 00043 } 00044 namespace PLPhysics { 00045 class BodyImpl; 00046 } 00047 00048 00049 //[-------------------------------------------------------] 00050 //[ Namespace ] 00051 //[-------------------------------------------------------] 00052 namespace PLPhysics { 00053 00054 00055 //[-------------------------------------------------------] 00056 //[ Classes ] 00057 //[-------------------------------------------------------] 00058 /** 00059 * @brief 00060 * Abstract PL physics (rigid) body base class 00061 * 00062 * @remarks 00063 * If a physics body is created, it is frozen automatically. Bodies can interact if they are in 00064 * collision groups (see GetCollisionGroup()) which can collide, (see World::GetGroupCollision()) 00065 * and if within the body pair flag (see World::GetBodyPairFlags()) World::Ignore is NOT set - 00066 * AND at least, IsCollisionActive() of both must return 'true'. 00067 */ 00068 class Body : public Element { 00069 00070 00071 //[-------------------------------------------------------] 00072 //[ Events ] 00073 //[-------------------------------------------------------] 00074 public: 00075 PLCore::Event<> EventTransform; /**< Transform was changed by the physics */ 00076 00077 00078 //[-------------------------------------------------------] 00079 //[ Public functions ] 00080 //[-------------------------------------------------------] 00081 public: 00082 /** 00083 * @brief 00084 * Destructor 00085 */ 00086 PLPHYSICS_API virtual ~Body(); 00087 00088 /** 00089 * @brief 00090 * Returns a reference to the physics API specific body implementation 00091 * 00092 * @return 00093 * Reference to the physics API specific body implementation 00094 */ 00095 PLPHYSICS_API BodyImpl &GetBodyImpl() const; 00096 00097 /** 00098 * @brief 00099 * Returns whether the body is currently active or not 00100 * 00101 * @return 00102 * 'true' if the body is currently active, else 'false' 00103 * 00104 * @note 00105 * - With 'active' we mean 'totally ignored' and not 'just currently frozen' (see IsFrozen()) 00106 */ 00107 PLPHYSICS_API bool IsActive() const; 00108 00109 /** 00110 * @brief 00111 * Sets whether the body is currently active or not 00112 * 00113 * @param[in] bActive 00114 * 'true' if the body is currently active, else 'false' 00115 * 00116 * @see 00117 * - IsActive() 00118 */ 00119 PLPHYSICS_API void SetActive(bool bActive = true); 00120 00121 /** 00122 * @brief 00123 * Returns the collision volume of the physics body 00124 * 00125 * @return 00126 * The collision volume of the physics body 00127 * 00128 * @remarks 00129 * The collision volume of the body can for instance be used for buoyancy. 00130 */ 00131 PLPHYSICS_API float GetCollisionVolume() const; 00132 00133 /** 00134 * @brief 00135 * Returns the mass of the physics body 00136 * 00137 * @return 00138 * Mass of the physics body 00139 */ 00140 PLPHYSICS_API float GetMass() const; 00141 00142 /** 00143 * @brief 00144 * Gets the relative center of the mass 00145 * 00146 * @param[out] vPosition 00147 * Will receive the relative center of the mass 00148 * 00149 * @note 00150 * - The relative center of the mass does't 'move' the body, it just has an influence on it's behavior 00151 */ 00152 PLPHYSICS_API void GetCenterOfMass(PLMath::Vector3 &vPosition) const; 00153 00154 /** 00155 * @brief 00156 * Sets the relative center of the mass 00157 * 00158 * @param[in] vPosition 00159 * New relative center of the mass (PLMath::Vector3::Zero by default) 00160 * 00161 * @see 00162 * - GetCenterOfMass() 00163 */ 00164 PLPHYSICS_API void SetCenterOfMass(const PLMath::Vector3 &vPosition); 00165 00166 /** 00167 * @brief 00168 * Gets the body position 00169 * 00170 * @param[out] vPosition 00171 * Will receive the current body position 00172 */ 00173 PLPHYSICS_API void GetPosition(PLMath::Vector3 &vPosition) const; 00174 00175 /** 00176 * @brief 00177 * Sets the body position 00178 * 00179 * @param[in] vPosition 00180 * New body position 00181 */ 00182 PLPHYSICS_API void SetPosition(const PLMath::Vector3 &vPosition); 00183 00184 /** 00185 * @brief 00186 * Gets the body rotation 00187 * 00188 * @param[out] qRotation 00189 * Will receive the body rotation 00190 */ 00191 PLPHYSICS_API void GetRotation(PLMath::Quaternion &qRotation) const; 00192 00193 /** 00194 * @brief 00195 * Sets the body rotation 00196 * 00197 * @param[in] qRotation 00198 * New body rotation 00199 */ 00200 PLPHYSICS_API void SetRotation(const PLMath::Quaternion &qRotation); 00201 00202 /** 00203 * @brief 00204 * Gets the body transform matrix 00205 * 00206 * @param[out] mTrans 00207 * Will receive the body transform matrix 00208 */ 00209 PLPHYSICS_API void GetTransformMatrix(PLMath::Matrix3x4 &mTrans) const; 00210 00211 /** 00212 * @brief 00213 * Sets the body transform matrix 00214 * 00215 * @param[in] mTrans 00216 * New body transform matrix 00217 */ 00218 PLPHYSICS_API void SetTransformMatrix(const PLMath::Matrix3x4 &mTrans); 00219 00220 /** 00221 * @brief 00222 * Gets the global space axis aligned bounding box of the body 00223 * 00224 * @param[out] cAABoundingBox 00225 * Will receive the global space axis aligned bounding box of the body 00226 */ 00227 PLPHYSICS_API void GetAABoundingBox(PLMath::AABoundingBox &cAABoundingBox) const; 00228 00229 /** 00230 * @brief 00231 * Gets the linear body velocity 00232 * 00233 * @param[out] vVelocity 00234 * Will receive the current linear body velocity 00235 */ 00236 PLPHYSICS_API void GetLinearVelocity(PLMath::Vector3 &vVelocity) const; 00237 00238 /** 00239 * @brief 00240 * Sets the linear body velocity 00241 * 00242 * @param[in] vVelocity 00243 * New linear body velocity 00244 */ 00245 PLPHYSICS_API void SetLinearVelocity(const PLMath::Vector3 &vVelocity); 00246 00247 /** 00248 * @brief 00249 * Gets the angular body velocity 00250 * 00251 * @param[out] vVelocity 00252 * Will receive the current angular body velocity 00253 */ 00254 PLPHYSICS_API void GetAngularVelocity(PLMath::Vector3 &vVelocity) const; 00255 00256 /** 00257 * @brief 00258 * Sets the angular body velocity 00259 * 00260 * @param[in] vVelocity 00261 * New angular body velocity 00262 */ 00263 PLPHYSICS_API void SetAngularVelocity(const PLMath::Vector3 &vVelocity); 00264 00265 /** 00266 * @brief 00267 * Gets the force applied to the body at the next simulation update 00268 * 00269 * @param[out] vForce 00270 * Will receive the force applied to the body at the next simulation update 00271 * 00272 * @remarks 00273 * The forces are accumulated on to each body, and the accumulators are zeroed after each simulation update. 00274 */ 00275 PLPHYSICS_API void GetForce(PLMath::Vector3 &vForce) const; 00276 00277 /** 00278 * @brief 00279 * Adds a force to the body 00280 * 00281 * @param[in] vForce 00282 * Force to add 00283 * 00284 * @remarks 00285 * If the simulation is currently inactive, it's NOT recommended to add forces because they are not reset each frame! 00286 * 00287 * @see 00288 * - GetForce() 00289 */ 00290 PLPHYSICS_API void AddForce(const PLMath::Vector3 &vForce); 00291 00292 /** 00293 * @brief 00294 * Sets the force applied to the body at the next simulation update 00295 * 00296 * @param[in] vForce 00297 * Force to set 00298 * 00299 * @remarks 00300 * It's not recommended to set the force directly by using this function, use AddForce() 00301 * instead whenever possible! 00302 * 00303 * @see 00304 * - GetForce() 00305 */ 00306 PLPHYSICS_API void SetForce(const PLMath::Vector3 &vForce); 00307 00308 /** 00309 * @brief 00310 * Gets the torque applied to the body at the next simulation update 00311 * 00312 * @param[out] vTorque 00313 * Will receive the torque applied to the body at the next simulation update 00314 * 00315 * @remarks 00316 * The torques are accumulated on to each body, and the accumulators are zeroed after each simulation update. 00317 */ 00318 PLPHYSICS_API void GetTorque(PLMath::Vector3 &vTorque) const; 00319 00320 /** 00321 * @brief 00322 * Adds a torque to the body 00323 * 00324 * @param[in] vTorque 00325 * Torque to add 00326 * 00327 * @remarks 00328 * If the simulation is currently inactive, it's NOT recommended to add torques because they are not reset each frame! 00329 * 00330 * @see 00331 * - GetTorque() 00332 */ 00333 PLPHYSICS_API void AddTorque(const PLMath::Vector3 &vTorque); 00334 00335 /** 00336 * @brief 00337 * Sets the torque applied to the body at the next simulation update 00338 * 00339 * @param[in] vTorque 00340 * Torque to set 00341 * 00342 * @remarks 00343 * It's not recommended to set the torque directly by using this function, use AddTorque() 00344 * instead whenever possible! 00345 * 00346 * @see 00347 * - GetTorque() 00348 */ 00349 PLPHYSICS_API void SetTorque(const PLMath::Vector3 &vTorque); 00350 00351 /** 00352 * @brief 00353 * Returns whether the body is frozen automatically or not 00354 * 00355 * @return 00356 * 'true' if the body is frozen automatically, else 'false' 00357 * 00358 * @remarks 00359 * A disabled body that is connected through a joint to an enabled body will be automatically re-enabled at 00360 */ 00361 PLPHYSICS_API bool IsAutoFreeze() const; 00362 00363 /** 00364 * @brief 00365 * Sets whether the body is frozen automatically or not 00366 * 00367 * @param[in] bAutoFreeze 00368 * 'true' if the body is frozen automatically, else 'false' 00369 */ 00370 PLPHYSICS_API void SetAutoFreeze(bool bAutoFreeze = true); 00371 00372 /** 00373 * @brief 00374 * Returns the freeze threshold 00375 * 00376 * @param[out] fLinearVelocity 00377 * Receives the linear velocity threshold 00378 * @param[out] fAngularVelocity 00379 * Receives the angular velocity threshold 00380 * @param[out] nSteps 00381 * Receives the number of steps the linear and angular velocity must be 00382 * below the thresholds before the body is frozen 00383 * 00384 * @note 00385 * - nSteps may not be supported by each physics API, in this case, this setting has no effect 00386 */ 00387 PLPHYSICS_API void GetFreezeThreshold(float &fLinearVelocity, float &fAngularVelocity, PLCore::uint32 &nSteps) const; 00388 00389 /** 00390 * @brief 00391 * Sets the freeze threshold 00392 * 00393 * @param[in] fLinearVelocity 00394 * Linear velocity threshold 00395 * @param[in] fAngularVelocity 00396 * Angular velocity threshold 00397 * @param[in] nSteps 00398 * Number of steps the linear and angular velocity must be below the thresholds 00399 * before the body is frozen 00400 * 00401 * @see 00402 * - SetFreezeThreshold() 00403 */ 00404 PLPHYSICS_API void SetFreezeThreshold(float fLinearVelocity = 0.01f, float fAngularVelocity = 0.01f, PLCore::uint32 nSteps = 10); 00405 00406 /** 00407 * @brief 00408 * Returns whether the body is currently frozen or not 00409 * 00410 * @return 00411 * 'true' if the body is currently frozen, else 'false' 00412 */ 00413 PLPHYSICS_API bool IsFrozen() const; 00414 00415 /** 00416 * @brief 00417 * Sets whether the body is currently frozen or not 00418 * 00419 * @param[in] bFrozen 00420 * 'true' if the body is currently frozen, else 'false' 00421 */ 00422 PLPHYSICS_API void SetFrozen(bool bFrozen = true); 00423 00424 /** 00425 * @brief 00426 * Returns whether the body can collide or not 00427 * 00428 * @return 00429 * 'true' if the body can collide, else 'false' 00430 */ 00431 PLPHYSICS_API bool IsCollisionActive() const; 00432 00433 /** 00434 * @brief 00435 * Sets whether the body can collide or not 00436 * 00437 * @param[in] bActive 00438 * 'true' if the body can collide, else 'false' 00439 */ 00440 PLPHYSICS_API void SetCollisionActive(bool bActive = true); 00441 00442 /** 00443 * @brief 00444 * Returns whether the body ignores the gravity or not 00445 * 00446 * @return 00447 * 'false' if the body ignores the gravity, else 'true' 00448 */ 00449 PLPHYSICS_API bool GetUseGravity() const; 00450 00451 /** 00452 * @brief 00453 * Sets whether the body ignores the gravity or not 00454 * 00455 * @param[in] bUseGravity 00456 * 'false' if the body ignores the gravity, else 'true' 00457 */ 00458 PLPHYSICS_API void SetUseGravity(bool bUseGravity = true); 00459 00460 /** 00461 * @brief 00462 * Returns the collision group the body is in 00463 * 00464 * @return 00465 * The collision group the body is in (0-31) 00466 * 00467 * @see 00468 * - World::GetGroupCollision() 00469 */ 00470 PLPHYSICS_API PLCore::uint8 GetCollisionGroup() const; 00471 00472 /** 00473 * @brief 00474 * Sets the collision group the body is in 00475 * 00476 * @param[in] nCollisionGroup 00477 * The collision group the body is in (0-31) 00478 * 00479 * @see 00480 * - World::GetGroupCollision() 00481 */ 00482 PLPHYSICS_API void SetCollisionGroup(PLCore::uint8 nCollisionGroup = 0); 00483 00484 /** 00485 * @brief 00486 * Returns the number of partner bodies 00487 * 00488 * @return 00489 * The number of partner bodies 00490 * 00491 * @remarks 00492 * Each body can have multiple partners. You can set certain flags for each 00493 * of this pairs using World::SetBodyPairFlags(), for instance disabling contact 00494 * generation between them. If a body pair flag is set to 0, the partners are removed 00495 * automatically from each other. 00496 * 00497 * @see 00498 * - GetBodyPairFlags() & SetBodyPairFlags() 00499 */ 00500 PLPHYSICS_API PLCore::uint32 GetNumOfPartners() const; 00501 00502 /** 00503 * @brief 00504 * Returns a partner body 00505 * 00506 * @param[in] nIndex 00507 * Index of the partner body to return 00508 * 00509 * @return 00510 * The requested partner body, a null pointer on error 00511 * 00512 * @see 00513 * - GetNumOfPartners() 00514 */ 00515 PLPHYSICS_API Body *GetPartner(PLCore::uint32 nIndex) const; 00516 00517 00518 //[-------------------------------------------------------] 00519 //[ Public virtual Body functions ] 00520 //[-------------------------------------------------------] 00521 public: 00522 /** 00523 * @brief 00524 * Sets the mass of the physics body 00525 * 00526 * @param[in] fMass 00527 * New mass of the physics body, 0 = static body 00528 * 00529 * @see 00530 * - World::IsAlwaysStatic() 00531 */ 00532 PLPHYSICS_API virtual void SetMass(float fMass = 0.0f); 00533 00534 00535 //[-------------------------------------------------------] 00536 //[ Public virtual Element functions ] 00537 //[-------------------------------------------------------] 00538 public: 00539 PLPHYSICS_API virtual bool IsBody() const override; 00540 PLPHYSICS_API virtual bool IsJoint() const override; 00541 PLPHYSICS_API virtual bool IsSensor() const override; 00542 00543 00544 //[-------------------------------------------------------] 00545 //[ Protected functions ] 00546 //[-------------------------------------------------------] 00547 protected: 00548 /** 00549 * @brief 00550 * Constructor 00551 * 00552 * @param[in] cWorld 00553 * World this body is in 00554 * @param[in] cBodyImpl 00555 * Reference to the physics API specific body implementation 00556 */ 00557 PLPHYSICS_API Body(World &cWorld, BodyImpl &cBodyImpl); 00558 00559 00560 //[-------------------------------------------------------] 00561 //[ Private data ] 00562 //[-------------------------------------------------------] 00563 private: 00564 BodyImpl *m_pBodyImpl; /**< Pointer to the physics API specific body implementation (always valid!) */ 00565 00566 00567 }; 00568 00569 00570 //[-------------------------------------------------------] 00571 //[ Namespace ] 00572 //[-------------------------------------------------------] 00573 } // PLPhysics 00574 00575 00576 #endif // __PLPHYSICS_BODY_H__
|