## Optimizing For Loops

I am pretty obsessed with getting every little bit of performance I can out of my code, especially when developing for mobile platforms. For loops, believe it or not, can be optimized using some very simple tricks. Normally you probably write a for loop like this:

for (int i = 0; i < some.Length; i++) {
// Code
}

If you know that the length of whatever you're iterating is going to be less than 256 (0-255) you can use byte instead of int, if you aren't completely certain, you can probably get away with ushort (0-65,535). This will free up 24 and 16 bits as compared to int respectively, it's not much, but it can add up. You can also pre-lock the length into a variable to help speed things up. The reason this works is because for loop check their condition (second block) every single iteration. This means that you are accessing the length property every single iteration. Accessing a member of a class is more expensive than accessing a single variable, and thus adds up over many iterations. The performance change might be slight, but it adds up pretty quickly, especially if you are iterating through hundreds of pieces of data every single frame. So, what should the for loop look like? Well, like this:

byte length = some.Length;
for (byte i = 0; i < length; i++) {
// Code
}

## Finding Initial Velocity Given Angle and Distance

Most of the time a physics problem involves using a given angle and velocity to figure out where a projectile lands. This is all well and good, but when working on a game with projectiles you already know where it should land, and what angle it should be launched at. This article shows the process by which I solved the basic physics equations to take in the desired difference in distance, height, and launch angle, and give you the required velocity in return. If you aren't terribly interested in the work, you can just skip to the solution. Without further ado, here's the process by which I solved the equations.

## Rearranging and Replacing

To make things more obvious, we will replace the differences with a single variable, namely $\large{d\ =\ x\prime\ -\ x}$ (where $\large{d}$ is the intended travel distance), and $\large{h\ =\ y\ -\ y\prime}$ (where $\large{h}$ is how high the projectile starts relative to its ending height).

## Eliminating $t\!\!\!\;$ ime

In order for this equation to work, time cannot be a factor. In truth, it really doesn't matter how long the projectile takes to get there, just that we arrive. If you need time later, just divide the range by the $\large{x}$ velocity component. Since the $\large{y}$ equation has a quadratic for time, we'll solve that one.

Using the condensed height equation in $\large{\eqref{y2}\!}$ , we can solve using the quadratic formula (and some basic negation cancellation).

Since we know that what is coming out of the square root is positive, we can eliminate the possibility of a negative option as being the one we ultimately want. The projectile is supposed to be moving forwards after all.

## The Heart of the Matter

Now that we have time eliminated from $\large{\eqref{t}\!}$ , we need to plug it in to the $\eqref{x.2}$ equation.

Then we need to solve that monster for $\large{v}$ . We start by moving $\large{g}$ over, and distributing $\large{v\cos\theta}$ .

We then isolate the square root and square both sides to eliminate it, and consolidate common terms.

We continue to eliminate like terms, extract the velocity component, and re-balance.

Using trig identities we can simplify the equation, and keep like terms.

We're pretty much done here, just need to take the square root and integrate it into code.

## Putting it into Code

Coding it is pretty straight forward, below is an example function that I wrote to handle doing the mathematics of finding the velocity.

// This function determines the required velocity to reach the range at the given height and angle
private float DoTheMath(float distance, float height, float doubAngle)
{
// The lowest angle is a direct shot at an infinite velocity
float lowestAngle = Mathf.Atan2(height, distance) * 2;

// If the angle doesn't fall within the (lowestPossible, 90) range, there is no solution
if (doubAngle >= Mathf.PI || doubAngle <= -lowestAngle) {
return 0;
}

// I figured this equation out after working through the standard physics equations. See: http://wp.me/p35fKy-4k
return Mathf.Sqrt((9.81f * distance * distance) / (height * Mathf.Cos(doubAngle) + height + distance * Mathf.Sin(doubAngle)));
}


## Particular Problems with Public Variables in Unity

The Unity documentation it indicates that all public variables are made available in the inspector for direct editing. While this is handy, it is also dangerous. This article will cover a few things regarding public variables, and how to avoid them in Unity.

## The Importance of Class and Object Oriented Programming

While class is certainly important to have in real life, it is equally important to have while programming. Taking advantage of an Object Oriented Programming method (or OOP method) may very well be one of the most important higher concepts to learn and understand. This article helps cover what OOP is, its benefits, and how to use it.

## The Case for C# in Unity

I would like to take this post to explain why using C# in Unity is a better idea than Javascript. If you end up not needing multiple returns for functions, non-type-specific logic, or are okay with the wonky auto-completion for Javascript, then that is okay. On the other hand, if you want to take advantage of those features, and more read on.

## Windows + DirectX + Minecraft Server Automation = Wat

A little while back I made mention of some Minecraft Server Automation programs that I would be working on. They were nearly finished when I had some data loss on my development machine. Sadly my backups were not very current. Fortunately this gives me the opportunity to rework the programs in a more interesting manner.

## The Pain of Windows

Doing my server automation programs on the windows platform, it was inevitable that I would have to #include <Windows.h> at one point and deal with one of the many 12 argument functions it has. Alas that day has come.

## C++ errno Values

For those of you wanting to know what exactly the numbers that errno is set to correlate to, I have listed them below: (this way you can set errno and then when someone uses strerror(errno) they'll get the right error)

## File System Library

After doing some looking around, and trying to get the Boost File System library to work I realized that had I spent the same amount of time and energy into writing my own file system library, it'd be done and usable by now.

Don't get me wrong, the Boost libraries are very comprehensive, I just had trouble building the extra libraries, getting them properly integrated, and when all was said and done it didn't even work properly due to "Improper Permissions" (despite running my .exe as an administrator).

So I'm going to make a library/dll combo to manage some basic file operations (copy, move, delete, rename).

If there's a file op you want just comment and I'll work it into the C++ source.