Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

ComposedPhysicalObject.cpp

Go to the documentation of this file.
00001 #include "ComposedPhysicalObject.h"
00002 #include "Simulator.h"
00003 #include "purgeContainer.h"
00004 #include <string>
00005 
00006 ComposedPhysicalObject::ComposedPhysicalObject(std::string label) :
00007    PhysicalObject(label){
00008 }
00009 
00010 ComposedPhysicalObject::ComposedPhysicalObject(real x, real y, real alpha, std::string label) :
00011    PhysicalObject(label){
00012    r.setXY(x,y);
00013    this->alpha=alpha;
00014 }
00015 
00016 ComposedPhysicalObject::~ComposedPhysicalObject(){
00017    deleteObjects();
00018 }
00019 
00020 
00021 
00022 void ComposedPhysicalObject::addObject(PhysicalObject* object, real x, real y, real alpha){
00023    object->parent=this;
00024    object->relativeR.setXY(x,y);
00025    object->relativeAlpha=alpha;
00026    object->label=label+": "+object->label;
00027    objects.push_back(object);
00028    computeMassProperties();
00029 }
00030 
00031 void ComposedPhysicalObject::deleteObjects(){
00032    purgeContainer(objects);   
00033 }
00034 
00035 
00036 void ComposedPhysicalObject::computeMassProperties(){
00037    Vector2 v;
00038    PhysicalObject* object;
00039    m=0;
00040    I=0;
00041    s.setToZero();
00042    for(PhysicalObjectPVector::iterator it=objects.begin(), 
00043                               end=objects.end(); it!=end; ++it){
00044       object=*it;
00045       assert(object);
00046       m+=object->m;     //add member object mass to total mass
00047       v=object->relativeR;//position of the member object relative to the composed object
00048       I+=object->m*v.getSquaredModule()+object->I;
00049       v*=object->m;     
00050       s+=v;
00051    }
00052    s/=m;
00053 }
00054 
00055 void ComposedPhysicalObject::computeMemberPositions(){
00056    Vector2 v;
00057    PhysicalObject* object;
00058    boxMin.setXY(ThyrixParameters::infinity,ThyrixParameters::infinity);
00059    boxMax.setXY(-ThyrixParameters::infinity,-ThyrixParameters::infinity);
00060    for(PhysicalObjectPVector::iterator it=objects.begin(), 
00061                               end=objects.end(); it!=end; ++it){
00062       object=*it;
00063       v=object->relativeR; //the position relative to the ORS
00064       v.rotate(alpha);
00065       v+=r;             //add the position of ORS relative to LRS
00066       object->rOld=object->r;
00067       object->r=v;      //update the position of member object relative to LRS
00068       object->alphaOld=object->alpha;
00069       object->alpha=alpha+object->relativeAlpha;
00070       object->computeBox();
00071       boxMin.updateMin(object->boxMin);
00072       boxMax.updateMax(object->boxMax);
00073    }
00074 }
00075 
00076 void ComposedPhysicalObject::registerPrimitives(Simulator* simulator){
00077    for(PhysicalObjectPVector::iterator it=objects.begin(), 
00078             end=objects.end(); it!=end; ++it) {
00079       simulator->registerPrimitive(*it);
00080   }
00081 }
00082 
00083 bool ComposedPhysicalObject::detectContacts(PhysicalObject* object, GlobalContactInfoVector* contacts) {
00084    bool isContact=false;
00085    for(PhysicalObjectPVector::iterator it=objects.begin(), 
00086                                                       end=objects.end(); it!=end; ++it)
00087       isContact = (*it)->detectContacts(object, contacts) || isContact;
00088    return isContact;
00089 }
00090 
00091 bool ComposedPhysicalObject::detectMouseContact(const Vector2& rMouse, Vector2& p, PhysicalObject*& object){
00092    bool isContact=false;
00093    for(PhysicalObjectPVector::iterator it=objects.begin(), 
00094          end=objects.end(); it!=end && !isContact; ++it){
00095          isContact = (*it)->detectMouseContact(rMouse, p, object);
00096          if (isContact){
00097             p.rotate(-relativeAlpha);
00098             p+=(*it)->relativeR; //p is now relative to the origin of the composed object
00099             object=this;
00100          }
00101       }
00102    return isContact;
00103 }
00104 
00105 void ComposedPhysicalObject::setSensors(){
00106    ContactInfo* current;
00107    for(ContactInfoPVector::iterator it=contacts.begin(), end=contacts.end();
00108         it!=end;++it){
00109       current=*it;
00110       if(current->iSensor>=0){
00111          current->object->setSensor(current);
00112       }
00113    }
00114 }
00115 
00116 void ComposedPhysicalObject::resetSensors(){
00117    for(PhysicalObjectPVector::iterator it=objects.begin(), 
00118             end=objects.end(); it!=end; ++it) {
00119       (*it)->resetSensors();
00120   }
00121 }
00122 
00123 void ComposedPhysicalObject::draw(GUI *gui){
00124    for(PhysicalObjectPVector::iterator it=objects.begin(), 
00125             end=objects.end(); it!=end; ++it) {
00126       (*it)->draw(gui);
00127    }
00128    //drawContactForces(gui);
00129 }
00130 
00131 void ComposedPhysicalObject::setOutlineColor(Color color){
00132    outlineColor=color;
00133    for(PhysicalObjectPVector::iterator it=objects.begin(), 
00134             end=objects.end(); it!=end; ++it) {
00135       (*it)->setOutlineColor(color);
00136    }
00137 }
00138 
00139 void ComposedPhysicalObject::setFillColor(Color color){
00140    fillColor=color;
00141    for(PhysicalObjectPVector::iterator it=objects.begin(), 
00142             end=objects.end(); it!=end; ++it) {
00143       (*it)->setFillColor(color);
00144    }
00145 }

Thyrix homepageUsers' guide

(C) Arxia 2004-2005