Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

PhysicsSystem.h File Reference

Go to the source code of this file.

Defines

#define PHYSICSSYSTEM_H

Typedefs

typedef gePhysicsSystem gePhysicsSystem

Functions

GENESISAPI gePhysicsSystem
*GENESISCC 
gePhysicsSystem_Create (void)
GENESISAPI geBoolean GENESISCC gePhysicsSystem_Destroy (gePhysicsSystem **ppSys)
GENESISAPI geBoolean GENESISCC gePhysicsSystem_Iterate (gePhysicsSystem *psPtr, geFloat Time)
GENESISAPI geBoolean GENESISCC gePhysicsSystem_AddJoint (gePhysicsSystem *psPtr, gePhysicsJoint *Joint)
GENESISAPI geBoolean GENESISCC gePhysicsSystem_AddObject (gePhysicsSystem *psPtr, gePhysicsObject *Object)
GENESISAPI int GENESISCC gePhysicsSystem_GetSourceConfigIndex (const gePhysicsSystem *pSys)
GENESISAPI gePhysicsObject
**GENESISCC 
gePhysicsSystem_GetPhysobs (const gePhysicsSystem *pSys)
GENESISAPI gePhysicsJoint
**GENESISCC 
gePhysicsSystem_GetPhysjnts (const gePhysicsSystem *pSys)
GENESISAPI int GENESISCC gePhysicsSystem_GetNumPhysobs (const gePhysicsSystem *pSys)
GENESISAPI int GENESISCC gePhysicsSystem_GetNumPhysjnts (const gePhysicsSystem *pSys)
GENESISAPI int GENESISCC gePhysicsSystem_GetSumOfConstraintDimensions (const gePhysicsSystem *pSys)


Define Documentation

#define PHYSICSSYSTEM_H
 

Definition at line 23 of file PhysicsSystem.h.


Typedef Documentation

typedef struct gePhysicsSystem gePhysicsSystem
 

Definition at line 29 of file PhysicsSystem.h.


Function Documentation

GENESISAPI geBoolean GENESISCC gePhysicsSystem_AddJoint gePhysicsSystem psPtr,
gePhysicsJoint Joint
 

Definition at line 102 of file PhysicsSystem.c.

References GE_FALSE, GE_RAM_ALLOCATE_STRUCT, GE_TRUE, geBoolean, geFloat, GENESISAPI, GENESISCC, gePhysicsJoint_GetType(), gePhysicsJoint_Kind, geRam_Allocate, geRam_Free, geRam_Realloc, gePhysicsSystem::Joints, JT_SPHERICAL, JT_WORLD, LinearSystemStruct::KVector, gePhysicsSystem::linsys, LinearSystemStruct::M, NULL, gePhysicsSystem::PhysicsJointCount, gePhysicsSystem::sumOfConstraintDimensions, type, and LinearSystemStruct::X.

Referenced by PhysicalSystem_Spawn().

00103 {
00104         gePhysicsJoint **       NewList;
00105         int                                     i;
00106         gePhysicsJoint_Kind type;
00107         assert( PS != NULL );
00108         assert( Joint != NULL );
00109 
00110         NewList = geRam_Realloc(PS->Joints, sizeof(*NewList) * (PS->PhysicsJointCount + 1));
00111         if      (!NewList)
00112                 return GE_FALSE;
00113 
00114         NewList[PS->PhysicsJointCount] = Joint;
00115         PS->PhysicsJointCount++;
00116         PS->Joints = NewList;
00117 
00118         // Free any old data
00119         if      (PS->linsys)
00120         {
00121                 assert(PS->linsys->M);
00122                 assert(PS->linsys->X);
00123                 assert(PS->linsys->KVector);
00124 
00125                 for (i = 0; i < PS->sumOfConstraintDimensions; i++)
00126                 {
00127                         assert(PS->linsys->M[i]);
00128                         geRam_Free(PS->linsys->M[i]);
00129                 }
00130 
00131                 geRam_Free(PS->linsys->M);
00132                 geRam_Free(PS->linsys->X);
00133                 geRam_Free(PS->linsys->KVector);
00134                 geRam_Free(PS->linsys);
00135         }
00136 
00137         PS->linsys = GE_RAM_ALLOCATE_STRUCT(LinearSystemStruct);
00138         if (PS->linsys == NULL)
00139         {
00140                 return GE_FALSE;
00141         }
00142 
00144         // compute size of linear system
00145 
00146         PS->sumOfConstraintDimensions = 0;
00147         for (   i = 0; i < PS->PhysicsJointCount; i++)
00148         {
00149                 type = gePhysicsJoint_GetType(NewList[i]);
00150                 switch (type)
00151                 {
00152                         case JT_WORLD:
00153                         case JT_SPHERICAL:
00154                                 PS->sumOfConstraintDimensions += 3;
00155                                 break;
00156 
00157                         default:
00158                                 // shouldn't happen !
00159                                 assert(!"Illegal joint kind");
00160                                 return GE_FALSE;
00161                 }
00162         }
00163 
00164         if (PS->sumOfConstraintDimensions == 0)
00165                 return GE_FALSE;
00166 
00168         // alloc mem for the linear system and handle exceptions
00169 
00170         PS->linsys->M = (geFloat**)geRam_Allocate(PS->sumOfConstraintDimensions * sizeof(geFloat*));
00171 
00172         if (PS->linsys->M == NULL)
00173         {
00174                 return GE_FALSE;
00175         }
00176 
00177         for (i = 0; i < PS->sumOfConstraintDimensions; i++)
00178         {
00179                 PS->linsys->M[i] = (geFloat*)geRam_Allocate(PS->sumOfConstraintDimensions * sizeof(geFloat));
00180 
00181                 if (PS->linsys->M[i] == NULL)
00182                 {
00183                         return GE_FALSE;
00184                 }
00185         }
00186 
00187         PS->linsys->X = (geFloat*)geRam_Allocate(PS->sumOfConstraintDimensions * sizeof(geFloat));
00188 
00189         if (PS->linsys->X == NULL)
00190         {
00191                 return GE_FALSE;
00192         }
00193 
00194         PS->linsys->KVector = (geFloat*)geRam_Allocate(PS->sumOfConstraintDimensions * sizeof(geFloat));
00195 
00196         if (PS->linsys->KVector == NULL)
00197         {
00198                 return GE_FALSE;
00199         }       
00200 
00201         return GE_TRUE;
00202 }

GENESISAPI geBoolean GENESISCC gePhysicsSystem_AddObject gePhysicsSystem psPtr,
gePhysicsObject Object
 

Definition at line 86 of file PhysicsSystem.c.

References GE_FALSE, GE_TRUE, geBoolean, GENESISAPI, GENESISCC, geRam_Realloc, NULL, gePhysicsSystem::Objects, and gePhysicsSystem::PhysicsObjectCount.

Referenced by PhysicalSystem_Spawn().

00087 {
00088         gePhysicsObject **      NewList;
00089         assert( PS != NULL );
00090 
00091         NewList = geRam_Realloc(PS->Objects, sizeof(*NewList) * (PS->PhysicsObjectCount + 1));
00092         if      (!NewList)
00093                 return GE_FALSE;
00094 
00095         NewList[PS->PhysicsObjectCount] = Object;
00096         PS->PhysicsObjectCount++;
00097         PS->Objects = NewList;
00098 
00099         return GE_TRUE;
00100 }

GENESISAPI gePhysicsSystem* GENESISCC gePhysicsSystem_Create void   ) 
 

Definition at line 65 of file PhysicsSystem.c.

References GE_RAM_ALLOCATE_STRUCT, GENESISAPI, GENESISCC, gePhysicsSystemIdentityMatrix, Matrix33_SetIdentity(), NULL, gePhysicsSystem::sourceConfigIndex, and gePhysicsSystem::targetConfigIndex.

Referenced by PhysicalSystem_Spawn().

00066 {
00067         gePhysicsSystem* pPhyssys;
00068 
00069         pPhyssys = NULL;
00070         pPhyssys = GE_RAM_ALLOCATE_STRUCT(gePhysicsSystem);
00071         if (pPhyssys == NULL)
00072         {
00073                 return NULL;
00074         }       
00075 
00076         memset(pPhyssys, 0, sizeof(*pPhyssys));
00077 
00078         Matrix33_SetIdentity(&gePhysicsSystemIdentityMatrix);
00079 
00080         pPhyssys->sourceConfigIndex = 0;
00081         pPhyssys->targetConfigIndex = 1;
00082 
00083         return pPhyssys;
00084 }

GENESISAPI geBoolean GENESISCC gePhysicsSystem_Destroy gePhysicsSystem **  ppSys  ) 
 

Definition at line 204 of file PhysicsSystem.c.

References GE_TRUE, geBoolean, GENESISAPI, GENESISCC, geRam_Free, gePhysicsSystem::Joints, LinearSystemStruct::KVector, gePhysicsSystem::linsys, LinearSystemStruct::M, NULL, gePhysicsSystem::Objects, gePhysicsSystem::sumOfConstraintDimensions, and LinearSystemStruct::X.

Referenced by PhysicalSystem_Destroy().

00205 {
00206         gePhysicsSystem *pPhyssys;
00207         int                             i;
00208 
00209         pPhyssys = *ppPhyssys;
00210 
00211         geRam_Free(pPhyssys->Objects);
00212         geRam_Free(pPhyssys->Joints);
00213 
00214         // Free any old data
00215         if      (pPhyssys->linsys)
00216         {
00217                 assert(pPhyssys->linsys->M);
00218                 assert(pPhyssys->linsys->X);
00219                 assert(pPhyssys->linsys->KVector);
00220 
00221                 for (i = 0; i < pPhyssys->sumOfConstraintDimensions; i++)
00222                 {
00223                         assert(pPhyssys->linsys->M[i]);
00224                         geRam_Free(pPhyssys->linsys->M[i]);
00225                 }
00226 
00227                 geRam_Free(pPhyssys->linsys->M);
00228                 geRam_Free(pPhyssys->linsys->X);
00229                 geRam_Free(pPhyssys->linsys->KVector);
00230         }
00231 
00232         geRam_Free(pPhyssys->linsys);
00233         geRam_Free(*ppPhyssys);
00234         *ppPhyssys = NULL;
00235 
00236         return GE_TRUE;
00237 }

GENESISAPI int GENESISCC gePhysicsSystem_GetNumPhysjnts const gePhysicsSystem pSys  ) 
 

GENESISAPI int GENESISCC gePhysicsSystem_GetNumPhysobs const gePhysicsSystem pSys  ) 
 

GENESISAPI gePhysicsJoint** GENESISCC gePhysicsSystem_GetPhysjnts const gePhysicsSystem pSys  ) 
 

GENESISAPI gePhysicsObject** GENESISCC gePhysicsSystem_GetPhysobs const gePhysicsSystem pSys  ) 
 

GENESISAPI int GENESISCC gePhysicsSystem_GetSourceConfigIndex const gePhysicsSystem pSys  ) 
 

Definition at line 800 of file PhysicsSystem.c.

References GENESISAPI, GENESISCC, NULL, and gePhysicsSystem::sourceConfigIndex.

00801 {
00802         assert(pSys != NULL);
00803 
00804         return pSys->sourceConfigIndex;
00805 }

GENESISAPI int GENESISCC gePhysicsSystem_GetSumOfConstraintDimensions const gePhysicsSystem pSys  ) 
 

Definition at line 835 of file PhysicsSystem.c.

References GENESISAPI, GENESISCC, NULL, and gePhysicsSystem::sumOfConstraintDimensions.

00836 {
00837         assert(pSys != NULL);
00838 
00839         return pSys->sumOfConstraintDimensions;
00840 }

GENESISAPI geBoolean GENESISCC gePhysicsSystem_Iterate gePhysicsSystem psPtr,
geFloat  Time
 

Definition at line 247 of file PhysicsSystem.c.

References GE_FALSE, GE_TRUE, geBoolean, geFloat, GENESISAPI, GENESISCC, gePhysicsObject_ClearAppliedForce(), gePhysicsObject_ClearAppliedTorque(), gePhysicsObject_ComputeForces(), gePhysicsObject_Integrate(), gePhysicsObject_SetActiveConfig(), gePhysicsSystem_EnforceConstraints(), NULL, gePhysicsSystem::Objects, gePhysicsSystem::PhysicsJointCount, gePhysicsSystem::PhysicsObjectCount, gePhysicsSystem::sourceConfigIndex, gePhysicsSystem::sumOfConstraintDimensions, and gePhysicsSystem::targetConfigIndex.

Referenced by PhysicalSystem_Control().

00248 {
00249         int                             i;
00250 
00251         int numIntegrationSteps;
00252         geFloat minAssemblyRate, subStepSize;
00253         geFloat amountIntegrated = 0.f;
00254 
00255         assert( psPtr != NULL );
00256 
00258         // integrate numIntegrationSteps times during the frame
00259         // this is done to ensure smoother motion and enforce constraint stability
00260 
00261         minAssemblyRate = FLT_MAX;
00262 
00263         if (psPtr->PhysicsJointCount == 0)
00264         {
00265                 numIntegrationSteps = 1;
00266         }
00267 
00268         else
00269                 numIntegrationSteps = 5;
00270 
00271         if (Time > 0.03f) Time = 0.03f;
00272 
00273         subStepSize = Time / numIntegrationSteps;
00274 
00275         for (   amountIntegrated = 0.f;
00276                                 amountIntegrated < Time;
00277                                 amountIntegrated += subStepSize)
00278         {
00279                 for     (i = 0; i < psPtr->PhysicsObjectCount; i++)
00280                 {
00281                         if (!gePhysicsObject_ComputeForces(psPtr->Objects[i], psPtr->sourceConfigIndex))
00282                                 return GE_FALSE;
00283                 }                       
00284 
00286                 // enforce constraints
00287 
00288                 if (psPtr->sumOfConstraintDimensions > 0)
00289                 {
00290                         if (!gePhysicsSystem_EnforceConstraints(psPtr, subStepSize))
00291                                 return GE_FALSE;
00292                 }
00293 
00294                 for     (i = 0; i < psPtr->PhysicsObjectCount; i++)
00295                 {
00296                         if (!gePhysicsObject_Integrate(psPtr->Objects[i], subStepSize, psPtr->sourceConfigIndex))
00297                                 return GE_FALSE;                                                        
00298                 }
00299 
00300                 psPtr->sourceConfigIndex = (psPtr->sourceConfigIndex == 0 ? 1 : 0);
00301                 psPtr->targetConfigIndex = (psPtr->targetConfigIndex == 0 ? 1 : 0);
00302                 
00303                 // let physical object's control fns update themselves          
00304         }
00305 
00306         for     (i = 0; i < psPtr->PhysicsObjectCount; i++)
00307         {
00308                 gePhysicsObject* pod;           
00309                 
00310                 pod = psPtr->Objects[i];
00311                 
00312                 gePhysicsObject_ClearAppliedForce(pod, psPtr->sourceConfigIndex);
00313                 gePhysicsObject_ClearAppliedTorque(pod, psPtr->sourceConfigIndex);
00314                 gePhysicsObject_SetActiveConfig(psPtr->Objects[i], psPtr->sourceConfigIndex);
00315         }
00316 
00317         return GE_TRUE;
00318 }


Generated on Tue Sep 30 12:37:54 2003 for GTestAndEngine by doxygen 1.3.2