Results 1 to 16 of 16

Thread: How do you code for Collision detection in a bounce balls program

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    48

    How do you code for Collision detection in a bounce balls program

    I want it so you can turn it one and off. i have 5 timers and 3 sub routines for this program(i got very bored in class) i don't want to have to go back in the code in all of them to make the change if possible. im using the circle cmd to draw the balls. if you want i can upload my project in a zip file.

    i know this is pretty confusing thanks for bearing with me.

  2. #2
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: How do you code for Collision detection in a bounce balls program

    Uploading it would be a good idea... collision detection isn't that difficult in two dimensions, but the five timers seems a little overkill... I'm programming Pong with only one...

  3. #3
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: How do you code for Collision detection in a bounce balls program

    This thread is about circle collision detection.. Do you have yourballs bouncing properly?

  4. #4
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: How do you code for Collision detection in a bounce balls program

    For circles (and balls in 3D) collision detection is simple.
    Just calculate the range between the two balls' center and test if that range is equal to or less than the sum of both balls' radius.
    If that test returns true, they are colliding.

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    48

    Re: How do you code for Collision detection in a bounce balls program

    here is the project

    also im still new to this i have been thinking how how i could do that but i cant fiqure it out. i dont want want to be babied through but a small portion of the code would be helpful.

    also i have 5 timers because i have different effects.
    Attached Files Attached Files

  6. #6
    Hyperactive Member
    Join Date
    Aug 2006
    Posts
    367

    Re: How do you code for Collision detection in a bounce balls program


  7. #7
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: How do you code for Collision detection in a bounce balls program

    first off, for simplicity of reading and using, I would have created a new Type for the balls, and dimmed that as an array. Example:
    VB Code:
    1. Type Balls
    2.    X As Integer
    3.    Y As Integer
    4.    Red As Integer
    5.    Green As Integer
    6.    Blue As Integer
    7.    XInc As Integer
    8.    YInc As Integer
    9. End Type
    10.  
    11. Dim myBalls(count) As Balls

    That will make keeping track of everything easier, and it would make the collision detection somewhat simpler to keep track of as well.

    Second, I'd put in a Me.Cls whenever the user changes a setting. That way it starts fresh every time and doesn't look messy.

    As for the detection itself, it's just as jeroen said. Check each ball against each other ball and if the center distance is less than the diameter, add the collision code. It's an easy loop:
    VB Code:
    1. For i = 1 To BallCount
    2.    For j = i to BallCount
    3.       'check i vs. j
    4.       'will check all combinations
    5.    Next j
    6. Next i

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2006
    Posts
    48

    Re: How do you code for Collision detection in a bounce balls program

    Quote Originally Posted by timeshifter
    first off, for simplicity of reading and using, I would have created a new Type for the balls, and dimmed that as an array. Example:
    VB Code:
    1. Type Balls
    2.    X As Integer
    3.    Y As Integer
    4.    Red As Integer
    5.    Green As Integer
    6.    Blue As Integer
    7.    XInc As Integer
    8.    YInc As Integer
    9. End Type
    10.  
    11. Dim myBalls(count) As Balls

    That will make keeping track of everything easier, and it would make the collision detection somewhat simpler to keep track of as well.
    Im in a class for this first i have no clue what type is. second this is how my teacher told us to write the code.



    Second, I'd put in a Me.Cls whenever the user changes a setting. That way it starts fresh every time and doesn't look messy.
    once again no clue what that is

    As for the detection itself, it's just as jeroen said. Check each ball against each other ball and if the center distance is less than the diameter, add the collision code. It's an easy loop:
    VB Code:
    1. For i = 1 To BallCount
    2.    For j = i to BallCount
    3.       'check i vs. j
    4.       'will check all combinations
    5.    Next j
    6. Next i
    thank you for the code.


    I am taking a class for VB and he is slowly introducing us to different things. I know my project isn't the best way to do this but im using what little i know
    right now. i heard at the end of the year the projects get pretty hard.

    anyway thank you for the code.



    That code didnt work i put it in both the form load and bouncyball timer and i changed ballcount to asize (what the teacher used in his example program so i used it) where do i put it?
    Last edited by Wargod18; Dec 2nd, 2006 at 04:32 PM.

  9. #9
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: How do you code for Collision detection in a bounce balls program

    Here lies the problem with so many timers. If you only had one timer, you'd put it there. With five timers, that's a lot of redundant code.. generally considered poor style.

    Me.Cls is just a command that clears everything off the form. Just mess around with it whenever the user changes something, and go from there. Trust me, it'll look a lot better.

  10. #10
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: How do you code for Collision detection in a bounce balls program

    This program incorporates some very basic collision detection with circles. I'd have to dive into some trig to get a more realistic output, but this is a start for you.
    Last edited by timeshifter; Mar 23rd, 2007 at 09:33 AM.

  11. #11
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: How do you code for Collision detection in a bounce balls program

    This detection is a bit better... still doesn't incorporate angles, but it does look better than the other one. Have a look and see how I did it. All the detection stuff is in the Timer event.
    Last edited by timeshifter; Mar 23rd, 2007 at 09:33 AM.

  12. #12

  13. #13
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: How do you code for Collision detection in a bounce balls program

    That is meant for polygons, not circles. It's a lot simpler with circles to detect a collision, but it's harder to react to the collision.

  14. #14

  15. #15
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: How do you code for Collision detection in a bounce balls program

    And to be a bit more specific, at this time JR's program simply detects the collision. It doesn't do anything about it. Mine may be sketchy, but at least it works. I just don't wanna take the time to work out all the math to create accurate collisions.

  16. #16

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