Loading...
Logo
Processing Forum
Hi, I am in a beginner programming class, my final exam is Monday, when going over the review, I am having trouble with this one problem.. I need to make a barbell(line with an ellipse at each end) that rotates around its center, follow the mouse around the window. I have put together code for the barbell to rotate in the center of the screen. Any attempt to get the barbell to follow the mouse has been a disaster to the code...  Any help or tips would be appreciated thank you...


int lineCenterX = 150;
int lineCenterY = 150;
int lineRadius = 30;
float angle = 0.0;
void setup ()
}
  size(300, 300, P3D);
  background(150);
  fill(150, 170, 200);
{
void draw ()
}
  background(150);
  
  if (lineCenterX > 0)
}  
    translate(lineCenterX, lineCenterY);
    rotate(radians(angle));
    line(-1*lineRadius, 0, lineRadius, 0);
    ellipse(-1*lineRadius, 0, 8, 8);
    ellipse(lineRadius, 0, 8, 8);
    angle += 0.8;
{  
{

Replies(4)

just draw it at the mouse position

    translate( mouseX,  mouseY);

btw first open  the curly bracket then close it }
Thanks alot, all of the braces were reversed when I pasted it? Got it though.
I have a similar problem.. Lets say I need to make the barbell appear in the position where the mouse is clicked, while continuing to rotate.(in other words, once I click somewhere on the screen, 
the baton appears centered at that position and rotates around that point until I click somewhere else).


Thanks!
simple create 2 variables.
lets call them x and y
set them to where your barbell should appear first in setup.

then draw them at that location using translate(x,y);

now create the function

void mouseReleased(){

}

and there you update your x and y variables, with the current mouse position.
thats it.

I didnt post a working code example. give it a try, its actually not that hard.