• fullscreen
  • Alcys_Attractor3D.pde
  • Attracteur.pde
  • Button2D.pde
  • ClickButton.pde
  • Fusee.pde
  • /**Alcys_Attractor3D
      *
      *  jacques Maire
      * 2 mai 2012
      */
    
    
    import remixlab.proscene.*;
    
    
    Scene scene;
    Fusee lanceur;
    ArrayList lista;
    Button2D button1, button2;
    void setup() {
         size(890, 700, P3D);
         scene=new Scene(this); 
         scene.setGridIsDrawn(false);
         scene.setAxisIsDrawn(false);
         scene.camera().setPosition(new PVector(0, 0, 2200));
         lista=new ArrayList();
         lista.add(new Attracteur(new PVector(0, 0, 0)));
         lista.add(new Attracteur(new PVector(500, -400, 300)));
         lista.add(new Attracteur(new PVector(-700, -200, -700)));
         lanceur=new Fusee();
         button1 = new ClickButton(scene, new PVector(10, 10), "  attracteur +  ", 16, true);
         button2 = new ClickButton(scene, new PVector((10 + button1.myWidth + 5), 10), "  attractor -", 16, false);
    }
    void draw() {
         background(255);
    
         directionalLight(255, 255, 255, 2, -1, -1);
         directionalLight(255, 255, 255, 0, 1, -1);
         scene.drawGrid(2000, 20);
         fill(255, 255, 0);
         noStroke();
         for (int i=0;i<lista.size();i++) {
              pushMatrix();
              ((Attracteur)lista.get(i)).repere.applyTransformation();
              sphere(20);
              popMatrix();
         }
         lanceur.draw();
         button1.display();
         button2.display();
    }
    void addAttracteur() {
         Attracteur att = new Attracteur();  
         lista.add(att);
         lanceur.initialiser();
    }
    
    void removeAttracteur() {
         if (lista.size()>1) {
              scene.removeFromMouseGrabberPool(((Attracteur)lista.get(0)).repere);
              lista.remove(0);
              lanceur.initialiser();
         }
    }
    
    PVector comb(float t1, PVector v1, float t2, PVector v2) {
         PVector res=PVector.add(PVector.mult(v1, t1), PVector.mult(v2, t2));
         return res;
    }
    
    void ligne(PVector u, PVector v) { 
         line(u.x, u.y, u.z, v.x, v.y, v.z);
    }
    
    
    class Attracteur {
         InteractiveFrame repere;
    
         Attracteur() {
              repere=new InteractiveFrame(scene);
              repere.setPosition(new PVector(random(-1000, 1000), random(-1000, 1000), random(-1000, 1000)));
         }
         Attracteur(PVector pos) {
              repere=new InteractiveFrame(scene);
              repere.setPosition(pos);
         }
    }
    
    
    /**
     * Button 2D.
     * by Jean Pierre Charalambos.
     * 
     * Base class of "2d buttons" that shows how simple is to implement
     * a MouseGrabber which can enable complex mouse interactions.
     */
    
    public class Button2D extends MouseGrabber {
      String myText;
      PFont myFont;
      int myWidth;
      int myHeight;  
      PVector position;
      
      Button2D(Scene scn, PVector p, int fontSize) {
        this(scn, p, "", fontSize);
      }
    
      Button2D(Scene scn, PVector p, String t, int fontSize) {
        super(scn);
        position = p;
        myText = t;    
        myFont = createFont("FFScala", fontSize);
        textFont(myFont);
        textMode(SCREEN);
        textAlign(CENTER);
        setText(t);    
      }
      
      void setText(String text) {
        myText = text;
        myWidth = (int) textWidth(myText);
        myHeight = (int) (textAscent() + textDescent());
      }
    
      void display() {
        pushStyle();
        if(grabsMouse())
          fill(255,0,0);
        else
          fill(100);
        text(myText, position.x, position.y, myWidth, myHeight);
        popStyle();
      }
    
      void checkIfGrabsMouse(int x, int y, Camera camera) {
        // Rectangular activation area
        setGrabsMouse( (position.x <= x ) && ( x <= position.x + myWidth ) && (position.y <= y ) && ( y <= position.y + myHeight ) );
      }
    }
    
    public class ClickButton extends Button2D {
         boolean addBox;
    
         public ClickButton(Scene scn, PVector p, String t, int fontSize, boolean addB) {
              super(scn, p, t, fontSize);
              addBox = addB;
         }
    
         void mouseClicked(Scene.Button button, int numberOfClicks, Camera camera) {
              if (numberOfClicks == 1) {
                   if (addBox)
                        addAttracteur();
                   else
                        removeAttracteur();
              }
         }
    }
    
    
    class Fusee {
    
         ArrayList trajet;
         InteractiveFrame rampe0, rampe1, fusee;
         PVector vitesse;
         float masse0, masse1, tempo;//les masses des attracteurs sont identiques (à modifier?)
         Fusee() {
              rampe0=new InteractiveFrame(scene);
              rampe1=new InteractiveFrame(scene);
              fusee=new InteractiveFrame(scene);
              rampe0.setPosition(new PVector(640, 260, -200));
              rampe1.setPosition(new PVector(820, -100, -110));
              fusee.setPosition(rampe1.position().get());
              vitesse=comb(0.01, rampe1.position(), -0.01, rampe0.position());
              masse0=10000;
              masse1=10 ;
              tempo=4;
              trajet= new ArrayList();
         }
    
         void draw() {
              PVector v=fusee.position();
              PVector at1, ww;
              PVector force=new PVector(0, 0, 0);
              for (int i=0;i<lista.size();i++) {
                   InteractiveFrame frame=((Attracteur)lista.get(i)).repere;
                   if (frame.grabsMouse()&&mousePressed) {
                        initialiser();
                   }
                   ww=comb(-1, v, 1, frame.position());
                   force=comb(1, force, masse0*masse1/(ww.dot(ww)), ww);
              }
              vitesse=comb(1, vitesse, tempo/masse0, force);
              fusee.setPosition(comb(1, fusee.position(), tempo, vitesse));
              pushMatrix();
              fusee.applyTransformation();
              noStroke();
              fill(255, 0, 0);
              sphere(5);
              popMatrix();
              pushMatrix();
              rampe0.applyTransformation();
              fill( 0, 0, 255); 
              noStroke();
              sphere(20);
              popMatrix();
              pushMatrix();
              rampe1.applyTransformation();
              sphere(20);
              popMatrix();
              trajet.add(fusee.position().get());
              dessineTrajet();
    
              stroke(0);
              ligne( rampe0.position(), rampe1.position());
              if ((rampe1.grabsMouse()|| rampe0.grabsMouse())&&mousePressed)initialiser();
             // println(rampe0.position()+"  "+rampe1.position());
         }
         void dessineTrajet() {
              if (trajet.size()>5000)trajet.remove(0);
              stroke(0, 0, 150); 
              strokeWeight(1);
              if (trajet.size()>1) {
                   for (int i=0;i<trajet.size()-1;i++)ligne((PVector)trajet.get(i), (PVector)trajet.get(i+1));
              }
         }
    
         void initialiser() { 
              trajet.clear();
              vitesse=comb(0.01, rampe1.position(), -0.01, rampe0.position());
              fusee.setPosition(comb(0.5, rampe1.position(), 0.5, rampe0.position()));
         }
    }
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    jacques maire

    Attractor 3D

    Add to Faves Me Likey@! 2
    You must login/register to add this sketch to your favorites.

    Click right and drag spheres

    Built with Proscene

    You need to login/register to comment.