One thing I always considered good practice is “If you know what you need, and ‘the wheel’ is over-bloated, it’s OK to reinvent.” I looked at animation systems involving many awesome features, but I wanted something really, really simple. Named after my room mate, here is the Super Simple Animation System (SSAS).
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.)
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!).
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).
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).
Man, I wish I had this blog going through school, because this place has become my online notebook. Game development is like riding a bike – but math, for me, can have a hard time stickin’. I’ve been working in databases so long that I have a lot of bit-shifting down, but my matrix and vector math is starting to lack. So, a creative way for me to remember all these algorithms is to try to explain them. Today, I’m going through Linear Interpolation, Spherical Linear Interpolation, and Normalized Linear Interpolation.Continue reading “Math Magician – Lerp, Slerp, and Nlerp”
I program applications that run on IBM AIX servers at work. Multiple people will be running different instances of my programs, and if any of my programs are writing to a file at any time, it could become problematic. For example:
- Person 1 runs MyProg, which opens MyFile.txt, which contains the text “Hello, World.”
- Person 2 runs MyProg, and also opens MyFile.txt, which contains the text “Hello, World.”
- Person 1 now makes edits to MyFile.txt and saves it, so now it reads “Hello, World. Beautiful day today!” He exits the program.
- Person 2 then makes his edits to MyFile.txt, which reads “Hello, World. Person 1 is a jerk!” and saves.
- Person 1 gets fired.
Because there’s no locking, data can get overwritten without anyone noticing.