# Accurate collision tests among circles

**URL:** <https://forum.kirupa.com/t/accurate-collision-tests-among-circles/238226>\
**Category:** programming\
**Created:** [September 8, 2007, 4:07pm UTC](https://forum.kirupa.com/t/accurate-collision-tests-among-circles/238226 "2007-09-08T16:07:53Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![fw2803](https://avatars.discourse-cdn.com/v4/letter/f/e5b9ba/32.png) [@fw2803](https://forum.kirupa.com/u/fw2803)\
**Post date:** [September 8, 2007, 4:07pm UTC](https://forum.kirupa.com/t/accurate-collision-tests-among-circles/238226/1 "2007-09-08T16:07:53Z")

</div>

Aye! Hi there.  
I’ve been trying some collision tests among circles these days. And very unluckily, I encounter some problems.

I could create a function of collision test pretty easily with this method:

```auto

dx = this._x - _root.ball2._x
dy = this._y - _root.ball2._y
d = Math.sqrt((dx*dx)+(dy*dy))

```

dx is the difference between the x-coordinates of the two circles and… you know.

Here are the full codes:  
Note that I named the two circles as “ball1” and “ball2”.

```auto

onClipEvent (enterFrame) {
 var speed = 1
 var dx:Number
 var dy:Number
 var d:Number
}
onClipEvent (enterFrame) {
 if (Key.isDown (Key.RIGHT)) {
  this._x += speed
 }
 if (Key.isDown (Key.LEFT)) {
  this._x -= speed
 }
 if (Key.isDown (Key.UP)) {
  this._y -= speed
 }
 if (Key.isDown (Key.DOWN)) {
  this._y += speed
 }
 dx = this._x - _root.ball2._x
 dy = this._y - _root.ball2._y
 d = Math.sqrt((dx*dx)+(dy*dy))
 if (d <= 40) {
  speed = 0
 } 
}

```

So ball1 did not stop when it hit ball2. It went through.

But when I change “speed = 0” to "trace(“Hit!”), I got the results in the output window. So flash knows that there had been a collision. However, it does not stop ball1 from going through. What’s the matter?
