ivaria/bullet.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

ivaria/bullet.h
Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2007 by Jorrit Tyberghein
    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 __CS_IVARIA_BULLET_H__
    00020 #define __CS_IVARIA_BULLET_H__
    00021
    00026 #include "csutil/scf_interface.h"
    00027 #include "iutil/objreg.h"
    00028 #include "iengine/mesh.h"
    00029 #include "iengine/engine.h"
    00030 #include "imesh/genmesh.h"
    00031 #include "csgeom/tri.h"
    00032 #include "cstool/primitives.h"
    00033
    00034 struct iView;
    00035 struct iRigidBody;
    00036 struct csLockedHeightData;
    00037 struct iTerrainCell;
    00038 struct iTerrainSystem;
    00039
    00040 namespace CS
    00041 {
    00042 namespace Physics
    00043 {
    00044 namespace Bullet
    00045 {
    00046
    00047 struct iKinematicCallback;
    00048 struct iSoftBody;
    00049 struct iPivotJoint;
    00050 struct iTerrainCollider;
    00051
    00055 enum BodyType
    00056 {
    00057   UNDEFINED_BODY = 0,
    00058   RIGID_BODY,
    00059   SOFT_BODY,
    00060   TERRAIN
    00061 };
    00062
    00066 struct iBody : public virtual iBase
    00067 {
    00068   SCF_INTERFACE(CS::Physics::Bullet::iBody, 1, 0, 0);
    00069
    00071   virtual BodyType GetType () const = 0;
    00072
    00077   virtual ::iRigidBody* QueryRigidBody () = 0;
    00078
    00083   virtual iSoftBody* QuerySoftBody () = 0;
    00084
    00089   virtual iTerrainCollider* QueryTerrainCollider () = 0;
    00090 };
    00091
    00097 struct HitBeamResult
    00098 {
    00099   HitBeamResult ()
    00100   : hasHit (false), body (0), isect (0.0f), normal (0.0f), vertexIndex (0)
    00101   {}
    00102
    00106   bool hasHit;
    00107
    00111   iBody* body;
    00112
    00116   csVector3 isect;
    00117
    00121   csVector3 normal;
    00122
    00127   size_t vertexIndex;
    00128 };
    00129
    00133 enum DebugMode
    00134 {
    00135   DEBUG_NOTHING = 0,
    00136   DEBUG_COLLIDERS = 1,
    00137   DEBUG_AABB = 2,
    00138   DEBUG_JOINTS = 4
    00139 };
    00140
    00145 enum MeshDuplicationMode
    00146 {
    00147   MESH_DUPLICATION_NONE = 0,
    00149   MESH_DUPLICATION_INTERLEAVED,
    00151   MESH_DUPLICATION_CONTIGUOUS
    00153 };
    00154
    00160 struct iDynamicSystem : public virtual iBase
    00161 {
    00162   SCF_INTERFACE(CS::Physics::Bullet::iDynamicSystem, 4, 0, 0);
    00163
    00169   virtual void DebugDraw (iView* rview) = 0;
    00170
    00177   virtual HitBeamResult HitBeam (const csVector3 &start,
    00178                                  const csVector3 &end) = 0;
    00179
    00192   virtual void SetInternalScale (float scale) = 0;
    00193
    00209   virtual void SetStepParameters (float timeStep, size_t maxSteps,
    00210                                   size_t iterations) = 0;
    00211
    00218   virtual void SetDebugMode (DebugMode mode) = 0;
    00219
    00223   virtual DebugMode GetDebugMode () = 0;
    00224
    00230   virtual void SetSoftBodyWorld (bool isSoftBodyWorld) = 0;
    00231
    00235   virtual bool GetSoftBodyWorld () = 0;
    00236
    00240   virtual size_t GetSoftBodyCount () = 0;
    00241
    00245   virtual iSoftBody* GetSoftBody (size_t index) = 0;
    00246
    00254   virtual iSoftBody* CreateRope (csVector3 start, csVector3 end,
    00255                                  uint segmentCount) = 0;
    00256
    00263   virtual iSoftBody* CreateRope (csVector3* vertices, size_t vertexCount) = 0;
    00264
    00277   virtual iSoftBody* CreateCloth (csVector3 corner1, csVector3 corner2,
    00278                                   csVector3 corner3, csVector3 corner4,
    00279                                   uint segmentCount1, uint segmentCount2,
    00280                                   bool withDiagonals = false) = 0;
    00281
    00290   virtual iSoftBody* CreateSoftBody (iGeneralFactoryState* genmeshFactory,
    00291                                      const csOrthoTransform& bodyTransform,
    00292                                      MeshDuplicationMode duplicationMode = MESH_DUPLICATION_NONE) = 0;
    00293
    00302   virtual iSoftBody* CreateSoftBody (csVector3* vertices, size_t vertexCount,
    00303                                      csTriangle* triangles, size_t triangleCount) = 0;
    00304
    00308   virtual void RemoveSoftBody (iSoftBody* body) = 0;
    00309
    00313   virtual csPtr<iPivotJoint> CreatePivotJoint () = 0;
    00314
    00318   virtual void RemovePivotJoint (iPivotJoint* joint) = 0;
    00319
    00324   virtual bool SaveBulletWorld (const char* filename) = 0;
    00325
    00339   virtual iTerrainCollider* AttachColliderTerrain (csLockedHeightData& heightData,
    00340                                                    int gridWidth, int gridHeight,
    00341                                                    csVector3 gridSize,
    00342                                                    csOrthoTransform& transform,
    00343                                                    float minimumHeight = 0,
    00344                                                    float maximumHeight = 0) = 0;
    00345
    00355   virtual iTerrainCollider* AttachColliderTerrain (iTerrainCell* cell,
    00356                                                    float minimumHeight = 0,
    00357                                                    float maximumHeight = 0) = 0;
    00358
    00371   virtual iTerrainCollider* AttachColliderTerrain (iTerrainSystem* terrain,
    00372                                                    float minimumHeight = 0,
    00373                                                    float maximumHeight = 0) = 0;
    00377   virtual void DestroyCollider (iTerrainCollider* collider) = 0;
    00378
    00384   virtual void StartProfile () = 0;
    00385
    00389   virtual void StopProfile () = 0;
    00390
    00397   virtual void DumpProfile (bool resetProfile = true) = 0;
    00398 };
    00399
    00409 struct iAnchorAnimationControl : public virtual iBase
    00410 {
    00411   SCF_INTERFACE(CS::Physics::Bullet::iAnchorAnimationControl, 1, 0, 0);
    00412
    00416   virtual csVector3 GetAnchorPosition () const = 0;
    00417 };
    00418
    00431 struct iSoftBody : public iBody
    00432 {
    00433   SCF_INTERFACE(CS::Physics::Bullet::iSoftBody, 2, 0, 2);
    00434
    00439   virtual void DebugDraw (iView* rview) = 0;
    00440
    00444   virtual void SetMass (float mass) = 0;
    00445
    00449   virtual float GetMass () const = 0;
    00450
    00454   virtual size_t GetVertexCount () const = 0;
    00455
    00459   virtual csVector3 GetVertexPosition (size_t index) const = 0;
    00460
    00464   virtual void AnchorVertex (size_t vertexIndex) = 0;
    00465
    00470   virtual void AnchorVertex (size_t vertexIndex, iRigidBody* body) = 0;
    00471
    00476   virtual void AnchorVertex (size_t vertexIndex, iAnchorAnimationControl* controller) = 0;
    00477
    00490   virtual void UpdateAnchor (size_t vertexIndex, csVector3& position) = 0;
    00491
    00496   virtual void RemoveAnchor (size_t vertexIndex) = 0;
    00497
    00502   virtual void SetRigidity (float rigidity) = 0;
    00503
    00507   virtual float GetRigidity () const = 0;
    00508
    00512   virtual void SetLinearVelocity (csVector3 velocity) = 0;
    00513
    00517   virtual void SetLinearVelocity (csVector3 velocity, size_t vertexIndex) = 0;
    00518
    00522   virtual csVector3 GetLinearVelocity (size_t vertexIndex) const = 0;
    00523
    00527   virtual void AddForce (csVector3 force) = 0;
    00528
    00532   virtual void AddForce (csVector3 force, size_t vertexIndex) = 0;
    00533
    00537   virtual size_t GetTriangleCount () const = 0;
    00538
    00542   virtual csTriangle GetTriangle (size_t index) const = 0;
    00543
    00547   virtual csVector3 GetVertexNormal (size_t index) const = 0;
    00548
    00556   virtual void GenerateBendingConstraints (size_t distance) = 0;
    00557 };
    00558
    00562 struct SoftBodyHelper
    00563 {
    00572   static csPtr<iMeshFactoryWrapper> CreateClothGenMeshFactory
    00573   (iObjectRegistry* object_reg, const char* factoryName, iSoftBody* cloth)
    00574   {
    00575     csRef<iEngine> engine = csQueryRegistry<iEngine> (object_reg);
    00576
    00577     // Create the cloth mesh factory.
    00578     csRef<iMeshFactoryWrapper> clothFact = engine->CreateMeshFactory
    00579       ("crystalspace.mesh.object.genmesh", factoryName);
    00580     if (!clothFact)
    00581       return 0;
    00582
    00583     csRef<iGeneralFactoryState> gmstate = scfQueryInterface<iGeneralFactoryState>
    00584       (clothFact->GetMeshObjectFactory ());
    00585
    00586     // Create the vertices of the genmesh
    00587     size_t vertexCount = cloth->GetVertexCount ();
    00588     gmstate->SetVertexCount (vertexCount * 2);
    00589     csVector3* vertices = gmstate->GetVertices ();
    00590     for (size_t i = 0; i < vertexCount; i++)
    00591     {
    00592       vertices[i] = cloth->GetVertexPosition (i);
    00593       vertices[i + vertexCount] = cloth->GetVertexPosition (i);
    00594     }
    00595
    00596     // Create the triangles of the genmesh
    00597     gmstate->SetTriangleCount (cloth->GetTriangleCount () * 2);
    00598     csTriangle* triangles = gmstate->GetTriangles ();
    00599     for (size_t i = 0; i < cloth->GetTriangleCount (); i++)
    00600     {
    00601       csTriangle triangle = cloth->GetTriangle (i);
    00602       triangles[i * 2] = triangle;
    00603       triangles[i * 2 + 1] = csTriangle (triangle[2] + vertexCount,
    00604                                          triangle[1] + vertexCount,
    00605                                          triangle[0] + vertexCount);
    00606     }
    00607
    00608     gmstate->CalculateNormals ();
    00609
    00610     // Set up the texels of the genmesh
    00611     csVector2* texels = gmstate->GetTexels ();
    00612     csVector3* normals = gmstate->GetNormals ();
    00613     CS::Geometry::TextureMapper* mapper = new CS::Geometry::DensityTextureMapper (1.0f);
    00614     for (size_t i = 0; i < vertexCount * 2; i++)
    00615       texels[i] = mapper->Map (vertices[i], normals[i], i);
    00616
    00617     gmstate->Invalidate ();
    00618
    00619     return csPtr<iMeshFactoryWrapper> (clothFact);
    00620   }
    00621 };
    00622
    00626 enum BodyState
    00627 {
    00628   STATE_STATIC = 0,
    00630   STATE_DYNAMIC,
    00632   STATE_KINEMATIC
    00635 };
    00636
    00642 struct iRigidBody : public iBody
    00643 {
    00644   SCF_INTERFACE(CS::Physics::Bullet::iRigidBody, 1, 0, 1);
    00645
    00654   virtual void MakeKinematic () = 0;
    00655
    00659   virtual BodyState GetDynamicState () const = 0;
    00660
    00665   virtual void SetDynamicState (BodyState state) = 0;
    00666
    00671   virtual void SetKinematicCallback (iKinematicCallback* callback) = 0;
    00672
    00676   virtual iKinematicCallback* GetKinematicCallback () = 0;
    00677
    00687   virtual void SetLinearDampener (float d) = 0;
    00688
    00692   virtual float GetLinearDampener () const = 0;
    00693
    00703   virtual void SetRollingDampener (float d) = 0;
    00704
    00708   virtual float GetRollingDampener () const = 0;
    00709 };
    00710
    00719 struct iKinematicCallback : public virtual iBase
    00720 {
    00721   SCF_INTERFACE (CS::Physics::Bullet::iKinematicCallback, 1, 0, 0);
    00722
    00726   virtual void GetBodyTransform (::iRigidBody* body,
    00727                                  csOrthoTransform& transform) const = 0;
    00728 };
    00729
    00735 struct iPivotJoint : public virtual iBase
    00736 {
    00737   SCF_INTERFACE (CS::Physics::Bullet::iPivotJoint, 1, 0, 0);
    00738
    00744   virtual void Attach (::iRigidBody* body, const csVector3& position) = 0;
    00745
    00749   virtual iRigidBody* GetAttachedBody () const = 0;
    00750
    00754   virtual void SetPosition (const csVector3& position) = 0;
    00755
    00759   virtual csVector3 GetPosition () const = 0;
    00760 };
    00761
    00765 struct iTerrainCollider : public iBody
    00766 {
    00767   SCF_INTERFACE (CS::Physics::Bullet::iTerrainCollider, 1, 0, 0);
    00768
    00769 };
    00770
    00771 } //namespace Bullet
    00772 } //namespace Physics
    00773 } //namespace CS
    00774
    00775 CS_DEPRECATED_METHOD_MSG("Use CS::Physics::Bullet::iDynamicSystem instead")
    00776 typedef CS::Physics::Bullet::iDynamicSystem iBulletDynamicSystem;
    00777
    00778 #endif // __CS_IVARIA_BULLET_H__
    00779