We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Simple "follow the mouse"
Page Index Toggle Pages: 1
Simple "follow the mouse" (Read 1926 times)
Simple "follow the mouse"
Jan 29th, 2010, 2:05pm
 
Hi!  I'm new to the forum, and fairly new to processing.

I'm trying to make a simple program where a "cannon's" (really a rectangle) barrel follows the mouse.  The cannon is supposed to be in a fixed position.  The problem that I'm finding is that the head doesn't follow the mouse fully until it gets to 45 degrees.  It jumps to that position when you put the mouse at 45.  It's hard to explain, and I apologize.  Here is the code:

float position;

void setup()
{
 size(400, 400);

}

void draw()
{
 background(255);
 noStroke();
 pushMatrix();
   if (mouseX > 0) {
 position = atan(-mouseY/mouseX);}
 else {
   position = (PI/2);}
 rotate(position);
 fill(0);
 rect(0, 0, 70, 40);
 popMatrix();
}


I hope someone can help me!

-FayBee
Re: Simple "follow the mouse"
Reply #1 - Jan 29th, 2010, 4:01pm
 
Quote:
void setup()
{
  size(400, 400);
  rectMode(CENTER);
}

void draw()
{
  background(255);
  noStroke();
  pushMatrix();
  rotate(atan2(mouseY, mouseX));
  fill(0);
  rect(0, 0, 70, 40);
  popMatrix();
}

Re: Simple "follow the mouse"
Reply #2 - Jan 29th, 2010, 4:28pm
 
THANK YOU SO MUCH! Smiley
Re: Simple "follow the mouse"
Reply #3 - Jan 30th, 2010, 1:38am
 
TfGuy44 wrote on Jan 29th, 2010, 4:01pm:
Quote:
void setup()
{
  size(400, 400);
  rectMode(CENTER);
}

void draw()
{
  background(255);
  noStroke();
  pushMatrix();
  rotate(atan2(mouseY, mouseX));
  fill(0);
  rect(0, 0, 70, 40);
  popMatrix();
}



You know, if you really want to help people learn it's worth giving some kind of explanation for why something works.  In this case the key is the use of atan2 as opposed to atan.  For more info see this.
Re: Simple "follow the mouse"
Reply #4 - Jan 30th, 2010, 7:39am
 
Sometimes it's apparent that the person posting actually needs it explained to them. This was not one of those times. FayBee seemed to have a clear grasp of programming, and can clearly see the changes I made to the small amount of posted code

I do try to post a slightly more detailed answer if it is obvious that they need one. See, for example,  this, or this, or maybe even this.
Re: Simple "follow the mouse"
Reply #5 - Jan 30th, 2010, 9:54am
 
Fair enough; though don't forget that other less experienced programmers might search the forums, come across this and have no idea why your code works Wink
Re: Simple "follow the mouse"
Reply #6 - Feb 2nd, 2010, 4:22pm
 
I was looking to do something very similar and this has helped. Thanks
Re: Simple "follow the mouse"
Reply #7 - Feb 3rd, 2010, 1:28am
 
Thank you so much, i was about to make a topic asking about this too but you saved me the trouble and time. Smiley
Page Index Toggle Pages: 1