Results 1 to 14 of 14

Thread: collision detection for a circle???

  1. #1

    Thread Starter
    Hyperactive Member dflw's Avatar
    Join Date
    Apr 2001
    Location
    ct, usa
    Posts
    412

    collision detection for a circle???

    I am new to collision detection with circles. I pretty much know how to do collision detection with rectangles, and i am going to start with circles. to get collision detection for 2 circles, i "think" i would find out the center point of the two circles and then find out the distance between the 2 center points of the 2 circles. Then i would subtract the sum of each circle's radius. If the remaining number is >= 0 then a collision has taken place. If not, a collision hasnt taken place.

    So here's my questions:

    1. how do i find out the center point of a circle?

    2. how do i find out the distance between two center points of 2 circles?

    3. how do i find the radius of a circle?


    Thanks for any help!
    Visual Basic 6, HTML, JavaScript, learning C++

  2. #2
    Member
    Join Date
    Jul 2001
    Location
    Minneapolis, MN USA
    Posts
    32
    The centerpoints and radii of your circles will need to be known ahead of time. And, in a game, they should be since they'd either be part of the level, or sprites that are moved.

    So, then, knowing those, you can get the distance between by using our favorite equation from Pythagoras:

    d = sqr( (X2-X1) ^2 + (Y2-Y1) ^2 )


    So, if your circles have radii R1 and R2 respectively, then if your calculated d <= R1 + R2, you have a collision ...

    Here's a couple of tutorials:
    rookscape.com/vbgaming/tutAP.php
    rookscape.com/vbgaming/tutBU.php

    -Bryk

    Edit: Bryk -- added tutorial references

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    In case your circles are not defined by center and radii, we'd need know what you're intending to do, that is what is the problem.
    Squareroot is computionally a lot slower than multiplication so to speed it up the detection expression should be:

    dx=ax-bx
    dy=ay-by
    r=ar+br
    collision=r*r>dx*dx+dy*dy
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4

    Thread Starter
    Hyperactive Member dflw's Avatar
    Join Date
    Apr 2001
    Location
    ct, usa
    Posts
    412
    i still don't understand how to find the distance between the two center points of 2 circles?
    Visual Basic 6, HTML, JavaScript, learning C++

  5. #5
    Member
    Join Date
    Jul 2001
    Location
    Minneapolis, MN USA
    Posts
    32
    dflw ... In my original reply, I gave the function:

    d = sqr( (X2-X1) ^2 + (Y2-Y1) ^2 )

    In it, one circle has a centerpoint at (X1, Y1) and the other at (X2, Y2) ... so if you run that line, then d will give you the distance between the circles' centerpoints.

    And Kedaman's right that multiplication is much faster in VB than any power-related operators (sqr and "^"), or just about any other math operator.

    So, his code showed that if you are just using the calc to determine collision, you don't need to do the sqr function on it -- you can just square the sum of the radii ...

    Does that help?

    -Bryk

  6. #6

    Thread Starter
    Hyperactive Member dflw's Avatar
    Join Date
    Apr 2001
    Location
    ct, usa
    Posts
    412
    ok , i get that, but how do i constantly change the x, y coords for the second circle because it moves?
    Visual Basic 6, HTML, JavaScript, learning C++

  7. #7
    Member
    Join Date
    Jul 2001
    Location
    Minneapolis, MN USA
    Posts
    32
    Can you give a little more detailed description of what you're program is doing?

    I've been assuming that your circles are things that your program is in control of ... for example, a space ship or player character that the user will be moving around with the mouse, keyboard, or joystick ...

    "More info please ..."

    -Bryk

  8. #8

    Thread Starter
    Hyperactive Member dflw's Avatar
    Join Date
    Apr 2001
    Location
    ct, usa
    Posts
    412
    I am not really making a game yet, i 'm just figuring out how to do the basics of games like collision detection. Ok, heres my setup. I have two circles on a form, one of them is static and the other one is moved with the arrow keys. I am assuming that the x,y coords of the center points of the moving circle are constantly changing... so to put the center point x,y coords into the equation, how would i do this?Yeah i sound like a moron but i'm just starting off in games. is this enough info?
    thanks for any help.
    Visual Basic 6, HTML, JavaScript, learning C++

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Those should be declared as variables, and the circles should be drawn using the variables. In case you use shape objects, the circle center is centered on the shape, so positioning them would be at center-radius
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10
    Addicted Member
    Join Date
    Feb 2009
    Posts
    160

    Re: collision detection for a circle???

    this is really old thread, found it from google, lol, but couldn't help replying,
    radius is circle.width / 2
    the center of a circle is (circle.left + radius, circle.top + radius)

    lol 2001, i wont get a reply back, but in case anyone else finds this thread

  11. #11
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: collision detection for a circle???

    Quote Originally Posted by dflw View Post
    I am not really making a game yet, i 'm just figuring out how to do the basics of games like collision detection.
    Ok, this is your problem in a nutshell actually. You don't understand exactly what a game is (at least an arcade game)

    A game is a program that when you start it, it runs in a loop. It zips through that loop at least 30 times a second. Every time it does, it draws the graphics on the screen. Why 30? Because that's about the minimum frames per second that the human eye is unable to perceive flickering (most game designers aim for 60 or better)

    If you're used to application programming, this type of graphics control was never an issue. Things didn't "move", they just "sat". Windows only updated the graphics when something happened and handled it behind the scenes.

    In a Game, the positional information of each object on the screen are CONSTANTLY being updated. Even if a circle on the screen hasn't moved for 10 seconds, the game has updated itself at least 300 times! And every time, it's checking for collision between that circle and another.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  12. #12
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: collision detection for a circle???

    You are aware that dflw has a "Last Activity: Mar 31st, 2002 05:52 PM".
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  13. #13
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: collision detection for a circle???

    Arrgg... I hate it when someone necro's a thread.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  14. #14
    Addicted Member
    Join Date
    Feb 2009
    Posts
    160

    Re: collision detection for a circle???

    Quote Originally Posted by Jenner View Post
    Arrgg... I hate it when someone necro's a thread.
    sorry? lol

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