We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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();
}
}
}`
Answers
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)?
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:
Kf
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.