semi-inaccurate sin & cos (250% increase in performance!)
ok just to mention I like making particles
but I dislike needing to use Math.sin, Math.cos for each particle,
Why do I need so much accuracy?
I came up with the idea of making a fast sine and cosine without worrying
that much about accuracy
but worrying about performance.
dont use it for your shooting game this aint THAT accurate,
but it looks GREAT when used with stuff like particles (snow, dust, bricks, etc)
please help with any ideas to improve accuracy without taking away performance;
help improving performance.
it requires angles from an MC
ex.-
Cosine= fcos(alpha_mc.rotation)
Regardless of testing, I don't see how 200% speed could make up for 17% and 22% inaccuracy. Now my game is just screwed up faster.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD WEI Score: 7.6
Expanded the brackets (brackets are pretty slow), changed all your if/elses to conditional operations, pre-calculated some of the addition and subtraction.
There's probably a faster, bitwise way to do that reminded line.
Also, have you considered that for small angles of x, sin x = x? So, if the angle is below about 30º (lower if you want more accuracy), you can just convert to radians (multiply by 0.017 or π/180) and there's your sine. Plus, you can use this to get the cosine of angles 60º≤x≤90º (return 0.017(90-angle); basically just throwing it into sine instead).
Which reminder line are you talking about, Veng? There's 2.
Last edited by ImprisonedPride; 05-21-2010 at 08:57 AM.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD WEI Score: 7.6
Right. Thought there was a chance that Flash or Flash Player would be smart enough to simplify it like I did. It's the kind of thing that any good compiler would do for you.
@ImprisonedPride:
reminder= alf<0 -1 : 1;
This line. Just thinking that there's probably a faster way to check if a number is positive, using the bitwise operators (check if the first bit == 0). Also, the result (true or false, ie 1 or 0) can be immediately used as part of the calculation, so it'd be something like:
reminder = alf%0<<1-1;
where (alf%0) represents the hypothetical bitwise function I am referring to (I'm sure something like that must exist, but maybe I'm thinking of a function, in which case we wouldn't get a speed boost). But you can see that this method would not need a condition, and would use entirely bitwise operations, which are really fast. So it might just be faster.
Btw, the <<1-1 part doubles the result (using another bitwise operation) and subtracts one. Which turns the (alf%0)'s true and false (1 and 0) into 1 and -1, respectively.
However, reminder= alf<0 -1 : 1; is already a pretty damned fast way of doing it.
NeverLand I just d/loaded your example, sorry, I meant more of a test bed, ie the code you're running yourself to check the timing / accuracy, rather than an example of how to use it.
That way any changes can be compared straight away so we have no false starts.
Because of this, after testing (and I know your solution was hypothetical),
Code:
reminder = alf%0<<1-1;
doesn't produce the same answer.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD WEI Score: 7.6
Yes, a lookup table is the way to go. Pre-calculate all the values when the game initialises, push them into an array, and just read the array values when you need them.
Because of this, after testing (and I know your solution was hypothetical),
Code:
reminder = alf%0<<1-1;
doesn't produce the same answer.
Ah yeah, that was a typo, but I don't believe that breaks my other line of code in any way.
If we (as I intended) take alf%0 to mean (alf<0 ? -1 : 1), then surely that line,
(alf<0 ? -1 : 1)<<1-1
is one that compiles fine? unless I've got my bitwise BODMAS wrong.
@erixon,
I believe that array lookups are slower than using the trig functions in AS3. Actually, I think this optimization happened sometime around Flash Player 5.
hey have you guys ever tried rotating the object and then using localToGLobal, with the x value being the speed, to determine the next position of the object?.
Graphics Attract, Motion Engages, Gameplay Addicts
XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro
That sounds like it would be really, really slow, surely? Also, I'm not sure that their boundaries get updated as soon as you run the _rotation++; line - I think all coordinate changes happen with the render, at the end of the frame's code.
Sorry for my pessimism... ordinarily I used to just check these things, but Flash CS4 takes so long to load... am currently just working things out based on gut feelings.
Also, that idea only works if you're trying to move a movie clip. I'd want fast trig for blitting sprites around.
there are a lot of conditions in the posted code which i think would be pretty slow.
i think it would be pretty quick actually, only a couple lines of code and the local/global functions are very very quick
it doesn't need to render either.
why not give it a go?
Graphics Attract, Motion Engages, Gameplay Addicts
XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro
@erixon,
I believe that array lookups are slower than using the trig functions in AS3. Actually, I think this optimization happened sometime around Flash Player 5.
Definitely not! Lookups will be ~400 TRILLION BILLION etc. times faster. Especially using a Vector of floats.