You need some ball physics which Crappycoder has made already. In fact most of the code is available.
Here it is.........
rem **************************************************************************
`
` ========================================================================
` | Snooker Ball Collision Code |
` ========================================================================
`
` ----------------------------------------------------------------
` | |
` | Written By: Simon Anthony Ferguson aka( Crappycoder ) |
` | Date: 4/3/2003 |
` | |
` | Comments: A basic collision example |
` | |
` | Liscence: Use as you like, but please give credit |
` | if you release a project that uses this |
` | code. Thank You |
` | |
` ---------------------------------------------------------------
`
` ========================================================================
` | Snooker Ball Collision Code |
` ========================================================================
`
rem ******************************************************************************
rem arrays
dim ball#(100,7)
rem fills arrays
`ball#(0,0) = 10
`ball#(0,0 ) = total balls
`ball#(num,1) = x
`ball#(num,2) = y
`ball#(num,3) = xspeed
`ball#(num,4) = yspeed
`ball#(num,5) = radius
`ball#(num,6) = mass
`ball#(num,7) = type
rem start up
sync on
sync rate 1000
hide mouse
rem creates triangle ball setup
ballTriangleSize = 5
ballsize# = 15
trianglex# = 200
triangley# = (screen height() / 2) - ((ballTriangleSize / 2) * ballsize#)
For col = 1 To ballTriangleSize
sx = ((ballTriangleSize / 2) * ballsize#) - ((col - 1) * ballsize#)
For ball = 1 To col
inc b
ball#(0,0) = b
ball#(b,1) = trianglex# - (col * (ballsize# * 1.7))
ball#(b,2) = triangley# + (sx + ((ball - 1) * (ballsize# * 2)))
ball#(b,3) = 0.0
ball#(b,4) = 0.0
ball#(b,5) = ballsize# - 0.5
ball#(b,6) = 15
ball#(b,7) = 2
Next ball
Next col
rem cue ball
inc b
ball#(0,0) = b
ball#(b,1) = 450
ball#(b,2) = triangley# + (2.0 * ballsize#)
ball#(b,3) = 0.0
ball#(b,4) = 0.0
ball#(b,5) = ballsize# - 0.5
ball#(b,6) = 15
ball#(b,7) = 1
rem start of loop
do
cls
rem handles balls
for b = 1 to ball#(0,0)
`applies x,y speed to ball position
ball#(b,1) = ball#(b,1) + ball#(b,3)
ball#(b,2) = ball#(b,2) + ball#(b,4)
ball#(b,3) = ball#(b,3) * 0.99
ball#(b,4) = ball#(b,4) * 0.99
`updates ball collision
update_collision
`screen collision
if ball#(b,1) > (640 - ball#(b,5) * 2)
ball#(b,1) = (640 - ball#(b,5) * 2)
ball#(b,3) = 0 - ball#(b,3)
endif
if ball#(b,2) > (480 - ball#(b,5) * 2)
ball#(b,2) = (480 - ball#(b,5) * 2)
ball#(b,4) = 0 - ball#(b,4)
endif
if ball#(b,1) < ball#(b,5) * 2
ball#(b,1) = ball#(b,5) * 2
ball#(b,3) = 0 - ball#(b,3)
endif
if ball#(b,2) < ball#(b,5) * 2
ball#(b,2) = ball#(b,5) * 2
ball#(b,4) = 0 - ball#(b,4)
endif
`colors balls
ink rgb(255,0,0),0
if ball#(b,7) = 1 then ink rgb(200,200,200),0
circle ball#(b,1),ball#(b,2),ball#(b,5)
`controls
if ball#(b,7) = 1
mx# = mousex()
my# = mousey()
if sqrt((ball#(b,3) ^ 2) + (ball#(b,4) ^ 2)) < 0.1
if distance(mx#,my#,ball#(b,1),ball#(b,2)) < ball#(b,5)
if mouseclick() = 1 and hold = 0
hold = 1
endif
if mouseclick() = 0 and hold = 1
hold = 0
endif
else
if hold = 1
if distance(mx#,my#,ball#(b,1),ball#(b,2)) > 150
ang# = wrapvalue(atanfull(my# - ball#(b,2),mx# - ball#(b,1)))
mx# = newxvalue(ball#(b,1),90 - ang#,150)
my# = newzvalue(ball#(b,2),90 - ang#,150)
endif
ink rgb(0,200,0),0
line ball#(b,1),ball#(b,2),mx#,my#
if mouseclick() = 0
hold = 0
ball#(b,3) = (ball#(b,1) - mx#) * 0.25
ball#(b,4) = (ball#(b,2) - my#) * 0.25
endif
endif
endif
endif
endif
next b
rem draws mouse
ink rgb(200,200,200),0
line mousex() - 2,mousey(),mousex() + 2,mousey()
line mousex(),mousey() - 2,mousex(),mousey() + 2
rem end of loop
sync
loop
rem updates collision
function update_collision()
For b = 1 to ball#(0,0)
for b2 = 1 to ball#(0,0)
if b2 <> b
collisionDistance# = ball#(b,5) + ball#(b2,5)
actualDistance# = distance(ball#(b,1),ball#(b,2),ball#(b2,1),ball#(b2,2))
if actualDistance# < collisionDistance#
collNormalAngle# = atanfull( ball#(b2,2) - ball#(b,2),ball#(b2,1) - ball#(b,1) )
moveDist1# = (collisionDistance# - actualDistance#) * (ball#(b2,6) / (ball#(b,6) + ball#(b2,6)))
moveDist2# = (collisionDistance# - actualDistance#) * (ball#(b,6) / (ball#(b,6) + ball#(b2,6)))
ball#(b,1) = ball#(b,1) + moveDist1# * Cos(collNormalAngle# + 180.0)
ball#(b,2) = ball#(b,2) + moveDist1# * Sin(collNormalAngle# + 180.0)
ball#(b2,1) = ball#(b2,1) + moveDist2# * Cos(collNormalAngle#)
ball#(b2,2) = ball#(b2,2) + moveDist2# * Sin(collNormalAngle#)
nX# = Cos(collNormalAngle#)
nY# = Sin(collNormalAngle#)
a1# = (ball#(b,3) * nX#) + (ball#(b,4) * nY#)
a2# = (ball#(b2,3) * nX#) + (ball#(b2,4) * nY#)
optimisedP# = (2.0 * (a1# - a2#)) / (ball#(b,6) + ball#(b2,6) )
ball#(b,3) = ball#(b,3) - (optimisedP# * ball#(b2,6) * nX#)
ball#(b,4) = ball#(b,4) - (optimisedP# * ball#(b2,6) * nY#)
ball#(b2,3) = ball#(b2,3) + (optimisedP# * ball#(b,6) * nX#)
ball#(b2,4) = ball#(b2,4) + (optimisedP# * ball#(b,6) * nY#)
endIf
endif
next b2
next b
endfunction
rem function for distance
function distance(x1#,y1#,x2#,y2#)
dx# = x2# - x1#
dy# = y2# - y1#
distance# = sqrt((dx# * dx#) + (dy# * dy#))
endfunction distance#
