00001 // Pac.cpp: implementation of the Pac class. 00002 // 00004 00005 #include "Pac.h" 00006 #include "Circle.h" 00007 00009 // Construction/Destruction 00011 00012 Pac::Pac(real x0, real y0) : controller(0,5){ 00013 int i; 00014 nCircles=11; 00015 rPac=0.4; 00016 u=2*M_PI/nCircles; 00017 u0=u/2; 00018 Circle* c; 00019 for(i=0;i<nCircles;i++){ 00020 c=new Circle(0.1,x0+rPac*cos(u0+i*u),y0+rPac*sin(u0+i*u)); 00021 c->setColor(Color("slateGray")); 00022 addObject(c); 00023 } 00024 real k=3; 00025 for(i=0;i<nCircles;i++){ 00026 links.push_back(ElasticLink(objects[index(i-1)],objects[i],2*rPac*sin(u/2),k)); 00027 links.push_back(ElasticLink(objects[index(i-2)],objects[i],2*rPac*sin(u),k)); 00028 links.push_back(ElasticLink(objects[index(i-3)],objects[i],2*rPac*sin(3*u/2),k)); 00029 links.push_back(ElasticLink(objects[index(i-4)],objects[i],2*rPac*sin(2*u),k)); 00030 } 00031 activeLink=&(*(links.begin())); 00032 activeLink->k=4*k; 00033 l=2*rPac*sin(u/2); 00034 label="Pac"; 00035 } 00036 00037 Pac::~Pac(){ 00038 00039 } 00040 00041 void Pac::controll(){ 00042 activeLink->length=l*(1+2*controller.getOutput(0)); 00043 real thrust=controller.getOutput(1)*0.5; 00044 Vector2 n=objects[0]->r; 00045 n-=objects[nCircles-1]->r; 00046 n.normalize(); 00047 n.rotate(-M_PI/2); 00048 Vector2 force=n; 00049 force*=thrust; 00050 PhysicalObjectPVector::iterator it, end=objects.end(); 00051 for(it=objects.begin(); it!=end; ++it){ 00052 (*it)->externalForce+=force; 00053 } 00054 real rotation=(controller.getOutput(2)-controller.getOutput(3))*0.5; 00055 n*=rotation; 00056 n.rotate(M_PI/2+u0); 00057 for(it=objects.begin(); it!=end; ++it){ 00058 (*it)->externalForce+=n; 00059 n.rotate(u); 00060 } 00061 00062 00063 controller.advanceTime(); 00064 }
Thyrix homepage Users' guide
(C) Arxia 2004-2005