A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: collision detection - what's more efficient?

Hybrid View

  1. #1

    Talking

    Hey... say I were to make something like a breakout game where I'd need to detect when a ball or something hit a block. Would it be more efficient to script it so that every frame, the ball checks (action script in the ball object) if it's touching any blocks (does a loop for all of the blocks and checks how close it is to each), or to put action scripts in the blocks themselves to check if the ball touches them, then tell target the ball and make it change direction then remove themselves, or have all of the code in the "root" of the movie. Maybe another way I didn't think of? I'm not sure how much of a difference it will make depending on the way I do it. Any tips would be appreciated. Thanks.

  2. #2
    Senior Member Mad-Sci's Avatar
    Join Date
    Mar 2000
    Posts
    2,756
    hey, consider this:

    you have 200 bricks and one ball. it will be more efficient if each brick tests for itself if its hit, rather the ball which has to do a count to 200 to test all bricks.
    Make sence ?
    Mad_sci
    PS do one brick with a script and copy and paste after this.
    Mad_sci

  3. #3
    Senior Member
    Join Date
    Jul 2000
    Posts
    323
    I disagree. Have the ball check... Think about it... Either way you're doing 200 checks either way, but if you have 200 movie clips (The blocks) all running their own timelines, it'll slow down the game. Better to have just one moving clip (The ball), and the blocks stopped. Of course, that's just my two cents.

  4. #4
    Senior Member Mad-Sci's Avatar
    Join Date
    Mar 2000
    Posts
    2,756
    he he he Xentor add mine 2c as well. You know wnat Jitspoe do both things and see which one is going to work for you.

    Mad_sci
    PS Xentor do you think that it will speed up if
    the brick check for the distance before doing collision.

  5. #5
    Senior Member
    Join Date
    Jul 2000
    Posts
    323
    That's not the point... You can do the collision detection or distance check from either side... Block or ball... The point is that if you have the blocks checking collision, you'll have 200 movie clip timelines running at once... If, however, you tell them to Stop just after spawning them, they're static objects and don't take up much processor time.

    [Edit]

    Actually I think the most efficient way would be to use a grid background, kinda like what I tried to explain for your game... Say in this case you could position the blocks in an invisible grid, then check which grid squares the ball overlaps.

  6. #6
    Senior Member Mad-Sci's Avatar
    Join Date
    Mar 2000
    Posts
    2,756
    You know Xentor you just remind me this is acutaly an old collsision detection used in some tile games. If 2 sprites are in one and the same cell they collide. I remember there was a problem if a sprite is in between the cells. This is what you re talking about right ? Hehe he he. I have to take my memory pill. this was a good one as well.
    Mad_sci

  7. #7
    Senior Member
    Join Date
    Jul 2000
    Posts
    323
    Well, then to be safe, run collision detection for all adjacent cells... Still much more efficient than checking everything.

  8. #8
    Glad I could strike up a conversation Just so you know, I have a single block that gets cloned many times in the script, so that makes coding anything in the blocks pretty easy. I had some of the same thoughts with using the blocks to detect - having a whole bunch of them each cycling through frames and calculations might lead to slow down. I might mess around with the grid idea - check only the blocks that exist in the adjacent grid spaces. That way I can do a more detailed collision detection so that the ball acts spherical rather than cubical


    Oh, and another thing... is there a way to do absolute value? I've had problems when the ball hits two blocks at once... the direction is reversed and then reversed again, so it keeps on going straight through the blocks!

  9. #9
    Senior Member
    Join Date
    Jul 2000
    Posts
    323
    This is AS we're talking about... Here's your absolute value...

    If X < 0
    Set Variable: "X" = -X
    End If

    I can't wait for flash 5...

  10. #10
    Senior Member Ray Beez's Avatar
    Join Date
    Jun 2000
    Posts
    2,796

    So what's the final word on this?

    Reading this thread, I still don't know which method is faster...

    ??


  11. #11
    Senior Moderator
    Join Date
    Apr 2000
    Location
    Sheffield, UK
    Posts
    3,881
    The best way of all is to have the ball do a distance calculation to see which objects are the closest to itself lets say five objects and then do collision detection on those objects.That way less code,faster and more efficient

    Thats my views!!
    have a good one
    Regards FlashGuru

  12. #12
    Senior Member Mad-Sci's Avatar
    Join Date
    Mar 2000
    Posts
    2,756
    hey, I got an idea:
    Making the breack out game all the bricks will not move. so make them a MC and give each one a name like:
    MC_x_y where the X and y will be the _x and _y coord of each brick. Now you have to compare the ball's edge with those coord. simpy with calculation based on the balls actual -x and _y. Thus you dont have to script the brick MC only the ball. And you dont have to compare the ball coord with the bricks or calculate the distance.
    I hope this is not confusing.
    Mad_sci

  13. #13
    Senior Member
    Join Date
    Jul 2000
    Posts
    323
    Good idea... Check the _x and _y, and check for any blocks within N pixels (Looking at even multiples)... Avoids both a full grid and a board-wide loop...

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