Planetside Software Forums

General => Terragen Discussion => Topic started by: mogn on November 21, 2007, 10:55:30 AM

Title: Have do I implement something like the function Atan2(x,y)
Post by: mogn on November 21, 2007, 10:55:30 AM
Atan2(x,y) is a function which calculate an angle in a continous range from -PI to +PI.

If you makes a 'get position in geometry' and divides this vector with the length, you
get a vector cos(v),sin(v).
What I want is to find v from these two scalars in a continous range.

If any of you know 'apophysis', it contains a transform 'NGON', which transforms a circle to a
regular polygon. This is what I want to implement in tg2.
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: David Burnett on November 21, 2007, 02:33:19 PM
Of then top of my head atan2(x, y) is atan(y/x)  between -PI and +PI.
atan is arctan in the trig functions

Things that might cause issues / stuff I've forgotten....

I'm not sure how TG2 deals with divide by zero's.
It the trig functions take radians or degree
keeping x/y between -PI and +PI (-180, 180), probably mod PI

Actually here's some pascal code, if you're not a programmer read it out aloud, you'll get the idea.



Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: Harvey Birdman on November 21, 2007, 03:40:23 PM
If you're just trying to get some kind of continous scalar relative to an angle, why not calculate the dot product? A dot product, calculated on two unit vectors lying in a plane, will return the cosine of the angle betweenthe vectors; that is, it will yield a result between -1 and +1, where +1 means the two vectors are ponted in the same direction (angle = 0 degrees), and -1 means they're pointed in the opposite direction (angle = 180 degrees).

The key is making sure you have two unit vectors (magnitude 1.0).

My apologies if I'm telling you someting you already know.

<edit>
The dot product has the added benefit of being strictly multplication and addition - no expensive trig operations.
</edit>
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: mogn on November 22, 2007, 01:52:41 AM
Thanks for your replies. The use of dot function, does not solve my problem. By the way the dot function has othe uses than
calculate the cos of the angle between two normalized vectors.
Dot([x,y,z],[u,v,w]) is calculated as [x*u+y*v+z*w].

Thanks Dave I will try atan(z/x).

The function I'm going to build is:

u = PI/N
w = mod(v,2u)
f = length*cos(u)/cos(w-u)

EDIT: The last line should read length*cos(w-u)/cos(u)
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: Matt on November 22, 2007, 10:12:48 AM
This can be calculated by normalizing the position vector, using an Arccos on the X component to retrieve an angle, and then doing a bit of sign flippage and/or addition depending on the sign of the Y component. You can also do something based on Arctan but for some reason I chose to do it differently here. To make it more accurate in all situations you might need to do it differently depending on which value is larger, X or Y, but I haven't done that here.

I've attached a clip file with 3 function groups:

GetAngle: I think this is similar to the Atan2 function you wanted, and returns values between -Pi and +Pi

GetHeading: Same as GetAngle except that the angle is clockwise and starts at 0 along the Z axis. Returns values between -Pi and +Pi.

GetHeadingPositive: Same as GetHeading except that it returns values between 0 and 2 * Pi.

In the next update there will be a built-in function to convert from radians to degrees, but if you want to do that now you should multiply by 180 and divide by Pi (there is a Constant PI function in the clip file).

Matt
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: mogn on November 22, 2007, 11:00:12 AM
Thanks Matt  ;D
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: mr-miley on November 23, 2007, 05:59:10 AM
To quote Mr Gumby (from Monty Python)....

"Brain hurts, BRAIN HURTS...."

;D
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: rcallicotte on November 23, 2007, 08:56:15 AM
Thanks, Matt!
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: mogn on November 25, 2007, 07:42:58 AM
Although Matt's 0..2PI positive function works, I personally do not like conditionals in Programming.
So my solution is:

Cos_v = Get_x(Normalized vector)
v = Arccos(Cos_v)
v_minus_PI = v - PI
Sin_v = Get_z(Normalized vector)
Sign = floor(Sin_v)
Result = v + Sign*(v_minus_PI)

The result is rotated 90 degrees compared to Matts result.

The line 'Sign = floor(Sin_v)' should read: Sign = 2*floor(Sin_v)
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: mogn on November 26, 2007, 01:12:40 PM
A complete waste of time, but I have demonstrated that it is is possible to create regular polygons in tg2!
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: rcallicotte on November 26, 2007, 03:22:24 PM
Not a waste.  Something will probably come out of this.  Or a bunch of somethings.   ;D
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: old_blaggard on November 26, 2007, 04:33:04 PM
Horray!  Now make a tetradecagon :P.
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: mogn on November 27, 2007, 12:57:50 AM
I See what you mean. I lost an internal connection! :-[
So I hope I got it correct this time.
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: Tangled-Universe on November 27, 2007, 12:55:49 PM
That looks very nice  ;D
Something I'll probably never be able to, math is too much magic to me.

To get back to your primary question:

Where do you need this kind of function for? (function Atan2(x,y) )

Martin
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: j meyer on November 27, 2007, 03:47:08 PM
That's a nice polygon.Would you mind sharing the secret of
the internal connection(s)? And if you share: in simple terms
or with a visible example,please.
Title: Re: Have do I implement something like the function Atan2(x,y)
Post by: mogn on November 28, 2007, 03:33:22 AM
Quote from: Tangled-Universe on November 27, 2007, 12:55:49 PM
That looks very nice  ;D
Something I'll probably never be able to, math is too much magic to me.

To get back to your primary question:

Where do you need this kind of function for? (function Atan2(x,y) )

Martin

You can use atan2 in cases where you want control over what happens at a distance from a point
in the landscape, and also depending on the directions.

In this case, I calculates the distance from the center of a unit circle to all points on the the straights lines
which connects the six points of the hexagon.
The inner and the outer distance from the point [2500m, 0m, 2500m] is then multiplied, by the about function.
Then these distances are use to clamp the length function.

Now we have have a function which takes on the value 'inner distance' between 0 and 'inner distance'
and 'outer distance' outside 'outer distance'.

I then convert these values to a range between 0 and 1. This gives an ascending ramp.

Finally I convert this ramp (q) to a bell shaped curve by appying the function
[4q*(1-q)]^2 , which has the values 0 and slope 0 at the endpoints, and the value 1 at the midpoint.

This value is then connected to a displacement shader.