PDA

View Full Version : Pool Game


davidghemer
11-30-2011, 09:53 PM
I decided for some crazy reason to have a go at developing a pool game.

Not too sure how far I will get, but I've attached the first stage. At this stage I can aim the cue and adjust the amount of power on the shot. I've also implemented collision detection on the one ball (other balls hidden for now).

For the next stage I will introduce collision detection for the cushions.

Feel free to use and modify.

#################################################
# EDIT -
# Click on the PoolGame_LATEST.a2w attachment for the latest version
#
# Instructions
# -----------
# Down arrow to pull the cue back (increase power).
# Power is discrete so press the button once to increase power by one
# Up arrow to decrease power
# Left and right arrows to aim the cue ball
# Space to complete a shot
# Restart the game if the yellow ball is pocketed.
#################################################

davidghemer
12-01-2011, 01:01 AM
I've updated the code so that balls will now bounce off of the cushions using dummy objects as markers.

Also when the cue ball hits the one ball they move in fairly realistic manner - not perfect but you can plan straight shots or cut shots if you clip the ball.

Still thinking about how to bounce the balls off the cushions in a reasonable way. I need to know the angle that the ball hits the cushion at, but not sure on this right now. Any ideas would be welcome.

arty-fishL
12-01-2011, 03:41 AM
This is quite ambitious, not just for somebody who is fairly new to the forums, but for anybody. So far it is very good, I'm looking forward to the finished game. At first I couldn't work out the controls, but I worked them out eventually.

What I will suggest is to change the style of the left/right turning of the cue to abruptly. I made a compass (http://www.alice.org/community/showthread.php?t=7173), you could use elements of it to get the angle of the balls.

davidghemer
12-01-2011, 03:53 AM
Ok last update for the day. I've added support for pocketing of balls using circles as dummy markers for the pockets. It seems to work fairly well - slight adjustments of the dummy markers may be needed.

At this stage you will need to restart the game if either of the balls is pocketed.

davidghemer
12-01-2011, 04:04 AM
Some brief instructions for those wishing to play with the work in progress version.
- Down arrow to pull the cue back (increase power). Power is discrete so press the button once to increase power by one
- Up arrow to decrease power
- Left and right arrows to aim the cue ball
- Space to complete a shot

Restart the game if either of the balls are pocketed.

davidghemer
12-01-2011, 04:13 AM
This is quite ambitious, not just for somebody who is fairly new to the forums, but for anybody. So far it is very good, I'm looking forward to the finished game. At first I couldn't work out the controls, but I worked them out eventually.

What I will suggest is to change the style of the left/right turning of the cue to abruptly. I made a compass (http://www.alice.org/community/showthread.php?t=7173), you could use elements of it to get the angle of the balls.

Thanks for the feedback. Agree it is ambitious but thought I'd give it a crack. Thought it would be good way to find out some of the finer details and tricks of programming in Alice.

Turning of the cue has been changed to abruptly in the latest version. I'll check out your compass, sounds like it might do the trick.

davidghemer
12-01-2011, 06:49 PM
This version uses a bit of trig to calculate the angle of the cue ball when it hits a cushion and then to rebound at an appropriate angle. It is similar to the compass except I compare the current position of the cue ball with a previously marked position. The previous position is stored each time the cue ball changes direction and the end of a shot.

Rebounding for the one ball has not been changed yet, but the same principle could be applied.

Does anybody know if there is some way of adding properties to the balls? At the moment I have to put properties for the cue ball in the table class, which is getting pretty messy.

zero00
12-02-2011, 01:17 AM
Thank you so much for making this! :D
I had been trying to figure out how to do this for a long time!
(I didn't know the arcsin function outputs in radians :p)
I don't know what radians are, but I know some basic trig! :P

davidghemer
12-02-2011, 10:53 PM
For this version I've update the rebounding of the one ball so that it follows the same laws as the cue ball. I've also added some basic scoring (counts number of shots) and end game detection.

It generally runs pretty well when the balls are hit at low speed. At high speed it throws a lot of exceptions, so I would recommend only using power 1 or 2 shots.

I doubt that this will scale to a game with 15 balls. There are significant timing issues already with just two balls so with all the balls it just doesn't seem feasible. Another issue relates to decreasing the speed of the balls. Using a variable I can get a gradual decrease. However this doesn't happen when I use an array.

Anyway have a look and have a play and let me know if you see any obvious bugs in the code.

David B
12-04-2011, 11:31 AM
Unfortunately, I don't know how to play Pool, but I guess I will have to learn so I can play this game.

davidghemer
12-06-2011, 03:55 AM
Here's another update to the game. In this stage my main focus was decreasing lag in the collision detection and hopefully reducing the number of exceptions and incorrect behaviour.

To do this my main improvement was to calculate the angle of the ball periodically in the background, rather than on demand at the time when a collision with the cushion occurred. This works very well most of the time, except in the case where the angle needs to be calculated very soon after a ball is hit (especially when a ball is resting on the cushion).

The other main enhancement was to combine three lines of code into a single calculation in the method that handles collisions with the cushions. This worked surprisingly well.

I also slowed the ball movement down a bit which buys me a bit more time.

There's still a few problems, caused mainly by lag, but I'm not sure I can get much more improvement. Enjoy :)