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 › maths question
Page Index Toggle Pages: 1
maths question (Read 403 times)
maths question
Sep 18th, 2006, 11:10pm
 
Hi there again!

does anybody know how to calculate the rotation of an object (helicopter) that follows in a certain distance another object (fixed on the mouse) in the way that the nose is always in front (the flight direction)....
I tried something with atan of the distances between both positions but it doesnt exactly the right thing...

I hope finding some mathfreaks here in the artificial live division... ;)

regards, susanne

p.s.:

this is doin something similar - but not really what it should do - it's flash actionscript, but the calculation is the same:

     a = (_parent._xmouse-this._x);
     b = (_parent._ymouse-this._y);
     this._x = _parent._xmouse-0.9*a;  //---------movement
     this._y = _parent._ymouse-0.9*b;  //---------movement
     winkel = Math.atan (b/a);
     this._rotation = winkel*180/Math.PI;
Re: maths question
Reply #1 - Sep 19th, 2006, 12:16am
 
You are doing this in two dimensions, yes?  

It's a little vector math, you can calculate the difference between the two points:

Code:

float dx = x1 - x2;
float dy = y1 - y2;


Then use the atan2 function to return the angle of the vector (dx,dy).

Code:

float angle = (float) Math.atan2(y, x);


Depending on how you are doing your calculation / rotation, you might need to add PI/2 or reverse the polarity of the angle.

Here's more explanation:

http://www.shiffman.net/teaching/the-nature-of-code/vectors/

An example:

This will help too:

http://www.shiffman.net/teaching/the-nature-of-code/steering/

Dan
Re: maths question
Reply #2 - Sep 19th, 2006, 12:25am
 
Thanks - two dimensions yes. Hmmm... this is more or less the same that I did - but maybe it's a problem because of +/- as the center(null-point) of the background is not the center of the screen. In Processing I know how to fix that (with pushMatrix) - but in Flash.... I'll keep on working on that and check your teaching-stuff..  greatings from Linz, Susanne
Re: maths question
Reply #3 - Sep 19th, 2006, 1:20am
 
Hi Dan

I just made it - too easy always if you got it...
To avoid the flipping of the object I fixed two angles when the rotation is 90 and/or 270 degrees.

but your workshops do look very interesting!

thanks again, Susanne

(hope no-one will block me because of posting actionscript-code ....but I can transfer it into processing code if somebody wants to ;) )

___________________________________________

Code:

a = this._x - that._x;
b = this._y - that._y;
bogen = Math.atan(b/a);
this._rotation = bogen*180/Math.PI;

if (this._x ==  that._x && this._y <= that._y) {
this._rotation = 270;
}
if (this._x == that._x && this._y >= that._y) {
this._rotation = 90;
}

if (this._x >= that._x) {
this._rotation += 180;
}
Page Index Toggle Pages: 1