00001 #include "PhysicalObject.h" 00002 #include "Simulator.h" 00003 #include "Integrator.h" 00004 #include "purgeContainer.h" 00005 #include "ContactInfo.h" 00006 #include "GlobalContactInfo.h" 00007 00008 PhysicalObject::PhysicalObject(std::string iniLabel, int nSensors, 00009 real saturationForce, 00010 Color outlineColor, Color fillColor) : 00011 GraphicalObject(outlineColor, fillColor), 00012 label(iniLabel), nSensors(nSensors), saturationForce(saturationForce) 00013 { 00014 //vectors are initialized with 0 by their constructors 00015 m=0.0; 00016 alpha=0.0; 00017 omega=0.0; 00018 I=0.0; 00019 externalTorque=0; 00020 parent=NULL; 00021 relativeAlpha=0.0; 00022 if(nSensors){ 00023 activations=new real[nSensors]; 00024 for(int i=0; i<nSensors; i++) 00025 activations[i]=0.0; 00026 } else { 00027 activations=NULL; 00028 } 00029 boxMin.setXY(-ThyrixParameters::infinity,-ThyrixParameters::infinity); 00030 boxMax.setXY(+ThyrixParameters::infinity,+ThyrixParameters::infinity); 00031 } 00032 00033 PhysicalObject::~PhysicalObject(){ 00034 deleteContacts(); 00035 if(activations!=NULL) 00036 delete[] activations; 00037 } 00038 00039 void PhysicalObject::registerPrimitives(Simulator* simulator){ 00040 simulator->registerPrimitive(this); 00041 } 00042 00043 void PhysicalObject::deleteContacts() { 00044 purgeContainer(contacts); 00045 resetSensors(); 00046 } 00047 00048 void PhysicalObject::resetSensors(){ 00049 if(nSensors){ 00050 for(unsigned int i=0; i<nSensors; i++) 00051 activations[i]=0; 00052 } 00053 } 00054 00055 00056 void PhysicalObject::integrate(const Integrator &integrator) { 00057 rOld = r; 00058 integrator.integrate(r, v); 00059 alphaOld = alpha; 00060 integrator.integrate(alpha, omega); 00061 normalizeAlpha(); 00062 computeBox(); 00063 } 00064 00065 void PhysicalObject::rollback() { 00066 r=rOld; 00067 alpha=alphaOld; 00068 computeBox(); 00069 } 00070 00071 void PhysicalObject::computeDerivativesWithoutContacts(ContactSolver* contactSolver) { 00072 v=externalForce; 00073 v/=m; 00074 omega=externalTorque/I; 00075 //reset external force and torque 00076 externalForce.setToZero(); 00077 externalTorque=0; 00078 } 00079 00080 void PhysicalObject::computeDerivatives(GlobalContactInfoVector* globalContacts){ 00081 ContactInfo* current; 00082 Vector2 vt; 00083 00084 for(ContactInfoPVector::iterator it=contacts.begin(), end=contacts.end(); 00085 it!=end;++it){ 00086 current=*it; 00087 vt=current->n; 00088 vt*=current->force*current->sigma/m; 00089 v+=vt; 00090 omega+=current->force*current->sigma*current->pxn/I; 00091 } 00092 setSensors(); 00093 } 00094 00095 void PhysicalObject::fillContactMatrix(ContactSolver* contactSolver, ContactInfo* contact){ 00096 /* 00097 contactInfo is the info of contact alpha relative to the current object 00098 00099 Assumes v_0, w_0 (the velocity without the contribution of the contacts) are 00100 already computed as v, omega. 00101 00102 This method works for both simple objects and composed objects. 00103 00104 */ 00105 ContactInfo* current; 00106 Vector2 vt1, vt2; 00107 00108 for(ContactInfoPVector::iterator it=contacts.begin(), end=contacts.end(); 00109 it!=end; ++it){ 00110 current=*it; 00111 vt1=current->n; 00112 vt1/=m; 00113 vt2=contact->p; 00114 vt2.cross(current->pxn); 00115 vt2/=I; 00116 vt1+=vt2; 00117 contactSolver->contactMatrix[contact->alpha][current->alpha]+= 00118 vt1*contact->n*contact->sigma*current->sigma; 00119 } 00120 vt1=contact->p; 00121 vt1.cross(omega); 00122 vt1+=v; 00123 contactSolver->contactVector[contact->alpha]+=vt1*contact->n*contact->sigma; 00124 } 00125 00126 void PhysicalObject::setSensors(){ 00127 if(nSensors){ 00128 for(ContactInfoPVector::iterator it=contacts.begin(), end=contacts.end(); 00129 it!=end; ++it){ 00130 setSensor(*it); 00131 } 00132 } 00133 } 00134 00135 void PhysicalObject::drawContactForces(GUI* gui){ 00136 for(ContactInfoPVector::iterator it=contacts.begin(), end=contacts.end(); 00137 it != end; ++it){ 00138 ContactInfo* current = *it; 00139 Vector2 rt = r + current->p; 00140 Vector2 force = current->n * 00141 (current->force * current->sigma); 00142 gui->drawForce(rt, force); 00143 } 00144 }
Thyrix homepage Users' guide
(C) Arxia 2004-2005