Sorry, this isn’t a continuation to my last post. I’m meaning to make that happen, but I gotta get this one done first. This time I’ll try to keep it short and sweet, promise.
So, work on project “Hop & Pop” is coming along nicely. I managed to reach my programming deadline for the demo ahead of time and a few people have promised to help me out with graphics and sounds, which meant that I could keep working on new features. Kind of on a whim, I decided to add some gore into the game in the form of giblets and blood. Since I’m not using Unity’s own physics in any way anymore (topic for another time) I couldn’t really use Unity’s built in particle-system, which meant I had to use my own physics for the particles too. Since I’ve had my characters working for a long time now, I could easily copy their physics for the giblets as well. Except, I encountered some problems. For some reason, with the giblets I ran into problems with the float data type (floating point numbers).
In the past I haven’t really payed that much mind to back-end stuff like how data types actually work, but when my giblets starting flying all over the place with no apparent reason, I suddenly found myself knee-deep in the swamp that is floating point numbers. I identified the problem easily enough: my code thought that X was smaller than itself. To put it a little more nicely, I had two variables ,which when printed out were equal, but when compared were not. I had come across this same issue earlier when programming my character physics, but there I had managed to go around the issue somehow.
Long story short, the fault wasn’t mine or Unity’s, but the problem was inherent in floating point numbers: they’re not 100% accurate all the time. Well, that’s not really how it is exactly, but that’s the best way to simplify it.
So after some digging around, I found out that there is another, more accurate data type: decimal. I jumped on that immediately and my code started working. Unfortunately (as I had feared somewhat), the performance of my code suffered somewhat. I didn’t want that to happen, so it was back to the drawing board. Unfortunately, I couldn’t fix the issue without some kind of workaround, so in the end I converted my floats to strings and back again. Not very elegant, but it works for now and performs better than using decimals.
I’ll have to revisit the issue eventually, because my giblets are very CPU-hungry as they are, but as I still have tons of features left to develop the time for rewriting isn’t now. For a bit more in-depth look at the issue at hand, check out these two links:
http://effbot.org/pyfaq/why-are-floating-point-calculations-so-inaccurate.htm
http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html (this one is a doozy, very scientific)
Whew. I did it! A somewhat coherent, but not overly long post! And I didn’t even spend several hours writing it. Gotta keep this up.