void chain::EC(double x, int i,int m,int lastchain, int lastbead, vektor<double> ev, int d, bool beadKoll, bool rightKollBead, bool beadBeadKoll) { // cout.precision(20); // uniform_real_distribution<> distribution(0, 1); // auto rnd = bind(distribution, ref(*engineX)); // // uniform_real_distribution<> PIdistribution(0, PI); // auto rndPI = bind(PIdistribution, ref(*engineX)); // // uniform_real_distribution<> NEGdistribution(-1, 1); // auto rndWAY = bind(NEGdistribution, ref(*engineX)); // // uniform_int_distribution<> INTdistribution(0, N-1); // auto INTrnd = bind(INTdistribution, ref(*engineX)); // // uniform_int_distribution<> Mdistribution(0, M-1); // auto Mrnd = bind(Mdistribution, ref(*engineX)); // if(d==0) cout << "RANDOMZAHL IN EC=" << rnd() << endl; if(x<=0){ if(x<0) cout << "----Fehler: x=" << x << "!!!--- \n EC beendet" << endl; // if(Schritt==Bug) cout << "EC beendet" << endl; return; } if(d>1e4){//Einfache Lösung, noch zu überprüfen cout << "Endlosschleife gefunden, fahre mit EC fort, x=" << x << endl; AnzahlEndlosschleifen++; double phi=2*rndPI(); double theta=acos(1-2*rnd()); vektor<double> temp=vektor<double>(sin(theta)*sin(phi),sin(theta)*cos(phi),cos(theta)); ev=ev.kreuz(temp);//Randomvektor, senkrecht zu ev. ev=ev/ev.Betrag(); d=0; // int nextChain=M*rnd(); // Chains->at(nextChain).EC(x,N*rnd(),nextChain,-1,-1,ev,0); // return; } // if(Schritt==Bug) cout << "\n \n \n Bewegung von Kette " << m << " Bead " << i << " x=" << fixed << x << " d=" << d << endl; // if(Schritt==Bug) cout << "lastchain =" << lastchain << " lastbead=" << lastbead << endl; // if(Schritt==Bug && beadKoll) cout << "lastbead= " << lastbead+1 << endl; // if(Schritt==Bug && rightKollBead) cout << "rightKollBead " << endl; // if(Schritt==Bug && beadBeadKoll) cout << "beadBeadKoll " << endl; // double phi=2*rndPI(); // double theta=rndPI(); //vektor<double> ev=vektor<double>(sin(theta)*sin(phi),sin(theta)*cos(phi),cos(theta)); //vektor<double> ev=vektor<double>(0,0,1); //ev=ev/ev.Betrag(); //ev.Ausgabe(); //if(ev.Betrag()!=1) {cout << "Fehler: Betrag von ev ist " << ev.Betrag() << endl;} //double x=l; //while(x>0){ double w=x; double q=x; bool thisBeadKoll; bool nextBeadKoll=false; int nextKollChain; int nextKollBead; bool rightchain; // // cout << "Erstelle Kollisionsliste" << endl; // vector<pair<int,int>> Koll1Bonds; // vector<pair<int,int>> Koll2Bonds; // if(Schritt==Bug) // { // ListeAusgeben(listX,0); // ListeAusgeben(listY,1); // ListeAusgeben(listZ,2); // // cout << "Bewegung von "; // Beads.at(i).ort.Ausgabe(); // cout << " nach"; // (Beads.at(i).ort.add(ev*x)).Ausgabe(); // cout << "ev="; // ev.Ausgabe(); // cout << endl; // } // int mglKoll=0; // int tatKoll=0; // cout << "Berechne Kollisionen" << endl; // if(Schritt==Bug) cout << "\n Prüfe auf Kollision mit Kette " << j << " Beads " << k << "," << k-1 << endl; double q1=x; double q2=x; double p1=1000; double p2=1000; double p=1000; int j1,k1,j2,k2; bool nextBeadKoll1=false; bool nextBeadKoll2=false; // if (Schritt==Bug) cout << "linker Bond" << endl; int xSchwerL; int ySchwerL; int zSchwerL; if(i!=0){ // if(Schritt==Bug){ // cout << "Schwerpunkt" ; // Bonds.at(i-1).Schwerpunkt.Ausgabe(); // cout << "X=" << ((int(Bonds.at(i-1).Schwerpunkt.x)/Zellgroesse)-1+AnzZellen)%AnzZellen << endl; // cout << "XMax=" << (int(Bonds.at(i-1).Schwerpunkt.x)/Zellgroesse+2)%AnzZellen << endl; // cout << "Y=" << ((int(Bonds.at(i-1).Schwerpunkt.y)/Zellgroesse)-1+AnzZellen)%AnzZellen << endl; // cout << "YMax=" << (int(Bonds.at(i-1).Schwerpunkt.y)/Zellgroesse+2)%AnzZellen << endl; // cout << "Z=" << ((int(Bonds.at(i-1).Schwerpunkt.z)/Zellgroesse)-1+AnzZellen)%AnzZellen << endl; // cout << "ZMax=" << (int(Bonds.at(i-1).Schwerpunkt.z)/Zellgroesse+2)%AnzZellen << endl; vektor<double> r_=Beads.at(i).ort-Beads.at(i-1).ort; xSchwerL=Bonds.at(i-1).Schwerpunkt.x/Zellgroesse; ySchwerL=Bonds.at(i-1).Schwerpunkt.y/Zellgroesse; zSchwerL=Bonds.at(i-1).Schwerpunkt.z/Zellgroesse; int Xmin=(xSchwerL-1+AnzZellen)%AnzZellen; int Xmax=(xSchwerL+2)%AnzZellen; int Ymin=(ySchwerL-1+AnzZellen)%AnzZellen; int Ymax=(ySchwerL+2)%AnzZellen; int Zmin=(zSchwerL-1+AnzZellen)%AnzZellen; int Zmax=(zSchwerL+2)%AnzZellen; // if(x>x0){ // if(ev.x>0) Xmax=(Xmax+1)%AnzZellen; // else Xmin=(Xmin-1+AnzZellen)%AnzZellen; // if(ev.y>0) Ymax=(Ymax+1)%AnzZellen; // else Ymin=(Ymin-1+AnzZellen)%AnzZellen; // if(ev.z>0) Zmax=(Zmax+1)%AnzZellen; // else Zmin=(Zmin-1+AnzZellen)%AnzZellen; // } // for(int X=Xmin;X!=Xmax;X=(X+1)%AnzZellen){ for(int Y=Ymin;Y!=Ymax;Y=(Y+1)%AnzZellen){ for(int Z=Zmin;Z!=Zmax;Z=(Z+1)%AnzZellen){ // cout <<int(Bonds.at(i-1).Schwerpunkt.y)%Zellgroesse << endl; // if(Schritt==Bug) cout << X << Y << Z << endl; // cout << Zellen[X][Y][Z].size() << endl; // if(!Zellen[X][Y][Z].empty()) { for(list<pair<int,int>>::iterator it=Zellen[X][Y][Z].begin();it!=Zellen[X][Y][Z].end();++it) { // if(Schritt==Bug) cout << "for1 "; int j=it->first; int k=it->second; // if(Schritt==Bug) cout << "j="<<j << "k=" << k << endl; if((j==m && (k==i-2 || k==i || k==i-1)) || (j==lastchain && k==lastbead-1 && (rightKollBead || beadBeadKoll)) || (j==lastchain && k==lastbead && (rightKollBead || beadBeadKoll) && beadKoll) ); else { // if(Schritt==Bug) cout << "Prüfe ausgehend von Beads "<< i-1 << "," << i << endl; pair<double,double> Kollp=Kollision(Beads.at(i-1).ort, Chains->at(j).Bonds.at(k).Bead0->ort, Chains->at(j).Bonds.at(k).Bead1->ort, r_, ev,x); //Überprüfung des linken Bonds // if(Schritt==Bug) cout << "w=" << Koll << endl; // mglKoll++; // if(Koll<x) tatKoll++; double Koll=Kollp.first; if(abs(Koll-q1)<1e-10 && abs(Koll-x)>1e-10 && abs(k-k1)<=1 && j==j1)//Bead wird direkt getroffen { // if(Schritt==Bug) cout << "Bead wird direkt getroffen" << endl; nextBeadKoll1=true; k=min(k,k1); k1=k; // if(Schritt==Bug) cout << "k=" << k << endl; } if(Koll < q1) { q1=Koll; p1=Kollp.second; j1=j; k1=k; if(abs(Koll-q1)>1e-10) nextBeadKoll1=false; } } } }}} // } } // cout << "Rechter Bond " << endl; int xSchwerR; int ySchwerR; int zSchwerR; if(i!=N-1){ vektor<double> r_=Beads.at(i).ort-Beads.at(i+1).ort; xSchwerR=Bonds.at(i).Schwerpunkt.x/Zellgroesse; ySchwerR=Bonds.at(i).Schwerpunkt.y/Zellgroesse; zSchwerR=Bonds.at(i).Schwerpunkt.z/Zellgroesse; int Xmin=(xSchwerR-1+AnzZellen)%AnzZellen; int Xmax=(xSchwerR+2)%AnzZellen; int Ymin=(ySchwerR-1+AnzZellen)%AnzZellen; int Ymax=(ySchwerR+2)%AnzZellen; int Zmin=(zSchwerR-1+AnzZellen)%AnzZellen; int Zmax=(zSchwerR+2)%AnzZellen; // if(x>x0){ // if(ev.x>0) Xmax=(Xmax+1)%AnzZellen; // else Xmin=(Xmin-1+AnzZellen)%AnzZellen; // if(ev.y>0) Ymax=(Ymax+1)%AnzZellen; // else Ymin=(Ymin-1+AnzZellen)%AnzZellen; // if(ev.z>0) Zmax=(Zmax+1)%AnzZellen; // else Zmin=(Zmin-1+AnzZellen)%AnzZellen; // } //// // } for(int X=Xmin;X!=Xmax;X=(X+1)%AnzZellen){ for(int Y=Ymin;Y!=Ymax;Y=(Y+1)%AnzZellen){ for(int Z=Zmin;Z!=Zmax;Z=(Z+1)%AnzZellen){ // if(Schritt==Bug) cout << X << Y << Z << endl; // if(!Zellen[X][Y][Z].empty()){ for(list<pair<int,int>>::iterator it=Zellen[X][Y][Z].begin();it!=Zellen[X][Y][Z].end();++it) { // if(Schritt==Bug) cout << "for2 "; int j=it->first; int k=it->second; // if(Schritt==Bug) cout << "j="<<j << "k=" << k << endl; if((j==m && (k==i+1 ||k==i || k==i-1)) || (j==lastchain && k==lastbead-1 && (!rightKollBead|| beadBeadKoll)) || (j==lastchain && k==lastbead && (!rightKollBead || beadBeadKoll) && beadKoll) ); else { // if(Schritt==Bug) cout << "Prüfe ausgehend von Beads "<< i-1 << "," << i << endl; pair<double,double> Kollp=Kollision(Beads.at(i+1).ort, Chains->at(j).Bonds.at(k).Bead0->ort, Chains->at(j).Bonds.at(k).Bead1->ort, r_, ev,x); //Überprüfung des linken Bonds // if(Schritt==Bug) cout << "w=" << Koll << endl; // mglKoll++; double Koll=Kollp.first; // if(Koll<x) tatKoll++; if(abs(Koll-q2)<1e-10 && abs(Koll-x)>1e-10 && abs(k-k2)<=1 && j==j2)//Bead wird direkt getroffen { // if(Schritt==Bug) cout << "Bead wird direkt getroffen" << endl; nextBeadKoll2=true; k=min(k,k2); k2=k; } if(Koll < q2) { q2=Koll; p2=Kollp.second; j2=j; k2=k; if(abs(Koll-q2)>1e-10) nextBeadKoll2=false; } } } }}} // } } // if(Schritt==Bug) cout << "q1=" << q1 << " j1=" << j1 << " k1=" << k1<< " q2=" << q2 << " j2=" << j2 << " k2=" << k2 << endl; q=min(q1,q2); //cout << "---" << endl; if(q<0) cout << "--------------FEHLER, q<0! q=" << q << "---------------" << endl; if(q<w) //Kollision mit Chain j Beads k-1 & k { int j,k; if(abs(q1-q2)<1e-10 && x-q1>1e-10 &&j1==j2 && abs(k1-k2)<=1) { // if(Schritt==Bug) cout << "Kollision direkt mit Bead!" << endl; thisBeadKoll=true; //Bond wird direkt von bead getroffen j=j1; k=min(k1,k2); nextBeadKoll=(nextBeadKoll1 || nextBeadKoll2); } else {thisBeadKoll=false; if(q2<q1) { nextBeadKoll=nextBeadKoll2; rightchain=true; j=j2; k=k2; p=p2; } else { nextBeadKoll=nextBeadKoll1; rightchain=false; j=j1; k=k1; p=p1; } } // if(Schritt==Bug) cout << "k=" << k << endl; w=q; nextKollChain=j; nextKollBead=k+1; // nextBeadKoll=false; } // if(w!=x) w-=1e-3;// Bewegung nicht ganz bis zum Bond! ACHTUNG! // cout << "x= " << x << endl; double w1=ECMove(i,i+1,x,ev); double w2=ECMove(i,i-1,x,ev); double wMin=min(w,min(w1,w2)); // cout << "Anteil Kollision=" << 1.*tatKoll/mglKoll << endl; // cout << "verschiebe" << endl; Beads.at(i).verschiebe(ev*wMin); // if(Schritt==Bug) cout << "aktualisiere linken Bond" << endl; if(i!=0) { // if(Schritt==Bug) cout << "Setze Zeiger auf alte Zelle" << endl; // int Xold=int(Bonds.at(i-1).Schwerpunkt.x/(Zellgroesse)); // int Yold=int(Bonds.at(i-1).Schwerpunkt.y/(Zellgroesse)); // int Zold=int(Bonds.at(i-1).Schwerpunkt.z/(Zellgroesse)); Bonds.at(i-1).refreshSchwerpunkt(); // if(Schritt==Bug) Bonds.at(i).Schwerpunkt.Ausgabe(); // if(Schritt==Bug) cout << "neue Zelle " << int(Bonds.at(i).Schwerpunkt.x)/(Zellgroesse) << int(Bonds.at(i).Schwerpunkt.y)/(Zellgroesse) << int(Bonds.at(i).Schwerpunkt.z)/(Zellgroesse) << endl; // if(Schritt==Bug) cout << " hat " << Zellen[int(Bonds.at(i).Schwerpunkt.x)/(Zellgroesse)][int(Bonds.at(i).Schwerpunkt.y)/(Zellgroesse)][int(Bonds.at(i).Schwerpunkt.z)/(Zellgroesse)].size() << " Elemente" << endl; int X=int(Bonds.at(i-1).Schwerpunkt.x/(Zellgroesse)); int Y=int(Bonds.at(i-1).Schwerpunkt.y/(Zellgroesse)); int Z=int(Bonds.at(i-1).Schwerpunkt.z/(Zellgroesse)); if(X>AnzZellen-1 || X<0) cout << "Fehler! Falsche Zellenzuordnung. X=" << X << endl; if(Y>AnzZellen-1 || Y<0) cout << "Fehler! Falsche Zellenzuordnung. X=" << Y << endl; if(Z>AnzZellen-1 || Z<0) cout << "Fehler! Falsche Zellenzuordnung. X=" << Z << endl; if(X!=xSchwerL || Y!=ySchwerL || Z!=zSchwerL){ list<pair<int,int>> * old = &Zellen[xSchwerL][ySchwerL][zSchwerL]; // if(Schritt==Bug) Bonds.at(i-1).Schwerpunkt.Ausgabe(); // if(Schritt==Bug) cout << "neue Zelle " << int(Bonds.at(i-1).Schwerpunkt.x)/(Zellgroesse) << int(Bonds.at(i-1).Schwerpunkt.y)/(Zellgroesse) << int(Bonds.at(i-1).Schwerpunkt.z)/(Zellgroesse) << endl; // if(Schritt==Bug) cout << " hat " << Zellen[int(Bonds.at(i-1).Schwerpunkt.x)/(Zellgroesse)][int(Bonds.at(i-1).Schwerpunkt.y)/(Zellgroesse)][int(Bonds.at(i-1).Schwerpunkt.z)/(Zellgroesse)].size() << " Elemente" << endl; list<pair<int,int>>::iterator einf = Zellen[X][Y][Z].begin(); // cout << Bonds.at(i-1).self->second << endl; // if(Schritt==Bug) cout << "splice" << endl; Zellen[X][Y][Z].splice(einf,*old,Bonds.at(i-1).self); // if(Schritt==Bug) cout << "Setze neuen iterator" << endl; Bonds.at(i-1).self=Zellen[X][Y][Z].begin(); // Bonds.at(i-1).self; } } // if(Schritt==Bug) cout << "aktualisiere rechten Bond" << endl; if(i!=N-1){ // if(Schritt==Bug) cout << "Setze Zeiger auf alte Zelle" << endl; // if(Schritt==Bug) cout << "alte Zelle " << int(Bonds.at(i).Schwerpunkt.x)/(Zellgroesse) << int(Bonds.at(i).Schwerpunkt.y)/(Zellgroesse) << int(Bonds.at(i).Schwerpunkt.z)/(Zellgroesse) << endl; // if(Schritt==Bug) Bonds.at(i).Schwerpunkt.Ausgabe(); // int Xold=int(Bonds.at(i).Schwerpunkt.x/(Zellgroesse)); // int Yold=int(Bonds.at(i).Schwerpunkt.y/(Zellgroesse)); // int Zold=int(Bonds.at(i).Schwerpunkt.z/(Zellgroesse)); Bonds.at(i).refreshSchwerpunkt(); // if(Schritt==Bug) Bonds.at(i).Schwerpunkt.Ausgabe(); // if(Schritt==Bug) cout << "neue Zelle " << int(Bonds.at(i).Schwerpunkt.x)/(Zellgroesse) << int(Bonds.at(i).Schwerpunkt.y)/(Zellgroesse) << int(Bonds.at(i).Schwerpunkt.z)/(Zellgroesse) << endl; // if(Schritt==Bug) cout << " hat " << Zellen[int(Bonds.at(i).Schwerpunkt.x)/(Zellgroesse)][int(Bonds.at(i).Schwerpunkt.y)/(Zellgroesse)][int(Bonds.at(i).Schwerpunkt.z)/(Zellgroesse)].size() << " Elemente" << endl; int X=int(Bonds.at(i).Schwerpunkt.x/(Zellgroesse)); int Y=int(Bonds.at(i).Schwerpunkt.y/(Zellgroesse)); int Z=int(Bonds.at(i).Schwerpunkt.z/(Zellgroesse)); if(X>AnzZellen-1 || X<0) cout << "Fehler! Falsche Zellenzuordnung. X=" << X << endl; if(Y>AnzZellen-1 || Y<0) cout << "Fehler! Falsche Zellenzuordnung. X=" << Y << endl; if(Z>AnzZellen-1 || Z<0) cout << "Fehler! Falsche Zellenzuordnung. X=" << Z << endl; if(X!=xSchwerR || Y!=ySchwerR || Z!=zSchwerR){ list<pair<int,int>> * old = &Zellen[xSchwerR][ySchwerR][zSchwerR]; // if(Schritt==Bug) Bonds.at(i-1).Schwerpunkt.Ausgabe(); // if(Schritt==Bug) cout << "neue Zelle " << int(Bonds.at(i-1).Schwerpunkt.x)/(Zellgroesse) << int(Bonds.at(i-1).Schwerpunkt.y)/(Zellgroesse) << int(Bonds.at(i-1).Schwerpunkt.z)/(Zellgroesse) << endl; // if(Schritt==Bug) cout << " hat " << Zellen[int(Bonds.at(i-1).Schwerpunkt.x)/(Zellgroesse)][int(Bonds.at(i-1).Schwerpunkt.y)/(Zellgroesse)][int(Bonds.at(i-1).Schwerpunkt.z)/(Zellgroesse)].size() << " Elemente" << endl; list<pair<int,int>>::iterator einf = Zellen[X][Y][Z].begin(); // cout << Bonds.at(i-1).self->second << endl; // if(Schritt==Bug) cout << "splice" << endl; Zellen[X][Y][Z].splice(einf,*old,Bonds.at(i).self); // if(Schritt==Bug) cout << "Setze neuen iterator" << endl; Bonds.at(i).self=Zellen[X][Y][Z].begin(); // Bonds.at(i).self; } } // if(Schritt==Bug) cout << "===w1= " << w1 << " ; " << " w2= " << w2 << " wKoll="<< fixed << w << "===" << endl; if(w<w1 && w < w2){ // cout << "Kollision" << endl; // if(Schritt==Bug) cout << "Kollision Chain " << m << " mit " << nextKollChain << endl; AnzahlKollisionen++; // if(Schritt==Bug) cout << "Bead " << i << " wird um w" << w << "bewegt" << endl; // Beads.at(i).verschiebe(ev*w); // if(Schritt==Bug){ // if(rightchain) cout << "Kollision ausgehend von rechtem Bond" << endl; // else cout << "Kollision ausgehend von linkem Bond " << endl; // // } // if(Schritt==Bug && thisBeadKoll) cout << "thisBeadKoll" << endl; // if(Schritt==Bug && nextBeadKoll) cout << "nextBeadKoll" << endl; if(thisBeadKoll) { if(nextBeadKoll) Chains->at(nextKollChain).EC(x-w,nextKollBead,nextKollChain,m,i,ev,d+1,thisBeadKoll,false,true); else { // if(Schritt==Bug) cout << "else 1" << endl; if(rnd()<p) { Chains->at(nextKollChain).EC(x-w,nextKollBead,nextKollChain,m,i,ev,d+1,thisBeadKoll,true); //rechter Bead wird bewegt } else { Chains->at(nextKollChain).EC(x-w,nextKollBead-1,nextKollChain,m,i,ev,d+1,thisBeadKoll); //linker Bead wird bewegt } } } else{ if(nextBeadKoll) Chains->at(nextKollChain).EC(x-w,nextKollBead,nextKollChain,m,i+rightchain,ev,d+1,thisBeadKoll,false,false); else { // if(Schritt==Bug) cout << "else 2" << endl; if(rnd()<p) { Chains->at(nextKollChain).EC(x-w,nextKollBead,nextKollChain,m,i+rightchain,ev,d+1,thisBeadKoll,true); } else { Chains->at(nextKollChain).EC(x-w,nextKollBead-1,nextKollChain,m,i+rightchain,ev,d+1,thisBeadKoll); } } } return; } AnzahlFederEC++; if (w1<0 || w2<0) cout << "FEHLER: Wegstrecke <0!" << endl; if (w1<w2) { // cout << "Bead " << i << " wird um w1=" << w1 << "bewegt" << endl; // Beads.at(i).verschiebe(ev*w1); // i=i+1; Chains->at(m).EC(x-w1,i+1,m,m,i+1,ev,d+1); return; } if(w2<w1) { // cout << "Bead " << i << " wird um w2=" << w2 << "bewegt" << endl; // Beads.at(i).verschiebe(ev*w2); // i=i-1; Chains->at(m).EC(x-w2,i-1,m,m,i-1,ev,d+1); return; } if(w2==w1 && w1==x) { // if(Schritt==Bug) cout << "Ende" << endl; // cout << "Bead " << i << " wird um x=" << x << "bewegt" << endl; // Beads.at(i).verschiebe(ev*x); x-=w1; //Chains->at(i).EC(x-w,i,m,m,i,ev); return; } else if(w2==w1) { // cout << "Bead " << i << " wird um w1=w2=" << w1 << "bewegt" << endl; // Beads.at(i).verschiebe(ev*w1); //x-=w1; if(rnd()<0.5) i++; else i--; Chains->at(m).EC(x-w1,i,m,m,i,ev,d+1); return; } //} // cout << "k= " << k << endl; if(x!=0) cout << "FEHLER!!! x ist nicht 0 sondern " << x << endl; }
int main(int argc, char** argv) { srand((unsigned int)time(NULL)); gBallLock = SDL_CreateMutex(); gRenderLock = SDL_CreateMutex(); SDL_Init(SDL_INIT_VIDEO); gWnd = SDL_CreateWindow("Cannonball Marksman", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCR_W, SCR_H, SDL_WINDOW_SHOWN); gSurf = SDL_GetWindowSurface(gWnd); gRend = SDL_CreateRenderer(gWnd, -1, SDL_RENDERER_ACCELERATED); SimpleTimerClass timer; initBGTex(); clearBackground(); CannonBall ball = CannonBall(263860.0f, 2.0f, vec3f(10, 0, 1.0f), vec3f(100.0f, 0.0f, 66.0f), vec3f(0.0f, 0.0f, 0.0f)); RopeBall rball = RopeBall(850000.0f, 8.0f, 80.0f, vec3f(600.0f, 0.0f, 300.0f)); cannon = new Cannon(std::string("cannon.bmp"), vec3f(100, 0, 66)); SDL_Thread* commandThreadID = SDL_CreateThread(commandHandler, "commandThread", (void*)&ball); while (!exitCond) { timer.update(); rball.update((float)timer.deltaTime, wind); SDL_LockMutex(gBallLock); if (ball.launch == true) { Kollision(&ball, &rball); //Check if cannonball collides with pendulum(ropeBall) ball.update((float)timer.deltaTime, wind); if (length(ball.linVel) < 0.001f && ball.pos.z - ball.radius < 0.01f && length(ball.fricForce) < 0.01f) { printf("The ball has stopped.\n"); ball.launch = false; } } SDL_UnlockMutex(gBallLock); SDL_LockMutex(gRenderLock); clearBackground(); rball.render(); ball.render(); cannon->render(); SDL_RenderPresent(gRend); SDL_UnlockMutex(gRenderLock); } delete cannon; SDL_WaitThread(commandThreadID, NULL); SDL_DestroyMutex(gBallLock); SDL_DestroyMutex(gRenderLock); SDL_DestroyTexture(gBGTex); SDL_DestroyWindow(gWnd); SDL_DestroyRenderer(gRend); SDL_Quit(); return 0; }
void main(void) { Init(); FrontLED(ON); int i=0; int linienEnde = 0; int fast = 200; unsigned int stData = 0, schwarz = 0; //Weißwert festlegen unsigned int data[2]; LineData(data); stData = data[LEFT] + data[RIGHT] + 50; StatusLED(GREEN); msleep(3000); StatusLED(YELLOW); //Schwarzwert setzen LineData(data); schwarz = data[LEFT] + data[RIGHT]; msleep(1000); StatusLED(GREEN); MotorSpeed(fast,fast); StatusLED(YELLOW); while(1){ LineData(data); if(data[LEFT] > data[RIGHT]){ MotorDir(FWD,FREE); BackLED(ON,OFF); }else if(data[RIGHT] > data[LEFT]){ MotorDir(FREE,FWD); BackLED(OFF,ON); ; } if(data[LEFT]+data[RIGHT]>stData){ StatusLED(RED); i++; }else{ i=0; StatusLED(YELLOW); } if(i>100){ linienEnde = 1; } while(linienEnde){ MotorDir(FWD,RWD); LineData(data); if(data[LEFT]+data[RIGHT] < schwarz+100){ MotorDir(BREAK,BREAK); msleep(50); while(linienEnde){ MotorSpeed(80,80); MotorDir(RWD,FWD); LineData(data); if(data[LEFT] + data[RIGHT] < schwarz+100){ linienEnde = 0; } } MotorSpeed(fast,fast); } } Kollision(); } }