/*Alcys_RungeKutta4 ******************* IL s'agit de résoudre pas à pas l'équation différentielle F=m*dr2/dt2 de la mécanique classique dans un cas simple :la loi de Kepler. La méthode utilsée est celle RungeKutta à l'ordre 4. */ Vecteur centre; float angle,angleX,angleY,angleZ; RK4 rk; public void setup() { rk=new RK4(new Vecteur(200,300,0),new Vecteur(15,-10,0)); size(800,600,P3D); framerate(20); angle=0; centre=new Vecteur(400,300,-700); } public void draw() { angle+=0.1f; background(210*cos(angle/5),210*sin(angle/3),240*cos(angle/4)); directionalLight(100, 30, 125, 0, 0, -1); ambientLight(100+150*sin(angle/5),100+50*cos(angle/3),150+100*sin(angle/4)); translate(centre.x,centre.y,centre.z); tourner(); if (rk.pos.length()<50) { println("trop proche du centre d'attraction"); } else { rk.evoluer(); translate(rk.pos.x,rk.pos.y,rk.pos.z); rotate(angle); afficherSphere(100); } } public void afficherRepere(){ noStroke(); for(int i=1; i<11;i++){ fill(150,150,5*i+200,35-i); ellipse(0,0,140*i,140*i); } fill(255,0,0,15); rect(-500,-500,1000,1000); rotateY(HALF_PI); for(int i=1; i<11;i++){ fill(5*i+200,150,150,35-i); ellipse(0,0,140*i,140*i); } fill(0,0,255,15); rect(-500,-500,1000,1000); } void tourner(){ if(mousePressed==true) { angleZ+=(mouseX/50-angleZ)/50; } else { angleX+=(mouseY/50-angleX)/40; angleY+=((mouseX+mouseY)/100-angleY)/40; } rotateZ(angleZ); rotateY(angleY); rotateX(angleX); afficherRepere(); } void afficherSphere(float rayon){ pushMatrix(); for(int i=0;i<8;i++){ float co=(float)Math.cos(PI/8*i); float si=(float)Math.sin(PI/8*i); fill(130+ 15*abs(cos(angle/2))*i,15*abs(cos(angle))*i,130+15*abs(sin(angle/2))*i); afficherCercle(co,0,si,rayon,0.0f,0.0f,0.0f); afficherCercle(0.0f,1.0f,0.0f,rayon*si,0.0f,rayon*co,0.0f); } popMatrix(); } // ******************************************* void afficherCercle(float nx,float ny,float nz,float r,float cx,float cy,float cz){ pushMatrix(); ellipseMode(CENTER); translate(cx,cy,cz); rotateZ(atan2(ny,nx)); rotateY(atan2(sqrt(nx*nx+ny*ny),nz)); ellipse(0, 0, 2*r, 2*r); popMatrix(); }