dcsimg
A Flash Developer Resource Site

Page 20 of 21 FirstFirst ... 10161718192021 LastLast
Results 381 to 400 of 411

Thread: Optimization Tips

  1. #381
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    So, the purpose of using radius this way
    Code:
    var radius1 = (clip1._width * clip1._width)/2
    var radius2 = (clip2._width * clip2._width)/2
    Instead of this
    Code:
    var radius1 = clip1._width/2
    var radius2 = clip2._width/2
    Is cos the first way you are taking the whole formula and making everyhting to the power of 2,
    So, its faster cos you have to do this
    Code:
    var radius1 = (clip1._width * clip1._width)/2
    var radius2 = (clip2._width * clip2._width)/2
    Only once, out of each hitTest, instead of having an extra step to calculate the sqrt inside each test,right?
    Clever, i wonder if this principle can be used in other things
    Last edited by Cimmerian; 12-01-2006 at 05:29 AM.

  2. #382
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    I may be very dumb, cos i cant understand how you go from this
    Code:
    var radius1 = clip1._width/2
    var radius2 = clip2._width/2
    To this
    Code:
    var radius1 = (clip1._width * clip1._width)/2
    var radius2 = (clip2._width * clip2._width)/2
    I mean, if its all to the power of 2, why its not
    (clip1._width * clip1._width)/ 4
    ?
    Or this power of 2 has nothing to do?

  3. #383
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140
    the radius of something is half its circumference

    so if a ball has a radius of 10cm its circumference is 20cm
    lather yourself up with soap - soap arcade

  4. #384
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    I know that, i dont understand the stuff necessary to drop the sqrt in the distance formula

  5. #385
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    I dont know if i made something wrong, but please look at this fla
    With big clips,in this way
    Code:
    var radius1 = (clip1._width * clip1._width)/2;
    var radius2 = (clip2._width * clip2._width)/2;
    var radius = radius1 + radius2;
    the test start to gives true without them to touch eah other,but this
    Code:
    var radius1 = clip1._width/2;
    var radius2 = clip2._width/2;
    var radius = radius1 + radius2;
    radius = radius*radius;
    dont
    Attached Files Attached Files
    Last edited by Cimmerian; 12-02-2006 at 05:28 AM. Reason: i want

  6. #386
    Senior Member Ray Beez's Avatar
    Join Date
    Jun 2000
    Posts
    2,796
    Quote Originally Posted by mr_malee
    the radius of something is half its circumference

    so if a ball has a radius of 10cm its circumference is 20cm
    WRONG. It's half the diameter.

  7. #387
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140
    we all make mistakes
    lather yourself up with soap - soap arcade

  8. #388
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,182
    What's a circumference?

  9. #389
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140
    the distance around a circle
    lather yourself up with soap - soap arcade

  10. #390
    SaphuA SaphuA's Avatar
    Join Date
    Oct 2002
    Location
    The Netherlands
    Posts
    2,182
    Ah, ok

  11. #391
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    Why dont you answer me?

  12. #392
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    Since noone bother in makes things clear, i am beginning to think there is no such thing as circle hitTest without sqrt, cos this way has a bug
    Code:
    var radius1 = (clip1._width * clip1._width)/2;
    var radius2 = (clip2._width * clip2._width)/2;
    var radius = radius1 + radius2;
    But this dont
    Code:
    var radius1 = clip1._width/2;
    var radius2 = clip2._width/2;
    var radius = radius1 + radius2;
    radius = radius*radius;
    BUT this way is difficult to use cos you cant calculate the summ of the two radius before any test, cos you dont know what are you going to test, unless you pre calculate all the possible combinations before any hitTest, but with many clips this may be very dificult, and even if more clips are added dynamically?

  13. #393
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140
    create a circleToCircle hitTest function which accepts two circles and find the radius like that.
    lather yourself up with soap - soap arcade

  14. #394
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Circle to circle collision without a square root is very easy.

    function checkCollision(circle1, circle2) {
    circle1Radius = circle1._width>>1;
    circle2Radius = circle2._width>>1;
    xdist = circle2._x-circle1._x;
    ydist = circle2._y-circle1._y;
    return xdist*xdist+ydist*ydist<Math.pow(circle1Radius+cir cle2Radius, 2);
    }
    http://www.birchlabs.co.uk/
    You know you want to.

  15. #395
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    But what is the advantage in drop the sqrt this way since you have to add the stuff to calculate the power of the radius of the circle?
    I mean, take out one thing and add another
    OR do you mean that calculate this power stuff is faster them to calculate some sqrt?

  16. #396
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Well, lemme put it this way:the radius of the balls will never change, so you can precalculate the square of their radii, but the x and y distances will be changing constantly.
    http://www.birchlabs.co.uk/
    You know you want to.

  17. #397
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140
    thats an interesting way to do circle to circle vengeance, good to know
    lather yourself up with soap - soap arcade

  18. #398
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    Quote Originally Posted by VENGEANCE MX
    Well, lemme put it this way:the radius of the balls will never change, so you can precalculate the square of their radii,
    Still didnt get it
    The way i see, even if you have the two pre calculated sqrt radius, the distance should be compared to the sqrt of the sum of the two radius, not to the sqrt of one radius plus the sqrt of the another
    I mean, sqrt(a + b) its not the same as sqrt(a) + sqrt(b),and so as (a+b)*(a+b) too
    Last edited by Cimmerian; 12-06-2006 at 04:51 AM.

  19. #399
    Senior Member
    Join Date
    May 2000
    Posts
    347


    FASTER HITTEST FOR DOING TONS OF HIT DETECTION FOR PLATFORMS + PHYSICS

    see www.spiritonin.com/radius - to see example in action

    When doing a platform based game where you are checking collision on lots of blocks, I found it is much faster to do the hitTest(x,y,true) on the movieclip containing all the blocks, than it is to do a mc.hitTest(mc) on every block.

    Problem is I have a bunch of physics stuff based on individual block detection.

    I tried a method that I think is faster:

    You do Two MC's in the same coordinates in the same timeline - "world"(contains all the blocks) and "worldEdges"(contains dynamically created blocks that are a certain amount larger than the "world" blocks, creating an edge around all of them visually You will see this in the game if you look at the example)

    The amount larger is based on the size of your sprites, where the size should be a few pixels larger than half the width of the widest sprite (so if you are doing 32x32 sprites, make the edges ~20 pixels wider in each direction) so if you have a 50wx100h block, it should spawn a 90wx140h block that is 20 pixels up and left of the collision block, but in the "worldEdge" mc

    Then when you do the hitest you check to see if the center point of the sprites is hitting "worldEdge" then if it is you check collsion on each platfrom in "world".

    If you have different sized sprites, you can create more edge clips ("worldEdgeBig", "WorldEdgeMedium" etc...)

    You can also do it for different types of platforms or platform groups to cut down on the number of MC's being tested when an edge is penetrated.

    Hope that makes sense... It was a bit messy to set up, but the collision seems to work a lot cheaper especially with lots of enemies bouncing around the screen.

    If anyone knows a faster way to check a complex set of block for detection, leg me know.
    Last edited by rubbersharkman; 01-12-2007 at 12:12 PM.

  20. #400
    Senior Member webgeek's Avatar
    Join Date
    Sep 2000
    Posts
    1,356
    It was also recently proven by several of us that for AS3 at least, circle-to-circle collision detection (without the sqrt call) is significantly faster then anything else. The results are outlined in this thread along with sample code:

    http://board.flashkit.com/board/showthread.php?t=710720

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