I found this in my files maybe this can help you:
SuperStrict
'------------------------------------------
'2D COLLISION DEMO CODE
'------------------------------------------
' By Joseph 'Phish' Humfrey
' This Type of collision response isn't
' just useful For pool And billiard style
' games, And in fact, I didn't write it for
' that reason at all. I wrote it because I
' needed To have collision For the space
' ships in my game 'Unity'. When their
' shields are up, they use this relatively
' simple Method. It can be used For almost
' anything - player character bouncing off
' enemies in a platform game, To space
' ship collision, To a game which does
' actually involve balls. The basic
' algorithm which I used would work For
' both 2d And 3d, so If you would like To
' see it, email me at phish@...
'------------------------------------------
' The actual code which works out what
' happens after a collision is in the
' UpdateCirclePhysics() Function.
'------------------------------------------
' Enjoy!
'------------------------------------------
'------------------------------------------
Graphics 800, 600
SeedRnd MilliSecs()
'------------------------------------------
' MAIN DATA Type
'------------------------------------------
' This exact Type isn't supposed to be used
' Instead, you should use some of the fields
' in your own Type, Or just use this one
' For reference, To see what each Field does
Type circle
Field x#, y#'position
Field dx#, dy#'x And y speeds
Field radius#'radius of circle
Field mass#'mass of circle
End Type
'------------------------------------------
'------------------------------------------
Global list:TList = New TList
'------------------------------------------
' SET UP BALLS INTO A POOL STYLE ARRANGEMENT
' For DEMO
'------------------------------------------
Function setup()
Local ballTriangleSize%=5
For Local xloop:Int = ballTriangleSize To 1 Step -1
For Local yloop:Int = 1 To xloop
Local c:circle = New circle
c.x = (5-xloop)*27 + 200
c.y = yloop*31-(xloop*31)/2.0 + 300
c.dx=0
c.dy=0
c.radius = 15
c.mass = 50
list.addlast(c)
Next
Next
'Cue ball (smaller so you know which it is :)
Local cue:circle = New circle
cue.x = 800
cue.y = 300 +20
cue.dx = -20
cue.dy = Rnd(4)-2
cue.radius = 14
cue.mass = 50
list.addlast(cue)
End Function
'------------------------------------------
'------------------------------------------
'------------------------------------------
'MAIN LOOP
'------------------------------------------
' This is the main While..Wend game loop
setup()
While Not KeyDown(key_ESCAPE)
Cls
UpdateCirclePhysics()
RenderCircles()
'------------
' Reset button
DrawText "Press a mouse button to reset.", 10, 10
DrawText "Press Esc to exit.", 10, 25
If MouseDown(1) Then
For Local c:circle = EachIn list
list.remove(c)
Next
setup()
End If
'------------
Flip
Wend
'------------------------------------------
'------------------------------------------
End
'------------------------------------------
Function UpdateCirclePhysics()
'------------------------------------------
' This is the main physics Function For the
' circles. It contains the very basic
' movement physics as well as the collision
' response code.
'------------------------------------------
For Local c:circle = EachIn list
'update positions
c.x=c.x+c.dx
c.y=c.y+c.dy
'gradually slow down
c.dx=c.dx*0.991 '0 stop. 1 constant. >1 increase
c.dy=c.dy*0.991
'------------------------------------------
'COLLISION CHECKING
'------------------------------------------
' Check each circle in the loop against
' every other (c against c2)
For Local c2:circle = EachIn list
Local collisionDistance# = c.radius+c2.radius
Local actualDistance# = Sqr((c2.x-c.x)^2+(c2.y-c.y)^2)
'Collided Or Not?
If actualDistance<collisionDistance Then
Local collNormalAngle#=ATan2(c2.y-c.y, c2.x-c.x)
'Position exactly touching, no intersection
Local moveDist1#=(collisionDistance-actualDistance)*(c2.mass/Float((c.mass+c2.mass)))
Local moveDist2#=(collisionDistance-actualDistance)*(c.mass/Float((c.mass+c2.mass)))
c.x=c.x + moveDist1*Cos(collNormalAngle+180)
c.y=c.y + moveDist1*Sin(collNormalAngle+180)
c2.x=c2.x + moveDist2*Cos(collNormalAngle)
c2.y=c2.y + moveDist2*Sin(collNormalAngle)
'------------------------------------------
'COLLISION RESPONSE
'------------------------------------------
'n = vector connecting the centers of the circles.
'we are finding the components of the normalised vector n
Local nX#=Cos(collNormalAngle)
Local nY#=Sin(collNormalAngle)
'now find the length of the components of each movement vectors
'along n, by using dot product.
Local a1# = c.dx*nX + c.dy*nY
Local a2# = c2.dx*nX + c2.dy*nY
'optimisedP = 2(a1 - a2)
' ----------
' m1 + m2
Local optimisedP# = (2.0 * (a1-a2)) / (c.mass + c2.mass)
'now find out the resultant vectors
'r1 = c1.v - optimisedP * mass2 * n
c.dx = c.dx - (optimisedP*c2.mass*nX)
c.dy = c.dy - (optimisedP*c2.mass*nY)
'r2 = c2.v - optimisedP * mass1 * n
c2.dx = c2.dx + (optimisedP*c.mass*nX)
c2.dy = c2.dy + (optimisedP*c.mass*nY)
End If
Next
'------------------------------------------
'------------------------------------------
'Simple Bouncing off walls.
If c.x<c.radius Then
c.x=c.radius
c.dx=c.dx*-0.9
End If
If c.x>GraphicsWidth()-c.radius Then
c.x=GraphicsWidth()-c.radius
c.dx=c.dx*-0.9
End If
If c.y<c.radius Then
c.y=c.radius
c.dy=c.dy*-0.9
End If
If c.y>GraphicsHeight()-c.radius Then
c.y=GraphicsHeight()-c.radius
c.dy=c.dy*-0.9
End If
Next
End Function
'------------------------------------------
Function RenderCircles()
'------------------------------------------
' Simple Function draws all the circles
' on the screen.
'------------------------------------------
For Local c:circle = EachIn list
If c.radius=15 Then SetColor 200, 50, 50 Else SetColor 255, 255, 255
DrawOval c.x-c.radius, c.y-c.radius, c.radius*2, c.radius*2
Next
End Function
'------------------------------------------
'------------------------------------------