Code source de balistique


//-------------------------------------------------------------------------
//--    Autheur de ce programme : unité1994 
//--    Editeur de code : p5.js
//--    date : 20/07/2016
//--    Balistique_web : Version : 07-2016.11
//--------------------------------------------------------------------------

var prairie;
var fond_graphique;
var roue;
var socleroue;
var canon;
var socle;
var l1;
var l2;
var l3;
var l4;
var info;
var rectangle;
var feu_vert, feu_rouge;
var flv;
var v0;
var fleche_abscisse, fleche_ord;
var abscisse, ord;
var zero;


//  variables coordonnées du canon sur l'écran
var x_canon = 42;
var y_canon = 772;
var x_socle = 25;
var y_socle = 788;
var x_roue = 40;
var y_roue = 773;

//  Paramètres du projectile
var x;
var y;
var x0;
var y0;
var xm;
var ym;
var t;
var theta;
var vi;
var vix;
var viy;
var g;
var vitesse;
var alphaa;
var porte;
var duree;
var alt_max;
var rayonVecteur;
var tempoAngle1;
var tempoAngle2;
var h;

//  Variables vecteurs
var xrv, yrv ,xfl, yfl, xfl1, yfl1, xfl2, yfl2, x1, y1;
var rfl, rfl1, rfl2;
var anglefl, anglefl1, anglefl2;

//  Boolean pour touches clavier
var am, vp, vm, gr, st, hp, hm, information, pause, feucolor, rafraichir, affiVect;
// variables altitude, at, ap
var ap, alt, at;

//  Boolean pour lancer ou arrêter les méthodes
var project, projectx, blocage_btn, graph_on, mode_change, display_poly, action;

//  Typographie pour affichage angle et vitesse
//  PFont fontA;

//  Animation flamme au bout du canon
var numFrames_flamme      = 3;  // The number of frames in the animation
var images_flamme         = [numFrames_flamme];
var nb_frames_anim_flamme = 0;
var i_anim_flamme         = 0; 
var tempo_anim_flamme     = 175;  
var anim_flamme           = false;

//  formules
var vi_exp = 0, theta_exp = 0, a = 0, b = 0, h = 0, ai = 0; 

var tir_obu;
var tir_stop = true;
var son_couper = true;


function setup() {
  createCanvas(1080, 800);
   
  //  Décors (prairie et fonds graphique)
  //  On charge nos images
  roue                 = loadImage("assets/roue.png");
  canon                = loadImage("assets/canon3.png");
  socle                = loadImage("assets/socle.png");
  socleroue            = loadImage("assets/socleroue.png");
  prairie              = loadImage("assets/prairie.jpg");
  fond_graphique       = loadImage("assets/grille3.jpg");
  l1                   = loadImage("assets/l1.gif");
  l2                   = loadImage("assets/l2.gif");
  l3                   = loadImage("assets/l3.gif");
  l4                   = loadImage("assets/l4.gif");
  info                 = loadImage("assets/info.png");
  rectangle            = loadImage("assets/paneau.jpg");
  images_flamme[0]     = loadImage("assets/canon1.png");
  images_flamme[1]     = loadImage("assets/canon11.png"); 
  images_flamme[2]     = loadImage("assets/canon111.png");
  feu_vert             = loadImage("assets/feuvert.png");
  feu_rouge            = loadImage("assets/feurouge.png");
  v0                   = loadImage("assets/v0.png");
  fleche_abscisse      = loadImage("assets/fleche_abscisse.png");
  fleche_ord           = loadImage("assets/fleche_ord.png");
  abscisse             = loadImage("assets/abs.png");
  ord                  = loadImage("assets/ord.png");
  zero                 = loadImage("assets/zero.png");
  son_on               = loadImage("assets/son_on.png");
  son_off              = loadImage("assets/son_off.png");
  fleche               = loadImage("assets/fleche.jpg");
  tir_obu              = loadSound("assets/tir_obu.mp3");
  
  
  //  Paramètres projectile
  g            = 9.8;
  vitesse      = 100;
  alphaa       = 45;
  ym           = 0;

  
  blocage_btn   = true;
  project       = false;
  projectx      = true;
  graph_on      = true;
  mode_change   = true;
  display_poly  = false;
  action        = false; 
  information   = false;
  pause         = true;
  feucolor      = true;
  rafraichir    = true;
  affiVect      = true;
}

function draw() {
  
  if(mode_change) {
    image(prairie, 0, 0);
    display_canon();
    display_socleroue(); 
   
    if(anim_flamme) {
      display_animation_flamme(x_canon, y_canon); 
    }
    
  }  
  
  else {
      if(rafraichir) {
        background(255);
        vecteur(); 
        porte = 0;
        alt_max = 0;
        duree = 0;
      }
  }

  move_angle();
  move_vecteur_vitesse();
  hauteur();
 
  if(mode_change) {
      fill(255);
      rect(0, 0, width, 38);
      
      if(feucolor) {
        image(feu_vert, width - 74, 0);
      } 
      else {
        image(feu_rouge, width - 74, 0);
      }  
      
      if(son_couper) {
        image(son_on, width - 125, 0);
      }
      else {
        image(son_on, width - 125, 0);
        image(son_off, width - 125, 0);
      }
  }
  
  if(!mode_change) {
     fill(255);
     rect(0, 0, width, 38);
      if(feucolor) {
        image(feu_vert, width - 74, 0);
      } 
      else {
        image(feu_rouge, width - 74, 0);
      }
  }
  
  if(!mode_change) {
    display_repere();
  }
  display_angle_alphaa();
  display_vecteur_vitesse();
  
  if(!mode_change) {
    display_porte();
    display_alt_max();
    display_duree();
  }
  
  if (action) {
    if(mode_change) {
      projectile_prairie();
    }
    else { 
      projectile_graphique(); 
      polynome(); 
    }
  }
  
  display_info();
  
   if (mouseX > 1 && mouseX < 39 && mouseY > 1 && mouseY < 46) {
   cursor(HAND);
  } 
  else {
    cursor(ARROW);    
  }
}
//*************************************************************
//  Affichage canon
function display_canon() {
  push();
  imageMode(CENTER);
  translate(x_canon, y_canon);
  rotate(radians(alphaa));
  image(canon, 0, 0);
  imageMode(CORNERS);
  pop();
}
//  Affichage socle plus roue
function display_socleroue() {
  imageMode(CENTER);
  image(socleroue, 34, height - 27);
  image(roue, x_roue , y_roue);
  fill(84, 46, 14);
  ellipse(40, 772, 8, 8);
  noFill();
  imageMode(CORNER);
}

function display_angle_alphaa() {
  textFont("Georgia", 20);
  fill(254, 135, 18);
  text("alpha :", 78, 26);
  var m = map(alphaa, 0, 89.99, 89.99, 0);
  m_precision = nfc(m, 2); 
  text(m_precision + "°", 148, 26);
}

function display_vecteur_vitesse() {
  fill(254, 135, 18);
  textFont("Georgia", 20);
  text("V0 : ", 260, 26);
  image(fleche, 260, 2);
  text(vitesse + " m/s", 301, 26);
}

function display_porte() {
  fill(254, 135, 18);
  textFont("Georgia", 20);
  text("Porté : ", 430, 26);
  if(porte < 1000) {
  aporte = nfc(porte, 2);  
  text(aporte, 495, 26);
  text("m", 570, 26);
  }
  else
  {
    aporte = nfc(porte, -3);   
    text(aporte, 495, 26);
    text("km", 560, 26);
  }
}

function display_alt_max() {
  fill(254, 135, 18);
  textFont("Georgia", 20);
  text("Alt max : ", 620, 26);
  if(alt_max < 1000) {
    alt = nfc(alt_max, 2);  
    text(alt, 703, 26);
    text("m", 780, 26);
  }
  else
  {
    alt = nfc(alt_max, -3); 
    text(alt, 703, 26);
    text("km", 765, 26);
  }
}

function display_duree() {
  fill(254, 135, 18);
  textFont("Georgia", 20); 
  text("Durée : ", 820, 26);
  at = nfc(duree, 2);
  text(at, 895, 26);
  text("sec", 955, 26);
}

function display_info() {
  image(info, 1, 0);
    if(information) {
      image(rectangle, width/2 - 305, 125);
  }
}
//**************************************************************
// Événements pressent touches clavier 
//**************************************************************/
 
function keyPressed() {
  //  Si on presse la touche p ou P
  //  on fait une pause dans le jeu
  if (( key == 'p' ) || ( key == 'P' )) {
    pause =! pause;
    noLoop();
    if (pause === false) loop();
  }
  
  //  (+) pour hauteur
  if (key == 'h' || ( key == 'H' )) { 
    hp = true;
    rafraichir = true;
    affiVect = true;
  }   
    //  (-) pour hauteur
  if (key == 'b' || ( key == 'B' )) { 
    hm = true;
    rafraichir = true;
    affiVect = true;
  }     
 
  
  if (blocage_btn) {
    //  Vecteur vitesse plus (vers 100 m/s)
    if (keyCode == UP_ARROW) { 
      vp = true;
      rafraichir = true;
      affiVect = true;
    } else 
      //  Vecteur vitesse moins (vers 0 m/s)
    if (keyCode == DOWN_ARROW) { 
      vm = true;
      rafraichir = true;
      affiVect = true;
    } else 
      // Angle canon haut (vers 90 degrées)
    if (keyCode == LEFT_ARROW) { 
      ap = true;
      rafraichir = true;
      affiVect = true;
    } else 
      //  Angle canon bas (vers 0 degrées)
    if (keyCode == RIGHT_ARROW) { 
      am = true;
      rafraichir = true;
      affiVect = true;
    } 
  
  //***********************************************************
  
      if ( key == ' ' ) {
      st = true;
      background(255);
      affiVect = false;
      
      //  Lance action projectile dans mode prairie
      if (mode_change) {
        anim_flamme = false; 
        project  = true; 
        projectx = true;
        action   = true;
        t = 0;
        nb_frames_anim_flamme = millis();
        information = false;
        if(son_couper) {
          tir_obu.play();
        }
      }
      else {
        //  Lance action projectile dans mode graphique
        project      = true; 
        projectx     = true;
        action       = true;
        t = 0;
        background(255);
        rafraichir   = false;
        information  = false;
       
      }
    }
    //  change de mode prairie ou graphique
    if (( key == 'g' ) || ( key == 'G' )) {
      gr = true;
      mode_change =! mode_change;
      display_poly =! display_poly;
    }
  }
}

//**************************************************************
// Événements relâchent touches clavier 
//**************************************************************

function keyReleased() {
    if (key == 'h' || ( key == 'H' )) { 
      hp = false;
    }   
      //  - pour hauteur
     if (key == 'b' || ( key == 'B' )) { 
      hm = false; 
    }     

    if (keyCode == UP_ARROW) 
    {
      vp = false;
    } else if (keyCode == DOWN_ARROW)
    {
      vm = false;
    } else if (keyCode == LEFT_ARROW)
    {
      ap = false;
    } else if (keyCode == RIGHT_ARROW)
    { 
      am = false;
    }
    if ( key == ' ' ) {
      st = false;
    }
 
} 
//***********************************************************************  
//  Événements cliquent souris
  function mousePressed() {
    if (mouseX > 1 && mouseX < 39 && mouseY > 1 && mouseY < 46) {
      information = true;
    }
    if (mouseX > 807 && mouseX < 842 && mouseY > 127 && mouseY < 161) {
      information = false; rafraichir = true;
    }
    if (mouseX > 955 && mouseX < 993 && mouseY > 0 && mouseY < 38) {
      son_couper =! son_couper;
    }
  }
//************************************************************************
//  Paramètre pour l'angle alphaa
function move_angle() {

  if (ap == true) { 
    alphaa -= PI/4;
    if (alphaa < 0) { 
      alphaa = 0;
    }
  }

  if (am == true) { 
    alphaa += PI/4;
    if (alphaa > 89.99) { 
      alphaa = 89.99;
    }
  }

  theta= -1 * (alphaa - 89.99);
      
    if(mode_change) {
      x0 = 130 * cos(radians(theta));
      y0 = 130 * sin(radians(theta));
    }
    else {      
        x0 = 53;
        y0 = ym;       
      }
}

function hauteur() {
   if(hp == true) {
     ym += 1;
     if(ym > 350) ym = 350;
     h += 1;
     if(h > 350) h = 350;
   }
   
   if(hm == true) {
     ym -= 1;
     if(ym < 0) ym = 0;
      h -= 1;
     if(h < 0) h = 0;
   }
}

//  Paramètre pour le vecteur vitesse
function move_vecteur_vitesse() {

  if (vp == true) {
    vitesse += 1 ;
    if (vitesse > 200) vitesse = 200;
  } 

  if (vm == true) {
    vitesse -= 1 ;
    if (vitesse < 1) vitesse = 1;
  }
  
  var m = map(vitesse, 1 , 200, 1, 250);
  rayonVecteur = m;
  vi = vitesse;
}

function polynome() {
    
    //   calcul et affichage Polynôme
    vi_exp = pow(vi, 2);
    theta_exp = pow(cos(radians(theta)), 2);
    
    a = -g/(2 * vi_exp * theta_exp);
    bb = tan(radians(theta));
    
    b = nfc(bb, 6); 
    
    ai = nfc(a, 6); 
    
    if(theta > 89.5)  {
      ai = nfc(a, 0);
    }
    
    
      fill(255);
      rect(70,742,800,50);
      fill(28, 122, 254);
      textFont("Georgia", 20); 
      text("Polynômes second degré:", 70, 776);
      textFont("Georgia", 20);
      image(l1 ,317, 762);
      text(ai, 382, 776);
      image(l2 ,587, 751);
      image(l4 ,612, 759);
      text(b, 645, 776);
      image(l3, 775, 764);
      image(l4 , 797, 759);
      text(h, 827, 775);
   
    //**********************************************
  
    var anglecos = cos(radians(theta));
    var anglesin = sin(radians(theta));
 
    //  Calcul porté
    if(h == 0) {
      porte = (vi_exp / g) * sin(radians(2 * theta));
    }
    else
    {
      porte =( (vi * anglecos / g) * ( vi * anglesin + sqrt(vi * anglesin * vi * anglesin + 2 * g * h)));
    }
    
    //***********************************************
   
    //  Calcul durée
    duree = (vi * anglesin + sqrt(vi * anglesin * vi * anglesin + 2 * g * h)) / g;
    
    //***********************************************
   
    //  Calcul altitude
    alt_max = (0.5 * (vi * anglesin * vi * anglesin) / g + h); 
}
   
//****************************************************

function projectile_graphique() {
  blocage_btn = false;
  feucolor = false;
  
  vix = vi * cos(radians(theta));
  viy = vi * sin(radians(theta));
  
  // Vitesse d'écoulement du temps
  t += 0.099999999;
  
  // Positionnement projectile
  if(projectx) {
    //   MRU  (mouvement rectiligne uniforme sur l'axe des x)
    x = x0 +  vix * t; 
    }
    //   MRUA (mouvement rectiligne uniforme accelléré sur l'axe des y)
    y = y0 - 0.5 * g * t * t  +  viy * t;   
  
  if (y < 0) { 
    y =  0; 
    projectx = false; 
    blocage_btn = true;
    feucolor = true; 
  }

  push();
  translate(x, height - y - 60);
  fill(79, 173, 38);
  strokeWeight(1);
  imageMode(CENTER);
  ellipse(0, 0, 5, 5);
  noStroke();
  noFill();
  imageMode(CORNER);
  pop();
 
  if(height - y0 > 790) {
    fill(255);
    rect(x0 - 49, height - y0 - 68, 18, 16);
    }
    else {
      fill(255);
      rect(x0 - 49, height - y0 - 68, 30, 16);
    }
    textSize(16);
    fill(255);
    rect(0, 0, 30,750);
    fill(24, 171, 225);
    text(h, x0 - 49, height - y0 - 55);
}

//******************************************************************

function projectile_prairie() {
 
  anim_flamme  = true;
  blocage_btn = false;
  feucolor = false;
  
  vix = vi * cos(radians(theta));
  viy = vi * sin(radians(theta));

  // Vitesse d'écoulement du temps
  t += 0.099999999;
  
  // Positionnement projectile
  if(projectx) {
    //   MRU  (mouvement rectiligne uniforme sur l'axe des x)
    x = x0 +  vix * t; 
    }
    //   MRUA (mouvement rectiligne uniforme accelléré sur l'axe des y)
    y = y0 - 0.5 * g * t * t  +  viy * t;   
    
  if (y < -14 ) { 
    y =  -14; 
    projectx = false; 
    blocage_btn = true;
    feucolor = true;
    rafraichir = true;
  }
 
  push();
  translate(x + 41, height - y - 23);
  fill(64);
  imageMode(CENTER);
  ellipse(0, 0, 16, 16);
  noFill();
  imageMode(CORNER);
  pop();
}
//********************************************************************************************************
//  Animation flamme au bout du canon
function display_animation_flamme(x, y) { 
  if ( millis() <= ( nb_frames_anim_flamme + tempo_anim_flamme )) { 
    
    push();
    imageMode( CENTER );
    translate(x, y);
    rotate(radians(alphaa));
    image( images_flamme[i_anim_flamme], 0, 0 + 3 );
    imageMode( CORNER  );
    pop();
    
    imageMode(CENTER);
    image(roue, x_roue, y_roue);
    fill(84, 46, 14);
    ellipse(40, 772, 8, 8);
    noFill();
    imageMode(CORNER);
  }

  if ( millis() == nb_frames_anim_flamme + tempo_anim_flamme ) {
    nb_frames_anim_flamme += tempo_anim_flamme;
    i_anim_flamme++;
    if ( i_anim_flamme == 3 ) {
      i_anim_flamme = 0;
      anim_flamme = false;
    }
  }
}

//*********************************************************************

function display_repere() {
  strokeWeight(1);
  stroke(0);
  for(i = 78; i <= width; i += 20) {
    for(j = 38; j <= height; j += 20) {
     if(j > height - 80) break; 
     point(i, j); 
     point(i+1,j);
    }
  }
  
  fill(255);
  stroke(24, 171, 225);
  strokeWeight(2);
  line(53,height - 25, 53, - height);
  line(25, height - 60, width, height - 60);
  strokeWeight(1);
  noStroke();
  fill(255);
  rect(width - 120, height - 110, 60, 22);
  image(abscisse, width - 120, height - 110);
  rect(65, height - 697, 60, 22);
  image(ord, 65, height - 700);
  noFill();
  image(zero, 25, height - 50);
  image(fleche_ord, 30, -1);
  image(fleche_abscisse, width - 30, height -83);
  
}

//***********************************************************************

function vecteur() {
    
    xrv  = rayonVecteur * cos(radians(theta));
    yrv  = rayonVecteur * sin(radians(theta));
    xfl  = rayonVecteur * cos(radians(theta));
    yfl  = rayonVecteur * sin(radians(theta));
   
    tempoAngle1 = 210 - theta;
    xfl1  = 19 * cos(radians(tempoAngle1));
    yfl1  = 19 * sin(radians(tempoAngle1));
    
    tempoAngle2 = 145 - theta;
    xfl2  = 19 * cos(radians(tempoAngle2));
    yfl2  = 19 * sin(radians(tempoAngle2));
  
    //  Affichage flêche
    push();
    translate(x0 , (-1 * y0) - 61);
    strokeWeight(3);
    stroke(24, 171, 225);
    line(0, height, xrv, height - yrv);
    image(v0, xfl - 45, height - yfl - 45);
    pop();
    
    //  Affichage bout de la flêche
    push();
    translate(xfl + x0, height + (-1 * y0) - yfl - 61);
    strokeWeight(3);
    stroke(24, 171, 225);
    line(0,0,xfl1, yfl1);
    line(0,0,xfl2, yfl2);
    noStroke();
    pop();
    fill(255);
    rect(0, 0, 30,750);
    textSize(16);
    fill(24, 171, 225);
    text(h, x0 - 49, height - y0 - 55);
 }
 
Flèche pour remonter la page web