ivaria/physics.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2011 by Liu Lu 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 */ 00018 00019 #ifndef __IVARIA_PHYSIH__ 00020 #define __IVARIA_PHYSIH__ 00021 00026 #include "csutil/scf.h" 00027 #include "csutil/scf_interface.h" 00028 #include "iutil/objreg.h" 00029 #include "iengine/mesh.h" 00030 #include "iengine/engine.h" 00031 #include "imesh/genmesh.h" 00032 #include "csgeom/tri.h" 00033 #include "cstool/primitives.h" 00034 #include "ivaria/collisions.h" 00035 00036 namespace CS 00037 { 00038 namespace Mesh 00039 { 00040 struct iAnimatedMesh; 00041 } 00042 } 00043 00044 namespace CS 00045 { 00046 namespace Collisions 00047 { 00048 struct iCollisionCallback; 00049 struct iCollisionObject; 00050 struct CollisionGroup; 00051 struct iCollisionObject; 00052 } 00053 } 00054 00055 namespace CS 00056 { 00057 namespace Physics 00058 { 00059 00060 struct iJoint; 00061 struct iObject; 00062 struct iRigidBody; 00063 struct iSoftBody; 00064 struct iKinematicCallback; 00065 struct iPhysicalSystem; 00066 00070 enum PhysicalBodyType 00071 { 00072 BODY_RIGID = 0, 00073 BODY_SOFT 00074 }; 00075 00079 enum RigidBodyState 00080 { 00081 STATE_STATIC = 0, 00082 STATE_DYNAMIC, 00083 STATE_KINEMATIC 00084 }; 00085 00090 struct iPhysicalBody : public virtual CS::Collisions::iCollisionObject 00091 { 00092 SCF_INTERFACE (CS::Physics::iPhysicalBody, 1, 0, 0); 00093 00095 virtual PhysicalBodyType GetBodyType () const = 0; 00096 00101 virtual iRigidBody* QueryRigidBody () = 0; 00102 00107 virtual iSoftBody* QuerySoftBody () = 0; 00108 00110 virtual bool Disable () = 0; 00111 00113 virtual bool Enable () = 0; 00114 00116 virtual bool IsEnabled () = 0; 00117 00119 virtual void SetMass (float mass) = 0; 00120 00122 virtual float GetMass () = 0; 00123 00125 virtual float GetDensity () const = 0; 00126 00136 virtual void SetDensity (float density) = 0; 00137 00139 virtual float GetVolume () = 0; 00140 00142 virtual void AddForce (const csVector3& force) = 0; 00143 00145 virtual csVector3 GetLinearVelocity (size_t index = 0) const = 0; 00146 00151 virtual void SetFriction (float friction) = 0; 00152 00154 virtual float GetFriction () = 0; 00155 }; 00156 00175 struct iRigidBody : public virtual iPhysicalBody 00176 { 00177 SCF_INTERFACE (CS::Physics::iRigidBody, 1, 0, 0); 00178 00180 virtual RigidBodyState GetState () = 0; 00181 00183 virtual bool SetState (RigidBodyState state) = 0; 00184 00186 virtual void SetElasticity (float elasticity) = 0; 00187 00189 virtual float GetElasticity () = 0; 00190 00192 virtual void SetLinearVelocity (const csVector3& vel) = 0; 00193 00195 virtual void SetAngularVelocity (const csVector3& vel) = 0; 00196 00198 virtual csVector3 GetAngularVelocity () const = 0; 00199 00201 virtual void AddTorque (const csVector3& torque) = 0; 00202 00204 virtual void AddRelForce (const csVector3& force) = 0; 00205 00207 virtual void AddRelTorque (const csVector3& torque) = 0; 00208 00213 virtual void AddForceAtPos (const csVector3& force, 00214 const csVector3& pos) = 0; 00215 00220 virtual void AddForceAtRelPos (const csVector3& force, 00221 const csVector3& pos) = 0; 00222 00227 virtual void AddRelForceAtPos (const csVector3& force, 00228 const csVector3& pos) = 0; 00229 00234 virtual void AddRelForceAtRelPos (const csVector3& force, 00235 const csVector3& pos) = 0; 00236 00238 virtual csVector3 GetForce () const = 0; 00239 00241 virtual csVector3 GetTorque () const = 0; 00242 00247 virtual void SetKinematicCallback (iKinematicCallback* cb) = 0; 00248 00250 virtual iKinematicCallback* GetKinematicCallback () = 0; 00251 00261 virtual void SetLinearDampener (float d) = 0; 00262 00264 virtual float GetLinearDampener () = 0; 00265 00275 virtual void SetRollingDampener (float d) = 0; 00276 00278 virtual float GetRollingDampener () = 0; 00279 }; 00280 00290 struct iAnchorAnimationControl : public virtual iBase 00291 { 00292 SCF_INTERFACE(CS::Physics::iAnchorAnimationControl, 1, 0, 0); 00293 00297 virtual csVector3 GetAnchorPosition () const = 0; 00298 }; 00299 00322 struct iSoftBody : public virtual iPhysicalBody 00323 { 00324 SCF_INTERFACE (CS::Physics::iSoftBody, 1, 0, 0); 00325 00327 virtual void SetVertexMass (float mass, size_t index) = 0; 00328 00330 virtual float GetVertexMass (size_t index) = 0; 00331 00333 virtual size_t GetVertexCount () = 0; 00334 00336 virtual csVector3 GetVertexPosition (size_t index) const = 0; 00337 00339 virtual void AnchorVertex (size_t vertexIndex) = 0; 00340 00345 virtual void AnchorVertex (size_t vertexIndex, 00346 iRigidBody* body) = 0; 00347 00352 virtual void AnchorVertex (size_t vertexIndex, 00353 iAnchorAnimationControl* controller) = 0; 00354 00367 virtual void UpdateAnchor (size_t vertexIndex, 00368 csVector3& position) = 0; 00369 00374 virtual void RemoveAnchor (size_t vertexIndex) = 0; 00375 00380 virtual void SetRigidity (float rigidity) = 0; 00381 00383 virtual float GetRigidity () = 0; 00384 00386 virtual void SetLinearVelocity (const csVector3& vel) = 0; 00387 00389 virtual void SetLinearVelocity (const csVector3& velocity, 00390 size_t vertexIndex) = 0; 00391 00395 virtual void SetWindVelocity (const csVector3& velocity) = 0; 00396 00398 virtual const csVector3 GetWindVelocity () const = 0; 00399 00401 virtual void AddForce (const csVector3& force, size_t vertexIndex) = 0; 00402 00404 virtual size_t GetTriangleCount () = 0; 00405 00407 virtual csTriangle GetTriangle (size_t index) const = 0; 00408 00410 virtual csVector3 GetVertexNormal (size_t index) const = 0; 00411 }; 00412 00416 struct SoftBodyHelper 00417 { 00426 static csPtr<iMeshFactoryWrapper> CreateClothGenMeshFactory 00427 (iObjectRegistry* object_reg, const char* factoryName, iSoftBody* cloth) 00428 { 00429 csRef<iEngine> engine = csQueryRegistry<iEngine> (object_reg); 00430 00431 // Create the cloth mesh factory. 00432 csRef<iMeshFactoryWrapper> clothFact = engine->CreateMeshFactory 00433 ("crystalspace.mesh.object.genmesh", factoryName); 00434 if (!clothFact) 00435 return 0; 00436 00437 csRef<iGeneralFactoryState> gmstate = scfQueryInterface<iGeneralFactoryState> 00438 (clothFact->GetMeshObjectFactory ()); 00439 00440 // Create the vertices of the genmesh 00441 size_t vertexCount = cloth->GetVertexCount (); 00442 gmstate->SetVertexCount (int (vertexCount * 2)); 00443 csVector3* vertices = gmstate->GetVertices (); 00444 for (size_t i = 0; i < vertexCount; i++) 00445 { 00446 vertices[i] = cloth->GetVertexPosition (i); 00447 vertices[i + vertexCount] = cloth->GetVertexPosition (i); 00448 } 00449 00450 // Create the triangles of the genmesh 00451 gmstate->SetTriangleCount (int (cloth->GetTriangleCount ()) * 2); 00452 csTriangle* triangles = gmstate->GetTriangles (); 00453 for (size_t i = 0; i < cloth->GetTriangleCount (); i++) 00454 { 00455 csTriangle triangle = cloth->GetTriangle (i); 00456 triangles[i * 2] = triangle; 00457 triangles[i * 2 + 1] = csTriangle (int (triangle[2] + vertexCount), 00458 int (triangle[1] + vertexCount), 00459 int (triangle[0] + vertexCount)); 00460 } 00461 00462 gmstate->CalculateNormals (); 00463 00464 // Set up the texels of the genmesh 00465 csVector2* texels = gmstate->GetTexels (); 00466 csVector3* normals = gmstate->GetNormals (); 00467 CS::Geometry::TextureMapper* mapper = new CS::Geometry::DensityTextureMapper (1.0f); 00468 for (size_t i = 0; i < vertexCount * 2; i++) 00469 texels[i] = mapper->Map (vertices[i], normals[i], i); 00470 00471 gmstate->Invalidate (); 00472 00473 return csPtr<iMeshFactoryWrapper> (clothFact); 00474 } 00475 }; 00476 00489 struct iJoint : public virtual iBase 00490 { 00491 SCF_INTERFACE (CS::Physics::iJoint, 1, 0, 0); 00492 00497 virtual void Attach (iPhysicalBody* body1, iPhysicalBody* body2, 00498 bool forceUpdate = true) = 0; 00499 00501 virtual iPhysicalBody* GetAttachedBody (int index) = 0; 00502 00507 virtual void SetTransform (const csOrthoTransform& trans, 00508 bool forceUpdate = false) = 0; 00509 00511 virtual csOrthoTransform GetTransform () const = 0; 00512 00514 virtual void SetPosition (const csVector3& position, 00515 bool forceUpdate = false) = 0; 00516 00518 virtual csVector3 GetPosition () const = 0; 00519 00528 virtual void SetTransConstraints (bool X, 00529 bool Y, bool Z, 00530 bool forceUpdate = false) = 0; 00531 00533 virtual bool IsXTransConstrained () = 0; 00534 00536 virtual bool IsYTransConstrained () = 0; 00537 00539 virtual bool IsZTransConstrained () = 0; 00540 00545 virtual void SetMinimumDistance (const csVector3& dist, 00546 bool forceUpdate = false) = 0; 00547 00549 virtual csVector3 GetMinimumDistance () const = 0; 00550 00555 virtual void SetMaximumDistance (const csVector3& dist, 00556 bool forceUpdate = false) = 0; 00557 00559 virtual csVector3 GetMaximumDistance () const = 0; 00560 00569 virtual void SetRotConstraints (bool X, 00570 bool Y, bool Z, 00571 bool forceUpdate = false) = 0; 00572 00574 virtual bool IsXRotConstrained () = 0; 00575 00577 virtual bool IsYRotConstrained () = 0; 00578 00580 virtual bool IsZRotConstrained () = 0; 00581 00586 virtual void SetMinimumAngle (const csVector3& angle, 00587 bool forceUpdate = false) = 0; 00588 00590 virtual csVector3 GetMinimumAngle () const = 0; 00591 00596 virtual void SetMaximumAngle (const csVector3& angle, 00597 bool forceUpdate = false) = 0; 00598 00600 virtual csVector3 GetMaximumAngle () const = 0; 00601 00607 virtual void SetBounce (const csVector3& bounce, 00608 bool forceUpdate = false) = 0; 00609 00611 virtual csVector3 GetBounce () const = 0; 00612 00617 virtual void SetDesiredVelocity (const csVector3& velo, 00618 bool forceUpdate = false) = 0; 00619 00621 virtual csVector3 GetDesiredVelocity () const = 0; 00622 00627 virtual void SetMaxForce (const csVector3& force, 00628 bool forceUpdate = false) = 0; 00629 00631 virtual csVector3 GetMaxForce () const = 0; 00632 00637 virtual bool RebuildJoint () = 0; 00638 00640 virtual void SetSpring(bool isSpring, bool forceUpdate = false) = 0; 00641 00643 virtual void SetLinearStiffness (csVector3 stiff, bool forceUpdate = false) = 0; 00644 00646 virtual csVector3 GetLinearStiffness () const = 0; 00647 00649 virtual void SetAngularStiffness (csVector3 stiff, bool forceUpdate = false) = 0; 00650 00652 virtual csVector3 GetAngularStiffness () const = 0; 00653 00655 virtual void SetLinearDamping (csVector3 damp, bool forceUpdate = false) = 0; 00656 00658 virtual csVector3 GetLinearDamping () const = 0; 00659 00661 virtual void SetAngularDamping (csVector3 damp, bool forceUpdate = false) = 0; 00662 00664 virtual csVector3 GetAngularDamping () const = 0; 00665 00667 virtual void SetLinearEquilibriumPoint (csVector3 point, bool forceUpdate = false) = 0; 00668 00670 virtual void SetAngularEquilibriumPoint (csVector3 point, bool forceUpdate = false) = 0; 00671 00673 virtual void SetBreakingImpulseThreshold (float threshold, bool forceUpdate = false) = 0; 00674 00676 virtual float GetBreakingImpulseThreshold () = 0; 00677 }; 00678 00686 struct iKinematicCallback : public virtual iBase 00687 { 00688 SCF_INTERFACE (CS::Physics::iKinematicCallback, 1, 0, 0); 00689 00693 virtual void GetBodyTransform (iRigidBody* body, 00694 csOrthoTransform& transform) const = 0; 00695 }; 00696 00712 struct iPhysicalSystem : public virtual iBase 00713 { 00714 SCF_INTERFACE (CS::Physics::iPhysicalSystem, 1, 0, 0); 00715 00720 virtual csRef<iRigidBody> CreateRigidBody () = 0; 00721 00723 virtual csRef<iJoint> CreateJoint () = 0; 00724 00725 /* 00726 * Create a P2P joint for rigid bodies by setting the position in 00727 * world space. 00728 */ 00729 virtual csRef<iJoint> CreateRigidP2PJoint (const csVector3 position) = 0; 00730 00731 /* 00732 * Create a slide joint for rigid bodies. 00733 * \param trans The transform of the joint in world space. 00734 * \param minDist The min distance the body can move along the axis. 00735 * \param maxDist The max distance the body can move along the axis. 00736 * \param minAngle The min angle the body can rotate around the axis. 00737 * \param maxAngle The max angle the body can rotate around the axis. 00738 * \param axis The slide axis, can only be 0, 1, 2. 00739 */ 00740 virtual csRef<iJoint> CreateRigidSlideJoint (const csOrthoTransform trans, 00741 float minDist, float maxDist, float minAngle, float maxAngle, int axis) = 0; 00742 00743 /* 00744 * Create a hinge joint for rigid bodies. 00745 * \param position The position of the joint in world space. 00746 * \param minAngle The min angle the body can rotate around the axis. 00747 * \param maxAngle The max angle the body can rotate around the axis. 00748 * \param axis The axis of the hinge, can only be 0, 1, 2. 00749 */ 00750 virtual csRef<iJoint> CreateRigidHingeJoint (const csVector3 position, 00751 float minAngle, float maxAngle, int axis) = 0; 00752 00753 /* 00754 * Create a cone twist joint for rigid bodies. 00755 * \param trans The transform of the joint in world space. 00756 * \param swingSpan1 The swing span the body can rotate around the local Z axis of joint. 00757 * \param swingSpan2 The swing span the body can rotate around the local Y axis of joint. 00758 * \param twistSpan The twist span the body can rotate around the local X axis of joint. 00759 */ 00760 virtual csRef<iJoint> CreateRigidConeTwistJoint (const csOrthoTransform trans, 00761 float swingSpan1,float swingSpan2,float twistSpan) = 0; 00762 00763 /* 00764 * Create a linear joint for soft body by setting the position in 00765 * world space. 00766 */ 00767 virtual csRef<iJoint> CreateSoftLinearJoint (const csVector3 position) = 0; 00768 00769 /* 00770 * Create a angular joint for soft body by setting the rotation axis. 00771 * The axis can only be 0, 1, 2. 00772 */ 00773 virtual csRef<iJoint> CreateSoftAngularJoint (int axis) = 0; 00774 00775 /* 00776 * Create a pivot joint to attach to a rigid body to a position in world space 00777 * in order to manipulate it. 00778 */ 00779 virtual csRef<iJoint> CreateRigidPivotJoint (iRigidBody* body, const csVector3 position) = 0; 00780 00788 virtual csRef<iSoftBody> CreateRope (csVector3 start, 00789 csVector3 end, size_t segmentCount) = 0; 00790 00797 virtual csRef<iSoftBody> CreateRope (csVector3* vertices, size_t vertexCount) = 0; 00798 00811 virtual csRef<iSoftBody> CreateCloth (csVector3 corner1, csVector3 corner2, 00812 csVector3 corner3, csVector3 corner4, 00813 size_t segmentCount1, size_t segmentCount2, 00814 bool withDiagonals = false) = 0; 00815 00822 virtual csRef<iSoftBody> CreateSoftBody (iGeneralFactoryState* genmeshFactory, 00823 const csOrthoTransform& bodyTransform) = 0; 00824 00835 virtual csRef<iSoftBody> CreateSoftBody (csVector3* vertices, 00836 size_t vertexCount, csTriangle* triangles, 00837 size_t triangleCount, const csOrthoTransform& bodyTransform) = 0; 00838 }; 00839 00849 struct iPhysicalSector : public virtual iBase 00850 { 00851 SCF_INTERFACE (CS::Physics::iPhysicalSector, 1, 0, 0); 00852 00857 virtual void SetSimulationSpeed (float speed) = 0; 00858 00874 virtual void SetStepParameters (float timeStep, size_t maxSteps, 00875 size_t iterations) = 0; 00876 00878 virtual void Step (float duration) = 0; 00879 00889 virtual void SetLinearDampener (float d) = 0; 00890 00894 virtual float GetLinearDampener () const = 0; 00895 00905 virtual void SetRollingDampener (float d) = 0; 00906 00908 virtual float GetRollingDampener () const = 0; 00909 00922 virtual void SetAutoDisableParams (float linear, float angular, 00923 float time) = 0; 00924 00929 virtual void AddRigidBody (iRigidBody* body) = 0; 00930 00932 virtual void RemoveRigidBody (iRigidBody* body) = 0; 00933 00935 virtual size_t GetRigidBodyCount () = 0; 00936 00938 virtual iRigidBody* GetRigidBody (size_t index) = 0; 00939 00941 virtual iRigidBody* FindRigidBody (const char* name) = 0; 00942 00947 virtual void AddSoftBody (iSoftBody* body) = 0; 00948 00950 virtual void RemoveSoftBody (iSoftBody* body) = 0; 00951 00953 virtual size_t GetSoftBodyCount () = 0; 00954 00956 virtual iSoftBody* GetSoftBody (size_t index) = 0; 00957 00959 virtual iSoftBody* FindSoftBody (const char* name) = 0; 00960 00962 virtual void AddJoint (iJoint* joint) = 0; 00963 00965 virtual void RemoveJoint (iJoint* joint) = 0; 00966 00972 virtual void SetSoftBodyEnabled (bool enabled) = 0; 00973 00977 virtual bool GetSoftBodyEnabled () = 0; 00978 }; 00979 00990 struct iSoftBodyAnimationControlType : public iGenMeshAnimationControlType 00991 { 00992 SCF_INTERFACE (CS::Physics::iSoftBodyAnimationControlType, 1, 0, 0); 00993 }; 00994 01007 struct iSoftBodyAnimationControlFactory : public iGenMeshAnimationControlFactory 01008 { 01009 SCF_INTERFACE (CS::Physics::iSoftBodyAnimationControlFactory, 1, 0, 0); 01010 }; 01011 01030 struct iSoftBodyAnimationControl : public iGenMeshAnimationControl 01031 { 01032 SCF_INTERFACE (CS::Physics::iSoftBodyAnimationControl, 1, 0, 0); 01033 01043 virtual void SetSoftBody (iSoftBody* body, bool doubleSided = false) = 0; 01044 01048 virtual iSoftBody* GetSoftBody () = 0; 01049 01074 virtual void CreateAnimatedMeshAnchor (CS::Mesh::iAnimatedMesh* animesh, 01075 iRigidBody* body, 01076 size_t bodyVertexIndex, 01077 size_t animeshVertexIndex = (size_t) ~0) = 0; 01078 01082 virtual size_t GetAnimatedMeshAnchorVertex (size_t bodyVertexIndex) = 0; 01083 01088 virtual void RemoveAnimatedMeshAnchor (size_t bodyVertexIndex) = 0; 01089 }; 01090 } 01091 } 01092 #endif
Generated for Crystal Space 2.1 by doxygen 1.6.1
