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

Project Euler – Problem 9

Problem 9 is sort of easy too! 🙂

The problem statement is as follows:

A Pythagorean triplet is a set of three natural numbers, a<b<c, for which,

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Knowing a little maths can take you a long way in this problem.

Mathematical Discussion

(skip this it bores you. The problem can still be solved without it)

Knowing this can cut your run time from 0.291 seconds to 0.001 seconds (as measured by the ctime library), which is basically 99.6% faster!

Basically, Pythagorean Triplets are an expression for possible integral values of the sides of a right angled triangle. Armed with this little nugget of knowledge, we can effectively reduce the iterations that we have to go through to find the required triplet.

General Algorithm

  1. We know that a + b + c = 1000.
  2. Therefore, m2 n2 + 2mn + m2 + n2 = 1000. So, 2m2 + 2mn = 1000.
  3. We now attempt to get the answer by applying brute force on the above expression, rather than plugging in all possible values of a, b and c.
  4. I believe that the brute force method merits no discussion. It has been included in the source, for comparison with the above method.

Solution in C++ 

Project Euler – Problem 7

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10 001st prime number?

Problem 7 is pretty easy too!

General Algorithm

  1. Begin testing numbers to see if they are prime, starting from 2.
  2. Break the loop when you have found 10001 prime numbers.

It’s that simple. 102,892 people have solved this problem.

Run time: 0.062 s.  Do let me know if you manage to make an improvement.

Solution in C++

Project Euler – Problem 4

The problem description, from the Project Euler website:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 ×99.

Find the largest palindrome made from the product of two 3-digit numbers.

Again, a little brute force never hurt anybody 😛

General Algorithm

Before you begin, you must identify how exactly your going to check if a number is a palindrome. The question defines a palindrome for you: A palindromic number reads the same both ways. IMO, the easiest way to check if a number is a palindrome is to reverse it and see if it is equal to it’s original value. Given below is a general reverse algorithm.

  1. Initialize reversed number to zero.
  2. While the number to be reversed (n), is not zero:
    1. Get the last digit by n % 10.
    2. Make it a subsequent digit of the reversed number by doing the following:
      1. Reversed number  *= 10.
      2. Reversed Number += last digit.
    3. Remove the last digit (n /= 10).
  3. At the end of the loop, you will have the reversed number.

I guess the iteration for checking the product of every 3 digit number needs no explanation.

Solution in C++

Note: If you’re having trouble with _int64 on your compiler, replace it with long long int

Falling Balls listed on games.softpedia.com

Falling Balls, which recently got listed on download.com, is now also listed at softpedia.com! It’s got about 9 downloads so far.
Visit the page or directly download: 

Dictionary

A Windows Dictionary App, written in C++. Unlike its cool cousin Mac OS X, Windows doesn’t come with  dictionary. Not that it’s a feature to die for. But still. Always nice to have it, in case you need it.

I’ve used SQLite3 to retrieve entries from the Data file.

Database management is an integral part of app development, and one that I had no idea about, until recently.

I’ve tried to make the client as small as possible (478KB).

Screenshot:

Download the App

Download the source

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

Brickbreaker Gameplay

Tic Tac Toe OR (X & O, if you like it that way)

Who hasn’t played Tic Tac Toe ?

Also know as Noughts and Crosses, its one of the best games to fill those idle moments, any-place, any-time!

Having previously written a console version of the same game, I thought it would be cool to rewrite the game with GUI elements, and to update the logic a little bit.

Tic Tac Toe is really easy to program, even for a newbie.  Try your own hand at it, or look through the source code to get some tips.

See the animation below:

It’s not that hard to get the hang of. First person to get 3 X’s or O’s in a row wins!

Download the game

Get the source