void ball() { float ballX,ballY,ballZ; int ifCollided=0; glPushMatrix(); ballX=-15+front; ballY=6; ballZ=0+lr; if(ballZ>25) { ballZ=25; ballX=ballX+OBJECT_SIZE; } else if(ballZ<-25) { ballZ=-25; ballX=ballX+OBJECT_SIZE; } else { //Do Nothing } glTranslatef(ballX,ballY,ballZ); glColor3f(1,0,0); glutWireSphere(5,100,10); glPopMatrix(); ifCollided=collisionTest(ballX+5,ballY,ballZ); //printf("%f\t%f\t%f\n",ballX,ballY,ballZ); if(ifCollided==1) { front=front-1; incX=0; } ifCollided=collisionTest(ballX,ballY,ballZ-5); if(ifCollided==1) { lr=lr+1; incX=0; } ifCollided=collisionTest(ballX,ballY,ballZ+5); if(ifCollided==1) { lr=lr-1; incX=0; } }
//main function used .Call() SEXP doCollisionTest(SEXP num, SEXP n, SEXP m) { if (!isNumeric(num) || !isNumeric(n) || !isNumeric(m)) error(_("invalid argument")); //temporary working variables int lenSample = asInteger( n ); //length of the sample 'num' int nbUrns = asInteger( m ); //number of urns int * rNum = INTEGER( num ); // vector of length n with random urn numbers int * Urns = (int *) R_alloc(nbUrns, sizeof(int)); //result int *nbCollision = (int *) R_alloc(1, sizeof(int)); SEXP resultinR; //result in R PROTECT(resultinR = allocVector(INTSXP, 1)); //allocate an integer /* if(resultinR == NULL) Rprintf("zog\n"); else Rprintf("toooo2\n");*/ nbCollision = INTEGER( resultinR ); //plug the C pointer on the R type R_CheckStack(); //computation step collisionTest(rNum, lenSample, nbUrns, Urns, nbCollision); UNPROTECT(1); return resultinR; }
void PlayState::move(int x, int y) { if (collisionTest(x, y)) { if (y == 1) { if (sPiece.y < 1) { GameStateManager::Instance()->changeGameState(new GameOverState()); } else { //Add piece to landed for (int row = 0; row != 4; row++) { for (int col = 0; col != 4; col++) { if (sPiece.size[row][col] != TILEBLANK) { landed[sPiece.y + row][sPiece.x + col] = sPiece.size[row][col]; } } } //Check filled rows for (int row = 0; row != 16; row++) { bool filled{ true }; for (int col = 0; col != 10; col++) { if (landed[row][col] == TILEBLANK) { filled = false; } } if (filled) { deleteRow(row); } } dropDelay -= .05; newPiece(); } } } else //No collision, move piece { sPiece.x += x; sPiece.y += y; } }
//TODO partie modèle à placer dans Joueur (garder partie graphique) void MainWindow::tryMove(int x, int y){ int newX = personnage->getX()+x; int newY = personnage->getY()+y; if(!collisionTest(x,y)){ personnage->setX(newX); personnage->setY(newY); personnage->getPicture()->moveBy(x,y); } }
void collide() { for (int i = 0; i < numballs; i++) { for (int j = i + 1; j < numballs; j++) { if (collisionTest(i, j)) { // move the two balls to their intersecting point balls[i].position = { balls[i].oldposition.x + balls[i].velocity.x * balls[i].collidetime, balls[i].oldposition.y + balls[i].velocity.y * balls[i].collidetime, balls[i].oldposition.z + balls[i].velocity.z * balls[i].collidetime }; balls[j].position = { balls[j].oldposition.x + balls[j].velocity.x * balls[j].collidetime, balls[j].oldposition.y + balls[j].velocity.y * balls[j].collidetime, balls[j].oldposition.z + balls[j].velocity.z * balls[j].collidetime }; // set the two balls' equlibrium point to their intersecting point balls[i].equilib = {balls[i].position.x, balls[i].position.y, balls[i].position.z}; balls[j].equilib = {balls[j].position.x, balls[j].position.y, balls[j].position.z}; // store old velocities of x and z because they've never been stored before balls[i].oldvelocity = {balls[i].velocity.x, balls[i].oldvelocity.y, balls[i].velocity.z}; balls[j].oldvelocity = {balls[j].velocity.x, balls[j].oldvelocity.y, balls[j].velocity.z}; // update the new velocities through switching them balls[i].velocity = {balls[j].oldvelocity.x, balls[j].oldvelocity.y, balls[j].oldvelocity.z}; balls[j].velocity = {balls[i].oldvelocity.x, balls[i].oldvelocity.y, balls[i].oldvelocity.z}; // now turn on the sticky force for both objects balls[i].spring = true; balls[j].spring = true; // identify the other ball that it's sticky to balls[i].relobj = j; balls[j].relobj = i; } } } }
void MainWindow::timerEvent ( QTimerEvent * event ){ // ----------- partie personnage if (gravity<0){ personnage->setCurrentH(personnage->getCurrentH()+1); if(personnage->getCurrentH()>= personnage->getMaxH() || collisionTest(0,-1)){ gravity = baseGravity; personnage->setCurrentH(0); } else{ personnage->setCurrentS(6); personnage->immobile(); } } else if(collisionTest(0,1)){ if(gravity == 1) personnage->setCurrentS(4); //LANDING gravity = 0; } else { gravity = baseGravity; personnage->setCurrentS(3); // FALLING personnage->immobile(); } int x = 0; if(gravity == 0 && controleur->getStateKeys(0)) tryJump(); if(controleur->getStateKeys(2)){ if (personnage->tryDropBombe()){ // ajouter un truc du style personnage->hasBonusBombe() ajouterBombe(personnage->getX()+personnage->getLargeur()/2,personnage->getY()+ personnage->getHauteur()); controleur->setPressed(Qt::Key_Down,false); } } if(controleur->getStateKeys(1)){ x +=- 1; personnage->courireG(); } else if(controleur->getStateKeys(3)){ x += 1; personnage->courireD(); } if(x == 0 && gravity == 0 /*&& personnage->getCurrentS() != 3*/) personnage->immobile(); // TODO ? asscocier les bombes au joueur, soit avec le trigger du Joueur, soir en ayant bombes[NumJ][bombes] if(/*personnage->hasBonusTrigger == trigger &&*/controleur->getStateKeys(4)){ triggerAll(); controleur->setPressed(Qt::Key_Space,false); } tryMove(0,gravity); tryMove(x,0); // ----------- partie bombes int tmpSizeB = bombes.size(); for(int i = 0; i<tmpSizeB; i++){ if(bombes[i]->isExploding()){ explosion(bombes[i],0,0); explosion(bombes[i],0,1); explosion(bombes[i],0,-1); explosion(bombes[i],1,0); explosion(bombes[i],-1,0); } if(bombes[i]->hasExploded()){ int tmpSizeE = bombes[i]->getExplosions().size(); for(int j = 0; j< tmpSizeE; j++) scene->removeItem(bombes[i]->getExplosions().at(j)); scene->removeItem((bombes[i])->getPicture()); bombes.remove(i); tmpSizeB--; personnage->decrNbBombe(); } } }
//TODO partie modèle à placer dans Joueur (garder partie graphique) void MainWindow::tryJump(){ if(collisionTest(0,1)){ gravity = -baseGravity; // personnage->setCurrentS(6); } }
bool Comb::preTest(Point startPoint, Point endPoint) { return collisionTest(startPoint, endPoint); }
bool Comb::calc(Point startPoint, Point endPoint, vector<Point>& combPoints) { if (shorterThen(endPoint - startPoint, MM2INT(1.5))) return true; bool addEndpoint = false; //Check if we are inside the comb boundaries if (!checkInside(startPoint)) { if (!moveInside(&startPoint)) //If we fail to move the point inside the comb boundary we need to retract. return false; combPoints.push_back(startPoint); } if (!checkInside(endPoint)) { if (!moveInside(&endPoint)) //If we fail to move the point inside the comb boundary we need to retract. return false; addEndpoint = true; } //Check if we are crossing any bounderies, and pre-calculate some values. if (!preTest(startPoint, endPoint)) { //We're not crossing any boundaries. So skip the comb generation. if (!addEndpoint && combPoints.size() == 0) //Only skip if we didn't move the start and end point. return true; } //Calculate the minimum and maximum positions where we cross the comb boundary calcMinMax(); int64_t x = sp.X; vector<Point> pointList; //Now walk trough the crossings, for every boundary we cross, find the initial cross point and the exit point. Then add all the points in between // to the pointList and continue with the next boundary we will cross, until there are no more boundaries to cross. // This gives a path from the start to finish curved around the holes that it encounters. while(true) { unsigned int n = getPolygonAbove(x); if (n == UINT_MAX) break; pointList.push_back(matrix.unapply(Point(minX[n] - MM2INT(0.2), sp.Y))); if ( (minIdx[n] - maxIdx[n] + boundery[n].size()) % boundery[n].size() > (maxIdx[n] - minIdx[n] + boundery[n].size()) % boundery[n].size()) { for(unsigned int i=minIdx[n]; i != maxIdx[n]; i = (i < boundery[n].size() - 1) ? (i + 1) : (0)) { pointList.push_back(getBounderyPointWithOffset(n, i)); } }else{ minIdx[n]--; if (minIdx[n] == UINT_MAX) minIdx[n] = boundery[n].size() - 1; maxIdx[n]--; if (maxIdx[n] == UINT_MAX) maxIdx[n] = boundery[n].size() - 1; for(unsigned int i=minIdx[n]; i != maxIdx[n]; i = (i > 0) ? (i - 1) : (boundery[n].size() - 1)) { pointList.push_back(getBounderyPointWithOffset(n, i)); } } pointList.push_back(matrix.unapply(Point(maxX[n] + MM2INT(0.2), sp.Y))); x = maxX[n]; } pointList.push_back(endPoint); //Optimize the pointList, skip each point we could already reach by not crossing a boundary. This smooths out the path and makes it skip any unneeded corners. Point p0 = startPoint; for(unsigned int n=1; n<pointList.size(); n++) { if (collisionTest(p0, pointList[n])) { if (collisionTest(p0, pointList[n-1])) return false; p0 = pointList[n-1]; combPoints.push_back(p0); } } if (addEndpoint) combPoints.push_back(endPoint); return true; }
float collisionTester::testCylinderCylinder(ofMatrix4x4& matA, ofVec3f& sA, ofMatrix4x4& matB, ofVec3f& sB){ setTransformFromOF(matA, sA, cylinderA); setTransformFromOF(matB, sB, cylinderB); return collisionTest(&cylinderA, &cylinderB, algoCC); }
float collisionTester::testSphereCylinder(ofMatrix4x4& matA, ofVec3f& sA, ofMatrix4x4& matB, ofVec3f& sB){ setTransformFromOF(matA, sA, sphereA); setTransformFromOF(matB, sB, cylinderB); return collisionTest(&sphereA, &cylinderB, algoSC); }
// // see bulletPhysics CollisionInterfaceDemo.cpp // float collisionTester::testSphereSphere(ofMatrix4x4& matA, ofVec3f& sA, ofMatrix4x4& matB, ofVec3f& sB){ setTransformFromOF(matA, sA, sphereA); setTransformFromOF(matB, sB, sphereB); return collisionTest(&sphereA, &sphereB, algoSS); }
//////////////////////////////////////////////////// // collision response // //////////////////////////////////////////////////// void collisionResponse(car* aCar,old_car_state* oCar) { int time; if (fabs(aCar->speed)<SP_SEN) aCar->speed=0.0; if (fabs(aCar->angle_speed)<AN_SP_SEN) aCar->angle_speed=0.0; // cout<<aCar->speed<<endl; int collisionPoint=collisionTest(aCar,oCar,time); //get the collision vertex, and the time // cout<<collisionPoint<<endl; if (collisionPoint!=-1) { // if there is a collision, restore the state to the moment just before collision aCar->cX=oCar->oldX; aCar->cY=oCar->oldY; aCar->angle=oCar->oldAngle; // cout<<time<<" "<<currentTime<<endl; // cout<<time-currentTime<<endl; ///* if ((time-currentTime)>(framePeriod+2)) // if collision time long enough, do the collision reaction { // collision response switch(collisionPoint) { case 0: keyPress[UP]=false; if (aCar->speed>0) aCar->speed=-aCar->speed*BNC_SP_DP; break; case 1: keyPress[LEFT]=false; if (aCar->speed>0) aCar->angle_speed=BNC_AN; else aCar->angle_speed=-BNC_AN; aCar->speed-=aCar->speed*SP_DP*0.5; break; case 2: keyPress[RIGHT]=false; if (aCar->speed>0) aCar->angle_speed=-BNC_AN; else aCar->angle_speed=BNC_AN; aCar->speed-=aCar->speed*SP_DP*0.5; break; case 3: case 4: keyPress[DOWN]=false; aCar->angle_speed=0.0; aCar->speed=-aCar->speed*BNC_SP_DP; break; case 5: keyPress[LEFT]=false; if (aCar->speed>0) aCar->angle_speed=BNC_AN*0.7; else aCar->angle_speed=-BNC_AN*0.7; aCar->speed-=aCar->speed*SP_DP; break; case 6: keyPress[RIGHT]=false; if (aCar->speed>0) aCar->angle_speed=-BNC_AN*0.7; else aCar->angle_speed=BNC_AN*0.7; aCar->speed-=aCar->speed*SP_DP; break; default: break; } } else // if the collision interval is too short, bounce, get rid of jitter { // cout<<"bounce back"<<endl; // cout<<"speed:"<<aCar->speed<<endl; ///* switch(collisionPoint) { case 0: case 1: case 2: case 5: case 6: keyPress[UP]=false; aCar->angle_speed=0.0; aCar->speed=-fabs(aCar->speed)*BNC_SP_DP; break; case 3: case 4: keyPress[DOWN]=false; aCar->angle_speed=0.0; aCar->speed=fabs(aCar->speed)*BNC_SP_DP; break; default: break; } //*/ } //*/ currentTime=time; } }