Math is hard. Life is Harder | The Brick Dead Project

Welcome to the third installment of The Brick Dead Project, a series of articles chronicling my misadventures to create a video game from scratch in the Unity 3D game development engine with no prior knowledge, experience, or clue. To catch up on previous posts in this series please click here.

Disclaimer: I am not the guy to learn this stuff from. These are the real, no BS adventures of the Starship Grandpop as I attempt to decipher the infinitely complex art of video game creation with no preparation or thought. Anything I say can and, probably, will be wrong. Enjoy the show!

Little did I know when I embarked on this little journey that I would not be learning not just one language (Unity), nor two languages (Unity and C#), but THREE languages (Unity, C#, and Math). Everything a computer does is math. We all know this yet we take it for granted, especially in this ooey-GUI world where every value and command is obfuscated by a dozen pictures. Computers are math. Video games are math on top of real-time math. 3D video games are math on real-time math on three-dimensional math. It can not be understated the shocking scale of the math involved. We’re talking Big Math. Hard Math. Big, Hard Math that will do unspeakable things to you in the shower if you drop the parenthesis. Now, you don’t actually have to solve the Math, but you do need to know it’s there, know when to use it, and know how to best leverage it. It is one thing to know that speed=distance/time. It is quite another to be staring at a ball on the screen and think about how your going to measure all those values yourself in a world that doesn’t really exist.

 300px-Quaternion2How bad does it get? Let’s start with the very basics. A vector looks like a point in 3D space defined by an X, Y, and Z coordinates. Except it’s not actually a point. It’s a direction. A direction from the origin of the world to your object, more specifically. It also has a magnitude, which isn’t actually one of the three numbers that defines it, but a whole ‘nother batch of Math altogether, possibly Euclidian, which sounds like a delicious flavor of ice cream. Anyway, you’re usually going to ‘normalize’ the magnitude of your vectors to be 0 or 1 for movement and… Video Games. Also, ‘normalize’ is a really hard thing to Google. Alternatively, you can ‘lerp’ or ‘slerp’ it which stands for ‘linear interpolation’ and sounds like a great thing to do to Euclidian ice cream. I do know that you can subtract the vector of your thing from the place you want to get to and it’ll automagically go there thanks to the power of… Confucianism, I think. Then, of course, things don’t just exist, but they’re facing certain directions. That brings us to ‘quaternions’ which are somehow fourth-dimensional, describe a rotation, and Wikipedia defines as: “Look, kid, you don’t want to know.”

Let me bring this back down to Earth for a moment with a little example: When you fire a gun in Call of Duty and hit someone to make red pixels fly out you are, quite literally, invoking Math on par with what was used to put men on the moon. This, more than anything else, is the reality of video games. So… Yeah, Call of Duty is rocket science.

That said, you can go surprisingly far while knowing little to none of this. I mean, once you get that blood spray in your modern military murder manager pointing the right way, you’re just going to cling to that one equation for the rest of your life. The really nasty stuff mostly comes into play when developing AI or ways to keep the camera on your player. Or vehicles. Or physics. Or… Okay, it comes up a lot. Still, Unity is jam-packed with little ‘helper’ values to demystify some of the math if you can crack Unity’s code. For example: “Vector3.up” always points up in the world where as “transform.up” points to what an object thinks is up. There’s even Quaternion.Identity which is shorthand for “I don’t give a s—t about rotation.” Additionally, a chatty Internet coupled with Unity’s buy-in investment of free means there’s a lot of help to be found out there and a lot of code snippets to copy.

Of course all the knowledge in the world won’t help if you don’t know which way is up.

This way is a very nice way. It’s pleasant down that way, too. Of course, some people go both ways – Scarecrow, The Wizard of Oz

I wanted my balls to make sparks when they hit something (Who doesn’t?). I had been working on it for hours. It worked but the little particle system that made the sparks just kept firing the wrong way. I felt like I had tried everything from changing values at random to drawing little pictures on paper. I had even copied some supposedly working code from a forum post. Still, the stubborn sparks kept shooting the wrong way!

I pounded my fist on the desk in frustration; A rare outburst.

Okay… Deep breath… One more time. We put the particle system at the contact point. Then we get the quaternion from the contact point to Up and then we destroy the particle system once it’s out of particles and… Wait… Quaternion.up… up… UP!!!!

Oh, dear Lord.

“Up isn’t up!” I yelled across my empty living room in the cold, dark hour of 1AM. I heard the cat bolt from the kitchen windowsill to take cover.

For the past week I had been iterating on the Breakout clone created from a tutorial video, happily carving my own vision from the rock of this original project. Since that Eureka moment of adopting a 3rd person viewpoint, The Brick Dead project had looked like this:

Unity 2014-03-09 14-26-10-18

This was what I saw, both from the in-game camera as well as the editor since zooming in to work. This was my world. This was what I thought to be true. Perspective, however, is quite subjective. What Unity saw, how the ‘real world’ actually existed, was something rather different.

Unity 2014-03-09 14-36-10-35

I had changed the camera, but I hadn’t really changed the game. Brick Dead was still Breakout. I was playing on a vertical plane. Up wasn’t up in my world! Forwards was up! Globally, at least. Locally, up was backwards. Literally. As in local.up = -global.z. My head hurt already.

I actually felt myself go pale at the revelation. Misbehaving particles were the least of my problems.

Oooookay… I saw three options. First, I could carry on my merry way with the knowledge that forward was actually up (except when it was backwards) and make sure to deal with things accordingly. Secondly, I could scrap the whole thing and start fresh. A third option led down the middle: Rebuild the game in a normal orientation but pick through and edit my assets and code for God knows how long to swap the Y axis for Z.

It didn’t take long to decide. Not being bright enough to handle the first option and too lazy to handle the second, the road right up the middle seemed like a great plan. Needless to say, the process took quite a while to complete. Little Gotcha!s sprang up everywhere, from oversights in scripting to the odd checkbox in Unity’s GUI to the simple rotation of every placed object and prefab. Every single asset in the project needed to be gone over, retooled, and recheked. It was a daunting task for someone still aspiring to noob levels to catch ‘em all. Finally, when the work was done, everything looked and played… Well, exactly like before.

Um… Yay… At least the sparks shot off in the correct direction.

Unfortunately, the fickle finger of fate wasn’t finished frobbing my fortune.

Data you don’t backup is data you don’t care about – Every good system administrator

Recover-Your-Data The system locked up. I sighed and reached for the reset button. It had been happening a couple times a week lately. I had been meaning to figure it out but there had been a rush of family and friends with buggered PCs that needed help lately so my own PC problems got pushed to the wayside. You know how your mechanic’s car always looks like the most busted PoS around yet is secretly a hot rod those rare times when it works? Same thing with IT folks. Except chicks never want to ride in your desktop.

Upon restarting Unity, I had the displeasure of feeling the color leave my body for the second time this week.

Every thing was pink. The objects of the game world still existed, but none of the textures did. Worse, the hierarchy that listed all the components in the current scene, the pink ones I was staring at, was empty. The Brick Dead project was dead. Bricked, even. Corrupted by a computer glitch.

Now, long time readers of GrowingUpOtaku.com know that I mention the importance of backing up your computer data on a fairly regular basis. Therefore, you are granted leave to take some measure of perverse joy in the following line:

I had not backed up once since starting to learn Unity.

Google threw me a bone on this one. I was lucky/persistent enough to find this forum thread with some possible fixes. Deleting the ‘Library’ folder got most everything back together with only a couple recent tweaks lost in the chaos. Comparatively speaking, I got very, very lucky.

I adopted a new mantra: “There is always time to back up.” After each work session I copied the entirety of my project to a backup folder on my hard drive and dumped another copy in my PC’s Dropbox folder which synched not only to the cloud, but also our home’s media server. It was a terrifying lesson I was determined not to forget. And with how crap my memory is these days, that’s sayin’ something! It’s one thing to loose data you had gathered, it is quite another to loose data you had created.

The backup proved to be a great boon. While I have as yet to experience another catastrophic failure on par with that one (Knock on simulated wood grain), it has bailed me out a few personal trainwrecks. Being fresh off the game-creation boat and yearning for adventure via experimentation, I’ve butchered a few perfectly good scripts and assets already. It’s great to be able to just restore from the backup folder rather than trying to undo the mess I can make of things in the name of science.

Speaking of making a mess of things, I really need to check out what’s behind these lockup problems I’ve been having. I’ll get to that right after I get this posted. And check the game bundle sales. When do those roll over? Thursdays? I think it’s Thursdays. Maybe-Aw crap! Look at the time! I gotta get crackin’ on dinner! I guess I never got around to sanding down that- Hey! Is that a quarter on the floor? Jackpot!

So very many shiny things in the world! Lemme just do a backup first. There’s always time to back up.

No comments :

Post a Comment