PDA

Click to See Complete Forum and Search --> : collision detection for a circle???


dflw
Jul 31st, 2001, 11:18 AM
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!

Brykovian
Jul 31st, 2001, 11:23 AM
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

kedaman
Jul 31st, 2001, 01:25 PM
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

dflw
Aug 1st, 2001, 06:50 AM
i still don't understand how to find the distance between the two center points of 2 circles?

Brykovian
Aug 1st, 2001, 07:01 AM
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

dflw
Aug 1st, 2001, 07:49 AM
ok , i get that, but how do i constantly change the x, y coords for the second circle because it moves?

Brykovian
Aug 1st, 2001, 08:29 AM
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 ...":D

-Bryk

dflw
Aug 1st, 2001, 02:05 PM
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.

kedaman
Aug 1st, 2001, 02:13 PM
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

Cruncher
Sep 7th, 2009, 10:10 PM
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

Jenner
Sep 8th, 2009, 08:58 AM
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.

opus
Sep 9th, 2009, 05:44 AM
You are aware that dflw has a "Last Activity: Mar 31st, 2002 05:52 PM".

Jenner
Sep 9th, 2009, 07:42 AM
Arrgg... I hate it when someone necro's a thread. :mad:

Cruncher
Sep 9th, 2009, 08:19 PM
Arrgg... I hate it when someone necro's a thread. :mad:

sorry? lol