help with pool
Author 
Message 
thoughtful

Posted: Sat Oct 11, 2003 1:36 am Post subject: help with pool 


Hello,
I am trying to make a pool game for my isp. So far i saw Tony's tutorial. I wrote this code from it. It is supposed to have one ball moving at the start. When the first one hits a second ball, the firs tone stops and the second one starts moving. Now my balls dont move at all. If some one can help me with this, or even show me another way, to make pool, ill be grateful.
====Code Starts====
code:  var balls_x : array 1 .. 7 of int
var balls_y : array 1 .. 7 of int
var balls_xsp : array 1 .. 7 of int %ball speeds
var balls_ysp : array 1 .. 7 of int
var distance : real
balls_x(1):=0
balls_y(1):=0
balls_xsp(1):=5
balls_ysp(1):=5
for i:2..7
balls_x(i):=Rand.Int(10,(maxx10))
balls_y(i):=Rand.Int(10,(maxy10))
balls_xsp(i):=0
balls_ysp(i):=0
end for
loop
for i : 1 .. 7
%movement of balls
balls_x (i) += balls_xsp(i)
balls_y (i) += balls_ysp(i)
end for
for i : 1 .. 7
for k : 1 .. 7
if i not= k then
%calculating distance between
distance := ((balls_x (i)  balls_x (k)) * (balls_x (i)  balls_x (k)) + (balls_y (i)  balls_y (k)) * (balls_y (i)  balls_y (k))) ** 0.5
if distance < 10 %if collision
then
balls_xsp (i) := round ((balls_x (k) * balls_x (k) + balls_y (k) * balls_y (k)) ** 0.5 * ((balls_x (i)  balls_x (k)) / (balls_x (i)  balls_x (k) + balls_y (i)  balls_y (k))))
balls_ysp (i) := round ((balls_x (k) * balls_x (k) + balls_y (k) * balls_y (k)) ** 0.5 * ((balls_y (i)  balls_y (k)) / (balls_x (i)  balls_x (k) + balls_y (i)  balls_y (k))))
balls_xsp (k) := 0 %first ball instantly stops, though in fact it should hit back with
balls_ysp (k) := 0 %atleast some energy... but we live in a perfect world :wink:
end if
end if
end for
end for
for i : 1 .. 7 %for the walls
if balls_x (i) < 10 then
balls_xsp (i) = balls_xsp (i)
end if
if balls_y (i) < 10 then
balls_ysp (i) = balls_ysp (i)
end if
if balls_x (i) > (maxx  10) then
balls_xsp (i) = balls_xsp (i)
end if
if balls_y (i) > (maxy  10) then
balls_ysp (i) = balls_ysp (i)
end if
end for
for i : 1 .. 7
drawfilloval (balls_x (i), balls_y (i), 10, 10, i)
end for
end loop 
====Code Ends==== 





Sponsor Sponsor



Tony





Homer_simpson





thoughtful

Posted: Sat Oct 11, 2003 7:26 pm Post subject: WEll.. 


Thnx both of you, well i actually have made pool, in which the balls intercollide & one can shoot the white ball. I havent yet made it so they can fall in the pockets.
Anyways i wanted to know if ne one can tell me an equation to find the real angles,(i.e if the ball hits at a slanted angle the other ball's angle should be different than when it hits at a straight angle)
hope someone know how this can be figured out. I saw tony's tutorial on this, also got his message but couldn't quite get it to work from n e angle. Jus wondering is someone already knows how to do this, else ill have to put my empty head to work 





Tony

Posted: Sat Oct 11, 2003 7:39 pm Post subject: (No subject) 


well the way I figured... you compare change of X and change of Y values between two balls collided.
The balls vecolity is split up between X and Y components at same ratio of the difference of X and Y values. 
Tony's programming blog. DWITE  a programming contest. 





