Be a Supporter!

hit testing circles sucks dick

  • 439 Views
  • 12 Replies
New Topic Respond to this Topic
mooseisloose
mooseisloose
  • Member since: Dec. 6, 2004
  • Offline.
Forum Stats
Member
Level 35
Game Developer
hit testing circles sucks dick 2011-06-16 10:22:18 Reply

I always used hitTests and they worked fine, but now in my new physics games I have a hitTestObject for two circles touching, which is obviously shit, as it shows them as touching when they aren't. What's another method? Something really really simple though, as they are just circles and I will not be using this for any other shapes.

Cheers guys :)

Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to hit testing circles sucks dick 2011-06-16 10:37:12 Reply

Don't use hit test. Duh.

Use extremely basic math to calculate the overlap. Which happens to be the easiest possible form of collision detection.


#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}

BBS Signature
Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to hit testing circles sucks dick 2011-06-16 10:39:28 Reply

I'll give you a hint:

radius1 + radius2 < distance between centers


#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}

BBS Signature
UnknownFury
UnknownFury
  • Member since: Aug. 10, 2005
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to hit testing circles sucks dick 2011-06-16 10:41:01 Reply

At 6/16/11 10:22 AM, mooseisloose wrote: I always used hitTests and they worked fine, but now in my new physics games I have a hitTestObject for two circles touching, which is obviously shit

You're making a physics game but don't even know this?

Calculate distance (squared) between centres of circles
Compare to sum of radii of circles (squared)

mooseisloose
mooseisloose
  • Member since: Dec. 6, 2004
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to hit testing circles sucks dick 2011-06-16 11:57:50 Reply

Redshift: thanks for sticking me along the right lines. This is what I've come up with, it works in my head but when I try it, flash traces it as NaN, I want it to just show the distance for now:

trace (Math.sqrt(elephant1.x - elephant2.x)*(elephant1.x - elephant2.x) + (elephant1.y - elephant2.y)*(elephant1.y - elephant2.y));

UnknownFury: yes, this will be my 3rd physics game (the other two are still waiting for official release from the sponsor), but I use box2d so don't usually have to worry about any of the maths behind it as it's already done.

mooseisloose
mooseisloose
  • Member since: Dec. 6, 2004
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to hit testing circles sucks dick 2011-06-16 12:02:08 Reply

Got it, nvm. Didn't have the sqrt-ing in brackets:

trace(Math.sqrt((elephant1.x - elephant2.x)*(elephant1.x - elephant2.x) + (elephant1.y - elephant2.y)*(elephant1.y - elephant2.y)));

xKiRiLLx
xKiRiLLx
  • Member since: Feb. 8, 2007
  • Offline.
Forum Stats
Member
Level 09
Game Developer
Response to hit testing circles sucks dick 2011-06-16 16:27:55 Reply

How would you detect the point where they collide using this method?

PSvils
PSvils
  • Member since: Feb. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Game Developer
Response to hit testing circles sucks dick 2011-06-16 16:53:12 Reply

Use Paul Bourke as a documentation for these kinds of things:

http://paulbourke.net/geometry/2circle/

SantoNinoDeCebu
SantoNinoDeCebu
  • Member since: Jul. 20, 2002
  • Offline.
Forum Stats
Supporter
Level 32
Programmer
Response to hit testing circles sucks dick 2011-06-16 17:17:05 Reply

At 6/16/11 04:27 PM, xKiRiLLx wrote: How would you detect the point where they collide using this method?

Well the normal to the collision plane will be the vector (v) between the 2 centres.

So pick a circle centre, and add on this direction vector with the radius as its length:

centre + v.normalised() * radius

steveconlan
steveconlan
  • Member since: Mar. 31, 2008
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to hit testing circles sucks dick 2011-06-16 18:04:46 Reply

http://coreyoneil.com/

Try out the Collision Detection Kit, works wonders.

PSvils
PSvils
  • Member since: Feb. 3, 2010
  • Offline.
Forum Stats
Member
Level 01
Game Developer
Response to hit testing circles sucks dick 2011-06-16 19:14:45 Reply

At 6/16/11 05:17 PM, SantoNinoDeCebu wrote:
At 6/16/11 04:27 PM, xKiRiLLx wrote: How would you detect the point where they collide using this method?
Well the normal to the collision plane will be the vector (v) between the 2 centres.

So pick a circle centre, and add on this direction vector with the radius as its length:

centre + v.normalised() * radius

No, because the 2 circles might be intersecting, in which case the point becomes harder to find, as there are 2 intersection points, and you should find their average as the collision point.

Also, do NOT use the Collision Detection Kit. It relies on BitmapData methods which are quite slow, and a lot slower than some simple math equations.

SantoNinoDeCebu
SantoNinoDeCebu
  • Member since: Jul. 20, 2002
  • Offline.
Forum Stats
Supporter
Level 32
Programmer
Response to hit testing circles sucks dick 2011-06-16 19:59:55 Reply

At 6/16/11 07:14 PM, PSvils wrote:
At 6/16/11 05:17 PM, SantoNinoDeCebu wrote:
At 6/16/11 04:27 PM, xKiRiLLx wrote: How would you detect the point where they collide using this method?
Well the normal to the collision plane will be the vector (v) between the 2 centres.

So pick a circle centre, and add on this direction vector with the radius as its length:

centre + v.normalised() * radius
No, because the 2 circles might be intersecting, in which case the point becomes harder to find, as there are 2 intersection points, and you should find their average as the collision point.

Also, do NOT use the Collision Detection Kit. It relies on BitmapData methods which are quite slow, and a lot slower than some simple math equations.

I missed the intersection bit for simplicity...
And you probably wanna correct the intersection instead of just averaging or else your circles might get stuck within each other if they don't get repelled far enough.

Correcting intersection between 2 circles is still very simple (of course you're better off preventing it in the first place but that's another matter).

The penetration distance will be, if you think about it: the shortest distance between the 2 centres minus the actual distance between the 2 centres, ideally this would come to 0!

distance = radius1 + radius2 - v.magnitude(). Where the v is the vector between the 2 centres.

Lets say 1 circle was static, so you would want to translate the other one along vector v by this distance, and hey presto you got your single collision point on the surface of both circles.

mooseisloose
mooseisloose
  • Member since: Dec. 6, 2004
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to hit testing circles sucks dick 2011-06-21 17:24:02 Reply

The method I posted shows the distance, I just run an "if" statement so if that sum < (ball1.width + ball2.width)/2 then theyre touching. i know you guys will probably say how its wrong in some way but it works perfectly for me =D

thanks a lot for all the help guys, it worked perfectly although my flash file went corrupt so now ive gotta start the whole game from scratch :(