void moveInHistory(LineState *l, historyDir dir) { if (vectLength(history) > 0) { l->history_index = clamp(0, l->history_index + dir, vectLength(history) - 1); strgPo entry = O_STRG(getVectEl(history, l->history_index)); clearBuffer(l->lineBuff); stringIntoBuffer(l->lineBuff, entry); refreshLine(l->firstPos, l->lineBuff); } }
bool intersectSphere(ray R, sphere S) { vect deltaP = {S.x - R.x,S.y-R.y, S.z-R.z}; double part1 = pow(dotProduct(R.u, deltaP),2.0); double part2 = pow(vectLength(deltaP),2.0); double part3 = pow(S.r,2.0); double result = part1 - part2 + part3; if(result<=0) return false; return true; }
/******************************************************************** * This function adds only the crystalline atoms to the superCell * the amorphous ones are handled by makeAmorphous, and makeSpecial ********************************************************************/ void makeSuperCell() { int g,p,iatom,ix,iy,iz,atomCount = 0; // atom *atomPtr; // atomPtr = (atom *)malloc(sizeof(atom)); static double *axCell,*byCell,*czCell=NULL; static double **Mm = NULL, **Mminv = NULL, **Mrot = NULL,**Mr=NULL,**Mr2=NULL; static double **a = NULL,**b= NULL; double maxLength,dx,dy,dz,d,dxs,dys,dzs; atom newAtom; // double xpos,ypos,zpos; int nxmin,nxmax,nymin,nymax,nzmin,nzmax; /* claculate maximum length in supercell box, which is naturally * the room diagonal: */ maxLength = vectLength(&(superCell.ax)); /* maxLength = sqrt(superCell.ax*superCell.ax+ superCell.by*superCell.by+ superCell.cz*superCell.cz); */ if (Mm == NULL) { Mm = double2D(3,3,"Mm"); Mminv = double2D(3,3,"Mminv"); Mrot = double2D(3,3,"Mrot"); Mr = double2D(3,3,"Mr"); Mr2 = double2D(3,3,"Mr"); axCell=Mm[0]; byCell=Mm[1]; czCell=Mm[2]; a = double2D(1,3,"a"); b = double2D(1,3,"b"); } atomCount = superCell.natoms; for (g=0;g<nGrains;g++) { /******************************************************** * if this grain is a crystalline one ... */ if (grains[g].amorphFlag == 0) { dx = grains[g].shiftx/superCell.ax; dy = grains[g].shifty/superCell.by; dz = grains[g].shiftz/superCell.cz; /* find the rotated unit cell vectors .. */ makeCellVect(grains+g, axCell, byCell, czCell); // showMatrix(Mm,3,3,"M"); /////////////////////////////////////////////////////////////////// memset(Mrot[0],0,3*3*sizeof(double)); Mrot[0][0] = 1.0; Mrot[1][1] = 1.0; Mrot[2][2] = 1.0; memcpy(Mr2[0],Mrot[0],3*3*sizeof(double)); memset(Mr[0],0,3*3*sizeof(double)); Mr[0][0] = 1.0; Mr[1][1] = cos(grains[g].tiltx); Mr[1][2] = sin(grains[g].tiltx); Mr[2][1] = -sin(grains[g].tiltx); Mr[2][2] = cos(grains[g].tiltx); matrixProduct(Mrot,3,3,Mr,3,3,Mr2); memcpy(Mrot[0],Mr2[0],3*3*sizeof(double)); // showMatrix(Mrot,3,3,"Mrotx"); memset(Mr[0],0,3*3*sizeof(double)); Mr[1][1] = 1.0; Mr[0][0] = cos(grains[g].tilty); Mr[0][2] = -sin(grains[g].tilty); Mr[2][0] = sin(grains[g].tilty); Mr[2][2] = cos(grains[g].tilty); matrixProduct(Mrot,3,3,Mr,3,3,Mr2); memcpy(Mrot[0],Mr2[0],3*3*sizeof(double)); // showMatrix(Mrot,3,3,"Mrotxy"); memset(Mr[0],0,3*3*sizeof(double)); Mr[2][2] = 1.0; Mr[0][0] = cos(grains[g].tiltz); Mr[0][1] = sin(grains[g].tiltz); Mr[1][0] = -sin(grains[g].tiltz); Mr[1][1] = cos(grains[g].tiltz); matrixProduct(Mrot,3,3,Mr,3,3,Mr2); memcpy(Mrot[0],Mr2[0],3*3*sizeof(double)); // showMatrix(Mrot,3,3,"Mrotxyz"); /////////////////////////////////////////////////////////////////// /* rotateVect(axCell,axCell,grains[g].tiltx,grains[g].tilty,grains[g].tiltz); rotateVect(byCell,byCell,grains[g].tiltx,grains[g].tilty,grains[g].tiltz); rotateVect(czCell,czCell,grains[g].tiltx,grains[g].tilty,grains[g].tiltz); */ inverse_3x3(Mminv[0],Mm[0]); matrixProduct(Mm,3,3,Mrot,3,3,Mr2); memcpy(Mm[0],Mr2[0],3*3*sizeof(double)); // showMatrix(Mm,3,3,"M"); inverse_3x3(Mr2[0],Mm[0]); /* find out how far we will have to go in units of unit cell vectors. * when creating the supercell by checking the number of unit cell vectors * necessary to reach every corner of the supercell box. */ memset(a[0],0,3*sizeof(double)); matrixProduct(a,1,3,Mr2,3,3,b); // showMatrix(Mm,3,3,"M"); // showMatrix(Mminv,3,3,"M"); nxmin = nxmax = (int)floor(b[0][0]-dx); nymin = nymax = (int)floor(b[0][1]-dy); nzmin = nzmax = (int)floor(b[0][2]-dz); for (ix=0;ix<=1;ix++) for (iy=0;iy<=1;iy++) for (iz=0;iz<=1;iz++) { a[0][0]=ix*superCell.ax; a[0][1]=iy*superCell.by; a[0][2]=iz*superCell.cz; matrixProduct(a,1,3,Mr2,3,3,b); // showMatrix(b,1,3,"b"); if (nxmin > (int)floor(b[0][0]-dx)) nxmin=(int)floor(b[0][0]-dx); if (nxmax < (int)ceil( b[0][0]-dx)) nxmax=(int)ceil( b[0][0]-dx); if (nymin > (int)floor(b[0][1]-dy)) nymin=(int)floor(b[0][1]-dy); if (nymax < (int)ceil( b[0][1]-dy)) nymax=(int)ceil( b[0][1]-dy); if (nzmin > (int)floor(b[0][2]-dz)) nzmin=(int)floor(b[0][2]-dz); if (nzmax < (int)ceil( b[0][2]-dz)) nzmax=(int)ceil( b[0][2]-dz); } // nxmin--;nxmax++;nymin--;nymax++;nzmin--;nzmax++; superCell.atoms = (atom *)realloc(superCell.atoms,(superCell.natoms+1+ (nxmax-nxmin)*(nymax-nymin)* (nzmax-nzmin)*grains[g].natoms)* sizeof(atom)); // showMatrix(Mm,3,3,"Mm"); // showMatrix(Mminv,3,3,"Mminv"); printf("Grain %d: range: (%d..%d, %d..%d, %d..%d)\n", g,nxmin,nxmax,nymin,nymax,nzmin,nzmax); dx = grains[g].shiftx; dy = grains[g].shifty; dz = grains[g].shiftz; for (iatom=0;iatom<grains[g].natoms;iatom++) { memcpy(&newAtom,&(grains[g].unitCell[iatom]),sizeof(atom)); // We need to convert the cartesian coordinates of this atom // to fractional ones: b[0][0] = newAtom.x; b[0][1] = newAtom.y; b[0][2] = newAtom.z; matrixProduct(b,1,3,Mminv,3,3,a); newAtom.x = a[0][0]; newAtom.y = a[0][1]; newAtom.z = a[0][2]; //printf("%2d: %d (%g,%g,%g) (%g,%g,%g)\n",iatom,newAtom.Znum, // b[0][0],b[0][1],b[0][2],a[0][0],a[0][1],a[0][2]); for (ix=nxmin;ix<=nxmax;ix++) { for (iy=nymin;iy<=nymax;iy++) { for (iz=nzmin;iz<=nzmax;iz++) { /* atom position in reduced coordinates: */ // a[0][0] = ix+newAtom.x; a[0][1] = iy+newAtom.y; a[0][2] = iz+newAtom.z; a[0][0] = newAtom.x+ix; a[0][1] = newAtom.y+iy; a[0][2] = newAtom.z+iz; matrixProduct(a,1,3,Mm,3,3,b); /* b[0][0] = a[0][0]*Mm[0][0]+a[0][1]*Mm[0][1]+a[0][2]*Mm[0][2]; b[0][1] = a[0][0]*Mm[1][0]+a[0][1]*Mm[1][1]+a[0][2]*Mm[1][2]; b[0][2] = a[0][0]*Mm[2][0]+a[0][1]*Mm[2][1]+a[0][2]*Mm[2][2]; */ /* // same as matrixProduct: b[0][0] = a[0][0]*Mm[0][0]+a[0][1]*Mm[1][0]+a[0][2]*Mm[2][0]; b[0][1] = a[0][0]*Mm[0][1]+a[0][1]*Mm[1][1]+a[0][2]*Mm[2][1]; b[0][2] = a[0][0]*Mm[0][2]+a[0][1]*Mm[1][2]+a[0][2]*Mm[2][2]; */ superCell.atoms[atomCount].x = b[0][0]+dx; superCell.atoms[atomCount].y = b[0][1]+dy; superCell.atoms[atomCount].z = b[0][2]+dz; if ((superCell.atoms[atomCount].x >= 0) && (superCell.atoms[atomCount].x < superCell.ax) && (superCell.atoms[atomCount].y >= 0) && (superCell.atoms[atomCount].y < superCell.by) && (superCell.atoms[atomCount].z >= 0) && (superCell.atoms[atomCount].z < superCell.cz)) { // If this is a sphere: if (grains[g].sphereRadius > 0) { dxs = superCell.atoms[atomCount].x - grains[g].sphereX; dys = superCell.atoms[atomCount].y - grains[g].sphereY; dzs = superCell.atoms[atomCount].z - grains[g].sphereZ; if (dxs*dxs+dys*dys+dzs*dzs < grains[g].sphereRadius*grains[g].sphereRadius) { superCell.atoms[atomCount].dw = newAtom.dw; superCell.atoms[atomCount].occ = newAtom.occ; superCell.atoms[atomCount].q = newAtom.q; superCell.atoms[atomCount].Znum = newAtom.Znum; atomCount++; } } // If this is a straight-edged grain else { for (p=0;p<grains[g].nplanes;p++) { /* printf("hello %d (%g %g %g)\n",g, superCell.atoms[atomCount].x,superCell.atoms[atomCount].y, superCell.atoms[atomCount].z); */ d = findLambda(grains[g].planes+p,&(superCell.atoms[atomCount].z),-1); /* printf("%3d lambda: %g (%g %g %g), (%g %g %g), %d\n",atomCount,d, newAtom.x,newAtom.y,newAtom.z, superCell.atoms[atomCount].x,superCell.atoms[atomCount].y, superCell.atoms[atomCount].z,grains[g].nplanes); */ if (d < 0) break; } /* if all the previous test have been successful, this atom is IN, * which means that we also need to copy the other data elements * for this atom. */ if (p == grains[g].nplanes) { superCell.atoms[atomCount].q = newAtom.q; superCell.atoms[atomCount].dw = newAtom.dw; superCell.atoms[atomCount].occ = newAtom.occ; superCell.atoms[atomCount].Znum = newAtom.Znum; atomCount++; } } // if this is a sphere or not ... } } /* iz ... */ } /* iy ... */ } /* ix ... */ } /* iatom ... */ superCell.natoms = atomCount; } /* end of if !amorph,i.e. crystalline */ } /* g=0..nGrains .. */ /* atomPtr->x = 0.5; atomPtr->y = 0.2; atomPtr->z = 0.7; findLambda(grains[0].planes,&(atomPtr->z),1); */ }
void Game::manageInputs() { // Player 1 if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { _p1.setVelocity(_p1.velocity().x, -1); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { _p1.setVelocity(-1, _p1.velocity().y); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { _p1.setVelocity(1, _p1.velocity().y); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { _p1.setVelocity(_p1.velocity().x, 1); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { _p1.boost(); } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) == false) { _p1.chargeBoost(); } if(_nbPlayer == 2) { // Player 2 if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { _p2.setVelocity(_p2.velocity().x, -1); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { _p2.setVelocity(-1, _p2.velocity().y); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { _p2.setVelocity(1, _p2.velocity().y); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { _p2.setVelocity(_p2.velocity().x, 1); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)) { _p2.boost(); } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift) == false) { _p2.chargeBoost(); } } else { // Sinon, c'est une ia if(_iaUpdateClock.getElapsedTime().asSeconds() >= 0.25f) { sf::Vector2f dep = _ball.extrapolatedPosition() - _p2.position(); float dist = distance(_p2.position().x, _p2.position().y, _ball.extrapolatedPosition().x, _ball.extrapolatedPosition().y); dep/=dist; _p2.setVelocity(dep); _iaUpdateClock.restart(); _oldDep = dep; } else { _p2.setVelocity(_oldDep); } sf::Vector2f p1_ball, p2_ball; p1_ball = _ball.position() - _p1.position(); p2_ball = _ball.position() - _p2.position(); if(vectDotProduct(_goal2.position() - _ball.position(), _ball.velocity()) > 0 && _ball.position().x - 30 < _p2.position().x && _ball.position().x >= 50 && vectLength(_ball.velocity()) != 0) { _p2.setVelocity(vectNormalize(_goal2.position() - _p2.position())); } else if(_ball.position().x < 50) { _p2.setVelocity(_oldDep); } // ========================================== if(_p2.boostValue() > 0 && _boostP2 && distance(_p2.position(), _ball.position()) > 100) _p2.boost(); else _p2.chargeBoost(); if(_p2.boostValue() <= 0 && _boostP2) { _boostP2 = false; } if(_boostP2 == false && _p2.boostValue() == 100) { _boostP2 = true; } } }