Author Topic: Simple math questions  (Read 607 times)

Simple math questions
« on: April 18, 2010, 09:19:07 pm »
For simple math questions not really deserving of a whole topic.





Just wondering about the equation of a circle. Right now, I've got my motion objects translating using:

Code: [Select]
t = elapsedTime / duration
And I want to find the position on a circle based on t. Right now I'm using:

Code: [Select]
angle = 2 * PI * t;
x = sin(angle) * radius;
y = cos(angle) * radius;

Which works fine and dandy and all, and I can even offset the start position easy by adding to angle, but is there a faster way to do this? I figure there's an accurate means of doing this which doesn't require me to call sin/cos every frame, but if there is I don't know it. I can understand math quite easily, I was just never taught it so all the math I know is random things I've looked up and taught myself via the internet and books.

Cheers.
« Last Edit: April 18, 2010, 10:01:12 pm by ChevyRay »

Re: Simple math questions
« Reply #1 on: April 19, 2010, 01:37:20 am »
Hmn! As far as  I can see, you will need to use trig functions here if you want a true circle, however you can probably come up with some kind of approximation which might be a bit faster (but could ultimately give some weirdness.)

Are the trig functions for sure slowing things down?

You could also use a lookup table and interpolate, which might be the most reasonable way to approximate it anyhow. For instance you have a table of 1000 entries and then just find the two that neighbour your current theta (scaled to a domain of 0...999 of course) then interpolate linearly between them. The effect should be the same as following a circle with 1000 segments, which I would think for most purposes would be nearly indistinguishable!

Let me know if you would like psuedocode or anything for this!

Re: Simple math questions
« Reply #2 on: April 19, 2010, 01:54:51 am »
Nah it's cool, I dig what you're saying. I thought there'd be an accurate way of working this out without using the trig, but I'm probably mistaken. The trig will doubtfully be a problem anyhow, it was mostly just a curiosity thing. I don't think I want to use a lookup table merely because this is for FlashPunk and I'd rather not make assumptions about what kind of values folks will be using for this, and just leave the accuracy to the trig.

Thanks!

Re: Simple math questions
« Reply #3 on: April 19, 2010, 09:04:56 am »
The other equation of a circle is x2 + y2 = r2, not including the offset of the centre of the circle (in which case it becomes (x-cx)2 + (y-cy)2 = r2). So maybe you could use this, but whether it would be faster and easier (what with sqrts in there) than using the (potentially already highly optimised) trig functions is debatable/investigatable.

(I once tried to create single float look-up tables for the sin/cos functions in System.Math in Microsoft's .NET Framework, but I couldn't get it to be more than 1-2% faster than the double math versions.)

Re: Simple math questions
« Reply #4 on: April 19, 2010, 09:14:15 am »
Ah, alright thanks Alex. I think that settles it for me, then. Cheers

Stephen L

  • Admin
    • View Profile
Re: Simple math questions
« Reply #5 on: April 19, 2010, 11:39:11 am »
Don't be afraid of trig stuff, Chevy :P

Re: Simple math questions
« Reply #6 on: April 19, 2010, 01:17:09 pm »
Be afraid of trig in Flash, the built in functions for it are some of the most expensive you can run.

Flash hates it when you try to be cool with math :P

Re: Simple math questions
« Reply #7 on: April 19, 2010, 02:33:29 pm »
I'd love to see the results of a benchmarking test similar to Alex's, comparing the built in trig functions with a lookup table!

Re: Simple math questions
« Reply #8 on: April 19, 2010, 04:11:40 pm »
Yes, sounds like a LUT would be good in the case of AS3 then. I await the results of your experiment in earnest! ;)

Re: Simple math questions
« Reply #9 on: April 19, 2010, 09:41:10 pm »
Don't be afraid of trig stuff, Chevy :P

Heheh alright :) yeah I did go with the trig. the thing is, it's all for FlashPunk, and the new version of the engine is pretty optimized all around the board, so I just like to have most things running as fast as possible in the background so that if people decide to use it for rather robust operations, they won't have to override my own weaker implementations with their own. I can't see that many people using the CircleMotion class all that extensively though, so it's not a worry.

Re: Simple math questions
« Reply #10 on: April 22, 2010, 10:28:23 am »
If you want to avoid the trig functions, you can use polynomial approximations (but I think this is just what the FPU does internally anyways):

http://en.wikipedia.org/wiki/Taylor_series

I once did that, mostly to find out how to compute sin(x) with "normal" math, but it's not that thrilling really ... somewhere on the web you should be able to find the polynomials without the need to derive them yourselves.

Edit:

After some googling it seems a rough approximation is:

sin(x) ~= x - 1/6*x3 + 1/120*x5

If my memory serves me right, that is only good for small values of x.

Edit 2:

sin(x) is symmetric and you can calculate all other values from the range of -PI/2 <= x < PI/2 IIRC, so the "small values of x" restriction may not be as bad as it sounds first. Within these bounds the approximation seems to be quite good.

I hope I got all that right now, must be like 15 years since I've actually learned about that. Refreshing the knowledge was good though :o)
« Last Edit: April 22, 2010, 01:16:28 pm by Hajo »

Re: Simple math questions
« Reply #11 on: June 08, 2010, 07:02:12 pm »
The time-honored tradition for fast trig functions is to have a table with the results for, say, every 0.01 degrees ready. A 36000-position array isn't that large on memory and is quite fast to access.

Re: Simple math questions
« Reply #12 on: August 25, 2010, 06:53:28 pm »
don't know what language we're talking about here but assuming something low level like, C, i don't think it's safe to assume a LUT will be faster than the trig functions or polynomial approximations. depending on the size of the table, and how many different angles you'll be looking up, you could very easily thrash the cache. if we're talking about anything higher level than C than it probably doesn't matter.