A Flash Developer Resource Site

Results 1 to 20 of 23

Thread: semi-inaccurate sin & cos (250% increase in performance!)

Threaded View

  1. #1
    talk to the hand! Never_land:('s Avatar
    Join Date
    Nov 2007
    Location
    your mother's bedroom
    Posts
    490

    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)

    Actionscript Code:
    var reminder:Number=1;
    function fcos(alf:Number):Number {
        if (alf < 0) {
            alf = -alf;
        }
        if (alf > 180) {
            alf = 180-alf;
        }
        if (alf>45) {
            alf=.7-((alf-45)*.0222)*.7;
        } else {
            alf=.7+((45-alf)*.0222)*.3;
        }
        return alf;
    }
    function fsin(alf:Number):Number {
        reminder=1;
        if (alf<0) {
            reminder=-1;
        }
        if (alf > 90) {
            alf = 180-alf;
        } else if (alf < -90) {
            alf = 180+alf;
        }
        if (alf < 0) {
            alf = -alf;
        }
        if (alf>45) {
            alf=.7+((alf-45)*.0222)*.3;
        } else {
            alf=(alf*.0222)*.7;
        }
        return alf * reminder;
    }

    (Flash player10)

    Speed Tests
    average 40 tests
    Mat.sin()=1081 fsin()=419
    +257% more performance
    Mat.cos()=1129.54 fcos()=371.72
    +303% more performance

    Accuracy tests

    System averaged 100000 tests with random angles
    fcos() = -6.845% accuracy
    fsin() = -9.937% accuracy
    EDIT: updated cosine,updated accuracy tests
    EDIT2: updated speed tests
    Last edited by Never_land:(; 05-20-2010 at 04:07 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center