Basketball

My very first sports game! A simple basketball game. Get the ball into the basket. No nonsense, really. The physics for this game was relatively simple. And fun. But then again, physics is always fun.

The key here is to setup a good aiming system. The game builds itself around that. One of the best solutions is to represent exactly half the parabolic path of the ball. The player will still have to judge the path of the ball, but the portion that we show on the screen would give him an idea of the velocity and angle.

Our interest is in calculating the path of the ball, which is determined by two parameters:

  • The velocity
  • The launch angle

Given that the mouse pointer represents the highest point on the parabolic path, both launch velocity and launch angle can be calculated. It’s really simple (should you have taken physics in high school).

Parabolic path of the ball

 

In the above equations R is the range of the projectile and H is the maximum height to which it rises. It’s easy to see why, for a given position of the mouse, R and H are constants that can be calculated. Rearranging and solving these equations will give us the velocity and angle which is what we need to describe the motion. The velocity will then be resolved into components, for convenience.

The game depends on a few libraries like SOIL for texture loading, freeglut for openGL and irrKlang for audio.

Coming up next is a version with highscore tables! Gotta learn a little php for that 😛

Edit: High score table is live but a little buggy.

Available on gumroad:
Basketball

Source

Tower Defense

First tower defense game 😀 Of course, it doesn’t have all the bells and whistles of commercial games. But it’s simple enough to play a couple of times.

If this project taught me anything, it’s that some things are easier with Vectors. The key to any good tower defense game is predicting the path of the enemy in order to hit!

Diagrammatic representation

Simply aiming at the enemy when you’re about to fire is no good. He isn’t likely to hang around there for two long!
The problem and its givens are listed:

We know,

  • The position of the enemy(which moves) at the time of firing
  • The position of the gun (which doesn’t move)
  • The absolute velocity (speed) of the bullet. (The direction is what we need to determine).
  • The direction and magnitude of the velocity of the enemy

My approach was as follows:

In order to hit the enemy, the final position of the bullet and the enemy must be the same. Also, in time t, the displacement of the bullet and the enemy must be equal. This is written vectorially as:

Which on expanding,


And on squaring and rearranging,

Where cos θ is given by,

Solving this equation using the quadratic formula, and selecting the positive root (since time can’t be negative, in this discussion), will give us the time after which the bullet and the enemy will collide.

Velocity of the bullet is then given by:

Keep in mind that they’re all vectors, so you need to write appropriate classes/structures to deal with them.

Game Screenshot

Source

Executable (Windows) 

Shoot ‘Em Up

Shoot ‘Em Up is a simple shooting game, where you have to survive for as long as possible. Enemies spawn at random locations and move towards you.

Kill them by firing bullets.

This particular game involved a bit of math, especially making the enemies re – orient themselves when the player moves.

It’s trigonometry time!

Here’s a short of explanation of the math behind that particular feature:

For this game, I’ve written a small 2D Vector class which uses Unit Vector Notation to represent vectors.

Both the player and the Enemies are objects of the particle class. The floating point co – ordinates, and the velocity of a particle are stored as vectors.

The acute angle between the line joining the two particles, and the  x – axis is given by:

Basically, the enemy must move along this line, if it is to reach the player. When this angle is calculated, the velocity of the enemy may be in some other direction. Our goal now, is to “point” the enemy in the direction of the player without changing his speed.

Speed is defined as:

The mod() function of the Vector class gives this result. To re-orient the velocity vector along the new θ, keeping it’s magnitude constant, we calculate the new x and y components as follows:

There is one problem. θ will always be between 0 and π/2. Hence cos θ and sin θ will always be positive. To get around this, we check the sign of Δy and Δx. If Δy is negative, multiply the y component by -1, and if Δx is negative, multiply the x component by -1.

Pointing bullets towards the mouse works in a similar fashion, where the position of the mouse replaces the position of the enemy. Also, the bullet is pointed towards the mouse only at the time of firing.

So there you have it. Application of high school maths and physics in programming. Hope this helped 🙂

Screenshot of the game:

 

Download the game

Download the source

View the help file