rotating arrow depending on mouseX and mouseY

edited January 2017 in Questions about Code

Hi all, neeedd a little help with code. I want to rotate arrows toward the mouse postion. Can anyone hlp pls

` PVector mouse, center; int scalar;

void setup(){
  size(500,500);
  pixelDensity(2);

  background(255);
  scalar=20;
  color(255,0,0);
  strokeWeight(0.5);

}


void draw(){
    background(255);
    mouse = new PVector(mouseX,mouseY);
    center = new PVector(width/2,height/2);
    mouse.sub(center);
    mouse.normalize();
    mouse.mult(15);
    for(int i=0; i<width; i+=scalar-5){
    for(int j=0; j<height; j+=scalar+10){
    pushMatrix();
    translate(i,j);
  line(0, 0, mouse.x, mouse.y);
  translate(mouse.x,mouse.y);
  //float a = PVector.angleBetween(center, mouse);
   float angle = atan2(mouseY-100, mouseX-100);

  rotate(angle);
  line(0, 0, -4, -4);
  line(0, 0, +4, -4);
  popMatrix();
    }
  }

}`

Screen Shot 2017-01-19 at 17.28.58

Answers

  • edited January 2017

    Cleaned up the code here, commented out two lines that Processing said had errors. What do you mean in your question exactly? You want all arrows pointing in the direction of the cursor, or you want every arrow to rotate simultaneously as you move the mouse (which it kinda does now)?

    PVector mouse, center; 
    int scalar;
    
    void setup() { 
      size(500, 500); 
      //pixelDensity(2);
      background(255); 
      scalar=20; 
      color(255, 0, 0); 
      strokeWeight(0.5);
    }
    
    void draw() { 
      background(255);
      mouse = new PVector(mouseX, mouseY);
      center = new PVector(width/2, height/2);
      mouse.sub(center); 
      mouse.normalize(); 
      mouse.mult(15); 
      for (int i=0; i<width; i+=scalar-5) { 
        for (int j=0; j<height; j+=scalar+10) { 
          pushMatrix(); 
          translate(i, j); 
          line(0, 0, mouse.x, mouse.y); 
          translate(mouse.x, mouse.y);
          //float a = PVector.angleBetween(center, mouse);
          //float angle = atan2(mouseY-100, mouseX-100);
          //rotate(angle); 
          line(0, 0, -4, -4); 
          line(0, 0, +4, -4); 
          popMatrix();
        }
      }
    }
    
  • Answer ✓

    Format your code. Edit post, select code and hit ctrl+o. Ensure there is an empty line above and below your code.

    Here is the code:

    PVector mouse, center; 
    int scalar;
    
    void setup() { 
      size(500, 500); 
      //pixelDensity(2);
    
      background(255); 
      scalar=20; 
      color(255, 0, 0); 
      strokeWeight(0.5);
    }
    
    void draw() { 
      background(255); 
      mouse = new PVector(mouseX, mouseY); 
      center = new PVector(width/2, height/2); 
      mouse.sub(center); 
      //mouse.normalize(); 
      //mouse.mult(15); 
    
      pushStyle();
      fill(255, 25, 25);
      ellipse(width/2, height/2, 5, 5);
      popStyle();
    
      for (int i=0; i<width; i+=scalar-5) { 
        for (int j=0; j<height; j+=scalar+10) { 
          pushMatrix(); 
          translate(i, j); 
    
          float a = atan2(mouse.y,mouse.x);       
          rotate(a); 
    
          line(0, 0, 20, 0);
          line(20-4, -4, 20, 0); 
          line(20-4, +4, 20, 0); 
          popMatrix();
        }
      }
    }
    

    Kf

  • edited January 2017

    ok. sorry for bad formatting first post here. @Tess_the_third . Yes, i want an arrow pointing to the position of the mouse. Right now, if you run the code, two lines that form the tip of the arrow dont rotate.

Sign In or Register to comment.