00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00039 #include "../include/IPSA/SoPhysicsBox.h"
00040 #include "../include/IPSA/SoCollisionShapeBox.h"
00041 #include "../include/IPSA/SoPhysics.h"
00042 #include <Inventor/nodes/SoCube.h>
00043
00044 SO_KIT_SOURCE(SoPhysicsBox);
00045
00048 void SoPhysicsBox::initClass()
00049 {
00050 SO_KIT_INIT_CLASS(SoPhysicsBox, SoPhysics, "Physics");
00051 }
00052
00057 SoPhysicsBox::SoPhysicsBox(const SbString& nodeName )
00058 : SoPhysics(nodeName)
00059 {
00060 SO_KIT_CONSTRUCTOR(SoPhysicsBox);
00061 isBuiltIn = TRUE;
00062
00063 SO_KIT_ADD_FIELD(dimension,(1.0f, 1.0f, 1.0f));
00064
00065 SO_KIT_CHANGE_ENTRY_TYPE(visualisationShape, SoNode, SoCube);
00066 SO_KIT_CHANGE_ENTRY_TYPE(collisionShape, SoCollisionShape, SoCollisionShapeBox);
00067
00068 SO_KIT_CHANGE_NULL_BY_DEFAULT(visualisationShape, FALSE);
00069 SO_KIT_CHANGE_NULL_BY_DEFAULT(collisionShape, FALSE);
00070
00071 SO_KIT_INIT_INSTANCE();
00072
00073 updateDependentObjectsTrigger.appendConnection(&dimension);
00074 }
00075
00076
00082 dMass SoPhysicsBox::getInitialMass() {
00083 const SbVec3f scaling = this->scale.getValue();
00084 const float m = this->mass.getValue();
00085 const SbVec3f dim = this->dimension.getValue();
00086
00087 float x = scaling[0] * dim[0];
00088 float y = scaling[1] * dim[1];
00089 float z = scaling[2] * dim[2];
00090 inertia.enableNotify(false);
00091 float a = (m/12.0f) * ((y*y) + (z*z));
00092 float b = (m/12.0f) * ((x*x) + (z*z));
00093 float c = (m/12.0f) * ((y*y) + (x*x));
00094 this->inertia.set1Value(0,a,0,0);
00095 this->inertia.set1Value(1,0,b,0);
00096 this->inertia.set1Value(2,0,0,c);
00097 inertia.enableNotify(true);
00098 SbVec3f i0 = this->inertia[0];
00099 SbVec3f i1 = this->inertia[1];
00100 SbVec3f i2 = this->inertia[2];
00101
00102 dMass mass;
00103 dMassSetParameters(&mass,m,0,0,0,i0[0],i1[1],i2[2],i0[1],i0[2],i1[2]);
00104
00105 return mass;
00106 }
00107
00108
00114 void SoPhysicsBox::updateDependentObjects()
00115 {
00116 const SbVec3f DIMENSION_VALUES = dimension.getValue();
00117
00118
00119 SoCube* visShape = dynamic_cast<SoCube*> (this->getPart("visualisationShape", FALSE));
00120 if (NULL != visShape)
00121 {
00122
00123 visShape->width.setValue(DIMENSION_VALUES[0]);
00124 visShape->height.setValue(DIMENSION_VALUES[1]);
00125 visShape->depth.setValue(DIMENSION_VALUES[2]);
00126 }
00127
00128 if (FALSE == AUTO_UPDATE_COLLISION_SHAPE.getValue())
00129 return;
00130
00131
00132 SoCollisionShapeBox* cShape = dynamic_cast<SoCollisionShapeBox*> (this->getPart("collisionShape", FALSE));
00133 if (NULL != cShape)
00134 {
00135
00136
00137 cShape->dimension.setValue(DIMENSION_VALUES);
00138 }
00139 }