Today’s game tasks included a fun algebra formula that we all learned in grade school. Fun you say? Well, yes! The Distance Formula computes the distance between two points. Here it is –

Pretty easy to follow. Take the x position of first and second points and the y position of the first and second points and plug them into the equation, then take the square root of the result.

Why am I employing this equation in the DuneTraveller game? Well, I want the player to “search” around his current location to look for hidden clues and items. To know where to search he has to have been told to search somewhere OR he could just be hitting the “S” key on his own in a shotgun approach to finding stuff.

When the current level loads, I read and cache all of the hidden search points and when the player hits the “S” key (“S” is for search!) I loop through the cache and compute the distance from the player to each one of the search area points. If the distance is less than or equal to the declared radius then the item is revealed. It could be a hidden note, or maybe even some loot.

The problem I encountered is the rather dearth of conversion functions in Swift. The second problem is that the output of some of the built in math functions is inconsistent. “pow” which is used to compute something like “what is 4 squared?” outputs a decimal. However “sqrt” takes a double. Turns out there is no direct conversion in Swift.

Thanks to this post on Stack Overflow the code looks like this –

You will also notice I still wasn’t finished with my conversion party after getting the square root. I had to cast the searchRadius (which is an integer) into a double before performing the comparison.

It’s fun learning a new language’s quirks.