Getting a Gist Of My Code

 

I know I don’t post very often – believe me, 2015 and 2016 have been a helluva ride – but I have been writing a lot of code. I mean, a lot. A lot of the code I write, since it’s Unity, I have compartmentalized in scripts that I import over to a multitude of projects. Seeing as how a lot of these are used and fleshed out in projects that will never see the light of day, I thought “Hey, why not just put them up on the site?”

So, I’ve created a Github account, and I’m placing them in their own GitHubGists (or Repositories, if they span multiple files). This would be a quick, dirty, and effective way to get my code out to you guys so maybe, just maybe, it can actually be used in a project somewhere.

Any code I put up in my Gist, I’ll also link to it somewhere on this site as well for direct linkage.

So, go have a look at my GitHub Gist! Maybe you can find something useful there!

Quick Code Tip: Fix Precision with Floating Points

Let’s say you have a floating point value of 200.21, and you convert it to an int, like so:

int amount = (int)(famount * 100);

Depending on processor, libraries, compiler and tons of other stuff, your value may APPEAR to be 200.21, but when you do your conversion the value comes out to 20020. What happened to that extra 1?!

Well, though you may see 200.21, the computer is actually storing 200.209999999… (because, you know, 0.999… == 1).

This doesn’t happen all the time, but when it does, it’s a pain. So how do we fix this?

Just add 0.5. Seriously.

int amount = (int)((famount * 100) + 0.5f));

Bam. Let’s break this down, PEMDAS style.

(famount * 100)
Ok, so we know that famount is not 200.21, but 200.2099999… This now brings us to 20020.99999…

((20020.999…) + 0.5f)
The 0.5 now brings us to 20021.49999… Like magic.

int amount = (int)(20021.4999…)
Saving all the pennies.

Good job. You just saved the world from mathematical inaccuracy.

Until next time, take care. (After that, do whatever the heck you want.)

C++ Link: A-star Shortest Path Algorithm (C++ recipe)

Pathfinding A Star
Image via Wikipedia

Thank you, StumbleUpon! I just so happen to have come across a handy code snippet for the A* pathfinding algorithm.  This is pretty much a staple in any AI programmer’s arsenal.

So here’s the code! Enjoy, peoples!

A-star Shortest Path Algorithm (C++ recipe)

Code Snippet Quick Reference

I have created a quick way to find the code snippets you’re looking for! The Code Snippet Quick Reference under the Code tab at the top. Now you can find all the snippets for C/C++, Unity, and even the Math Algorithms all in one easy place (OK, technically 3, but shut up; I’m trying to do you a favor, here!).

Unity 3D Code Snippet – Inheriting Parent Velocity

Imagine that you’re running at 10 MPH. While you’re running, you throw a ball at 5 MPH. Now, imagine the laws of physics are screwed up and the ball, instead of adding your running velocity to its own, only moves at its 5 MPH. Imagine the look on your face when you try to figure out how the heck you just did that. Imagine the towns’ people accusing you of being a witch and burning you at the stake. Gruesome.

This is (sorta) what happened to me. I instantiated particles, but they didn’t pick up the instantiators’ velocity. Well, here’s a handy script I found, and now I am sharing it to help get your game up to speed (Ugh. I rather just be burned at the stake than to hear another corny pun).

Continue reading

Unity 3D Code Snippet – Obstacle Detection Component

I’m working to get a good portion of my AI up here (in tutorial form) for my Unity 3D Flight Combat game. Since AI is a bit more complicated than a simple flight script, I’ll put it up piece-by-piece in the form of modular devices you can use for other aspects of the game. Today, I’m going to show you how to give your game a bit more insight (These jokes are killing me).

Continue reading

Unity 3D Code Snippet – Flight Script

Roll, yaw and pitch axis definition for an air...

Flying is a simple concept in games. An object that can move in 3 dimensions? I am all over that! But how about rotating to the forward vector? How about a constant velocity? Here is a quick way to get your game object up in the air (that joke hurt to type).

Continue reading