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 & HelpSyntax Questions › brain hurts too much to solve this....
Page Index Toggle Pages: 1
brain hurts too much to solve this.... (Read 587 times)
brain hurts too much to solve this....
Sep 6th, 2005, 11:00am
 
so i have a object that's rotating, based on it's position and the mouse so it would face the mouse, i'm using atan2 but when you position the mouse directly behind the object, horizontally, it bounces the other way instead of just following through.  i know this is a simple solution, but i can't think of any right now.  help?



Fish fishies [];
void setup(){
size(400,400);
fishies=new Fish[9];
for(int i=0; i<9; i++){
 fishies[i]=new Fish(i);
}
framerate(30);
noStroke();
}
void draw(){
background(0);
 for(int i=0; i<9; i++){
 fishies[i].move();
   fishies[i].draw();
 }
}
class Fish
{
 float x,y,r,xv,yv,xa,ya,xd,yd;
 int i;
 int d;
 Fish(int i){
 this.i=i;
 this.x=30+(i*10);
 this.y=100;
 this.r=0;
 }
 void move(){

 if(i==0){
 
 if(mouseY!=0){
   r+=((atan2(mouseY-y,mouseX-x))-r)/10;
   
   println(degrees(r));
   }
 if(i!=0){
 if(dist(fishies[i-1].x,fishies[i-1].y,this.x, this.y)<5){
     
}
}
}
void draw(){
pushMatrix();
 translate(x,y);
 pushMatrix();
 rotate(r);
 rect(0,0,10,20);
 popMatrix();
 popMatrix();
 }
   
   }
Re: brain hurts too much to solve this....
Reply #1 - Sep 6th, 2005, 7:37pm
 
Maybe have a look here, very similar thing:

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Syntax;action=display;num=1124476954

Anyway I think the mistake occurs because you "fade" the rotation value, and it always takes the "shortest" way to the mouse...it doesnt happen if you delete the "/10" in the "r+=" assignment.
Re: brain hurts too much to solve this....
Reply #2 - Sep 7th, 2005, 6:53pm
 
Came up with a solution to your problem last night, as I had always had this problem myself.

Already IMed it to you, but I'll post the solution here to share with everyone.

Jay. You owe me a cute japanese girl or three beers.



Solution:
http://users.design.ucla.edu/~mflux/p5/angexample/

That's pretty much it. It allows you to keep adding angles as you rotate, dialing upwards or downwards into the negative. This way it keeps your numbers on a scale rather than wrap between 0 and 360.

The following code does the trick:

Code:

mousePos=new vec(mouseX,mouseY);
float angDiff=getAng(center,mousePos)-ang;
ang=getAng(center,mousePos);

if(angDiff>180)
{
angDiff=angDiff-360;
}
if(angDiff<-180)
{
angDiff=angDiff+360;
}
totalAng+=angDiff;



totalAng will give you the final value.
Page Index Toggle Pages: 1