
-----------------------------------
Tony
Wed Nov 27, 2002 1:11 am

[trig] so you're making a pool game?
-----------------------------------
NOTE: thoughful has made a much better tutorial (well, mostly example) of a pool game found Trigionometry and Physics are very important for games... expesially if there's some movement involved. In this tutorail I'll be talking about Physics on a pool table.

How To Make Realistic Pool Game

well Pool uses round balls so we need a oval collision detection to start us off... You may want to refer to tutorail about that... here's the code to remind us:

distance = ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))^0.5 
if distance < radius1 + radius2 then
%We have a collision
end if

lets also set up some variables:
var x1, x2, y1, y2:int
var sp1x,sp1y, sp2x,sp2y:int
var distance:real

x1,y1 are position of first ball and x2,y2 are position of second. They are used for drawing the images and calculating distances.

sp1x, sp1y are movement of the ball along X and Y axis. Same for sp2's.

Now for some declarations:
x1:=100
x2:=500
y1:=100
y2:=105

sp1x:=5
sp1y:=0
sp2x:=0
sp2y:=0
here you actually put your start set up of the table. This is just for an example, with 2 balls, 1 set to hit the other.

and lets add the actual loop now...
loop
%movement of balls
x1 += sp1x
x2 += sp2x
y1 += sp1y
y2 += sp2y
%calculating distance between
distance := ((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))** 0.5

if distance < 10 %if collision
then 
    sp2x:=sp1x %ball 2 gets 100% transfer of enery from ball 1
    sp2y:=sp1y
    sp1x:=0 %first ball instantly stops, though in fact it should hit back with
    sp1y:=0 %atleast some energy... but we live in a perfect world  :wink: 
end if 

drawoval(x1,y1,5,5,red) %draw balls
drawoval(x2,y2,5,5,blue)
delay(100)
end loop

Put everything together and we'll have a little animation... But it doesn't look right... why? Because the second ball should have went @ 45degree angle, shouldn't it have? Yeah... we have a problem...

I'll continue this tutorial later on... I'm gonna go sleep now.

-----------------------------------
Tony
Fri Nov 29, 2002 9:49 pm


-----------------------------------
Making a pool game - Part 2 

Here's a little diagram for us:


              /```\
              \_`_/
        /```\
        \_`_/

Two balls colliding... If we draw the line connecting two centers, we'll get the direction of path for 2nd ball. Magnitude will be the speed of moving ball (@ perfect energy transfer). The X and Y components of this vector will make up relative velocity at which 2nd ball should start moving... Here's another diagram:


          .   