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 › Trigonometry question...maybe
Page Index Toggle Pages: 1
Trigonometry question...maybe (Read 457 times)
Trigonometry question...maybe
May 21st, 2009, 11:10pm
 
Hi, Thanks for having me.

Playing around with the Processing "Hello World" (drawing a line), I eventually brought myself to a place where I'm drawing a line to a point on a sin wave (two lines actually, one from the top and one from the bottom). It's pretty and I'm really just trying to transition from actionscript.

...

All I'm doing is increasing a variable and taking the sin of it for the x position of the line. Again, something I used in actionscript all the time.

Now... what I haven't done in a long, long time is really play with this. A few years ago I would make shooter games and the enemy behavior was usually based off of a sin or cos. I could SWEAR that using tan() I could get a pattern like:

...

At least I remember making enemies move in that pattern. Well when I switched the function to tan(i) I got:

...

Which not only wasn't the wave I was expecting, it's not a wave at all. In fact it's not even a pattern!

So that surprised me. Now, is my memory completely faulty? Second, it's been 15 years since I had highschool math but... is that right!?

Code:


void move(){
float n = sin(i); //make a float to control the up and down
float p = .7; //make a float to control the left and right
i = i+.1; //increase i
y = y+n; //move y by the sin(i)
if(x<width){ //if x isn't off screen
x=x+p; //move x
}
else{//and if it is
x=0;//move it back to the other side
}
Re: Trigonometry question...maybe
Reply #1 - May 22nd, 2009, 8:58am
 
I suspect the problem is that you're adding tan(i) recursively to y (y = y+n).  Having double-checked (my memory of trig is also hazy - always have to look things up!) at certain points tan(i) approaches positive and negative infinity.  If you add that recursively to a variable you're bound to run into problems.

I looked up a wave drawing example from 'Actionscript Animation' (Friends of Ed - Flash book, which has a handy section on trig) and that uses a fixed 'centreY' variable and adds the result of sin/cos/tan(angle) to that.  I replaced this line with your recursive assignment and the tan(angle) wave went horribly wrong....  Here's the example converted to Processing:

Code:
float angle;
float centreY;
float x,y;
float xspeed,yspeed;
float range;

void setup() {
 size(400,300);
 x = 0;
 angle = 0;
 centreY = height/2;
 range = 50;
 xspeed = 2;
 yspeed = .1;

}

void draw() {
 x += xspeed;
 y = centreY + tan(angle) * range;
 point(x,y);
 angle += yspeed;  
}
Page Index Toggle Pages: 1