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 › The trouble with angles...
Page Index Toggle Pages: 1
The trouble with angles... (Read 343 times)
The trouble with angles...
Feb 20th, 2009, 5:55pm
 
Hi. I'm moving an object around the screen by using an angle and a speed. I'd like it to seek the mouse, by changing it's heading to face my cursor.

I'm having a real tough time calculating the angle it needs to point, and an equal problem having it figure out how to turn towards the target angle!

My code is messy and half baked, but the fundamentals (I hope) are there. The black triangle represents the current heading (angle) and the grey one points to where it thinks my mouse is (doesn't work).

Right now it just turns in circles, meaning that I cannot find a condition that has different outcomes.

HALP

Code:

/* this is messy as crap and half finished
I can't figure out how to calculate the angle the ball should face in order to move towards the mouse
and I can't figure out how to make it turn the shortest way to face said angle!
*/

ball frank;
float fatness=50;

void setup() {
size(350,700);
frank = new ball();
ellipseMode(CENTER);
}

void draw() {
background(255);
frank.move();
}

class ball {
float x;
float y;
float angle;
float speed;
float targetangle;
float anglerate=.02*PI;

ball(){
x=width/2;
y=height/3;
speed=random(3)+2;
angle=random(PI)+.5*PI;
}

void move() {
//move towards current heading
x=x + speed*cos(angle);
y=y + speed*sin(angle);

//draw ellipse
fill(255);
ellipse(x,y,fatness,fatness);

//draw vector line
line( x,y, x + 5*speed*cos(angle), y + 5*speed*sin(angle));

//draw current angle triangle
fill(0);
triangle( x + 5*speed*cos(angle), y + 5*speed*sin(angle), x + 2*speed*cos(angle+.5*PI), y + 2*speed*sin(angle+.5*PI),x + 2*speed*cos(angle-.5*PI), y + 2*speed*sin(angle-.5*PI));

//draw target angle triangle
fill(100,100,100,100);
triangle( x + 5*speed*cos(targetangle), y + 5*speed*sin(targetangle), x + 2*speed*cos(targetangle+.5*PI), y + 2*speed*sin(targetangle+.5*PI),x + 2*speed*cos(targetangle-.5*PI), y + 2*speed*sin(targetangle-.5*PI));

// bounces off left/ right walls, still need to do top and bottom
if((x+speed*cos(angle)>width) || (x<0)) {
angle=angle+2*(.5*PI-angle);
}

//define our target angle DOESNT WORK?!?!
targetangle=PI+atan(1/((mouseX-x)/(mouseY-y)));

//try to make it gradually turn to face the target angle DOESNT WORK!
if (angle>PI) {
angle -=anglerate;
} else {
angle += anglerate;
}

}

void reset() {
x=mouseX;
y=mouseY;
speed=random(2)+3;
angle=random(2*PI);
}

}

void mousePressed(){
frank.reset();
}
Re: The trouble with angles...
Reply #1 - Feb 20th, 2009, 7:08pm
 
hi,

i also had the problems you are facing and they're hard but fun once solved.

to get the desired angle, you calc using atan2(dy,dx).
then you need to wrap the angle across two_pi

here's one solution:

Quote:
float px,py;
float angle, targetangle;
float speed = 2.1;

void setup(){  
  size(500,500);
  px = width/2;
  py = height/2;  
}


void draw(){
  background(255);
  px+=cos(angle)*speed;
  py+=sin(angle)*speed;

  targetangle = atan2(mouseY-py,mouseX-px);  
  float dis = (targetangle - angle);
  // wrap across pi,-pi
  if(dis > PI) dis = dis - TWO_PI;
  if(dis < -PI) dis = dis + TWO_PI;

   angle = angle + dis/20;

  pushMatrix();
  translate(px,py);
  rotate(angle);
  rect(-5,-5,10,10);
  popMatrix();

}



Re: The trouble with angles...
Reply #2 - Feb 20th, 2009, 8:16pm
 
as wrote on Feb 20th, 2009, 7:08pm:
hi,

to get the desired angle, you calc using atan2(dy,dx).
then you need to wrap the angle across two_pi





I just had the biggest 'i am duh' moment of my life.

Thanks for clearing this up.
Page Index Toggle Pages: 1