## 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)));
}


Raycasts are extremely versatile. With raycasts you can see if there is something under the mouse, in the path of a straight shooting object, or just about anything else regarding something being in the path of something else. As far as calculations go, raycasts are probably one of the most expensive (in terms of computational time) things that you can do. But how expensive are they? Well, let's put things into perspective about just how expensive raycasts are. We'll look at the basics of raycasting and how it is used with great frequency in a standard setting.

## Raycast Basics

What exactly is a raycast, how does it work, why is it so expensive? A raycast has an origin and a direction, like any vector, and is literally cast, or thrown if you will, into the scene. The process basically involves going through every object, then checking ever vertex, face, and edge, for an intersection. Even with rigorous optimization this can be checking against hundreds of elements, and this is what makes it computationally expensive.

## How it's Used

While it is relatively expensive, it is exactly how the scene is rendered. On my desktop that means 1680 x 1050 = 1,764,000 raycasts per frame just to display what is going on. On my tablet it's 1920 x 1200 = 2,304,000 raycasts per frame and 960 x 540 = 518,400 for my phone. So while it is an expensive calculation, remember that it is happening half a million to two million times a frame just to render, so doing it another 30 or so times is no worse than a 6 x 5 grid of extra pixels.

## Getting Trig Quadrant from Sine and Cosine with only 2 IF statements

So, I was working on a shader network today and needed to get the quadrant of an angle in a simple fashion. In most cases people will use a switch statement or an if, else tree. I wanted to do it using fewer steps, so I started to look at it a different way: