Batting Practice Quasi-Simulator



The super duper batting practice simulator consists of a pretty simple concept: hitting a ball with a bat. The user controls the on-screen action with some lucky clicks of the mouse. The ball gets pitched, and the user swings away in hopes of knocking one over the fences. The game works like so:

1. Click the mouse and the ball gets pitched. Get ready, because it comes fast (with approximately the speed of a 100 mph fastball, actually)
2. Click the mouse again to start your swing.
3. Watch it go flying! ...unless you suck and you missed.
4. Unless you hit a foul ball, the score (distance you hit the ball) will be printed to the console once it touches down. Hit 'r' to reset the ball to the pitcher's mound, and to start the whole thing over again.
5. You can also hit the 'c' key to switch between the 6 camera angles at any time. The outfield cameras work best after you've hit the ball, because it's kinda hard to hit from 400 feet away.

Implementation notes:

I used real-world physics equations to control the movement of the ball in the air, but (I'm guessing because of GLUT's sketchy control of how often idle gets called) this didn't work out much as intended - for example, the gravity more resembles something you might find on the moon. Also, coming up with a decent collision equation was near impossible. In its current state, line drives hardly bounce at all and fly balls bounce way too high. However, I cared more about making this halfway fun and fast-paced than I did about burying my head in physics textbooks or creating a realistic physics simulator.

Another problem I ran into, again stemming from GLUT's idle control, was that frequently the idle function would not be called while the bat was making contact with the ball, thus causing the user to swing through a pitch they should have hit. As a result, I had to allow for some leeway in terms of how far outside its supposed limitations the collision detection could operate. A side effect of this is that sometimes the user can miss a pitch and the ball will still react if hit, but again, a compromise had to be made and I'd rather allow for more hits than more whiffs.

Source code: here
Executable: here