Monday, July 14, 2014

Some pieces of code that can greatly help you in game programming.

One of the major problems I have faced during game programming is that some objects do not appear where I expected them to or they do not appear at all. Suppose you have created a new enemy inside your game and have coded up a complex movement behavior for it. The only problem is that when you run the game you can't find this enemy-object. It's not where it should be.

These things happen when you mess up your code or put a 9 instead of  a 0. If the analysis of code doesn't reveal the culprit, we debug this using the debug feature of eclipse and observing the position variable value of that object or using 'logcat' to log the same.

These 2 methods are painful and take much time, considering that we only want to know the position of an object. So I have created two static classes which contain some OpenGL ES oriented code that are extremely helpful in these situations and are quite easy to call.

On of them draws a line from a given starting point to a given end point. Give it a known point in the game world as the starting point and the position variable of the missing object as the end point. Now, even Osama bin Laden can't hide from you.
The second one draws a dot at a custom position. An example use of this is to mark the corners of a polygon generated by your code when doing a custom collision detection.

By default the code uses GL ES 1. Modify the GL ES instance if you are using another verson.

NOTE: If the active gl matrix is not the model matrix , call
and then revert to the earlier matrix at the end of the draw(..) function.

The Line class:
The Dot class:

No comments:

Post a Comment