Thursday, May 3, 2012

Math Rules. Also this: *=

Today I feel like writing about game math and stuff.

First, The things people say and have said about Math is a lie. Math rocks super hard. For example, you see that math dude over there with the glasses? Yeah. Underneath all the annoying ticks and unbelievable amounts of dandruff is a Wizard.


Do you want do control fire or lightning?  Do you want to conjure strange beasts from your imagination. Yes? Do Math. I Wish a younger me knew this and believed it.


booyah


Second, I want to talk about this: *= . Yep, that thing.  Don't be seduced by it's charms - the lovely looking asterisk and the striking parallel lines. It's not as awesome as you might think. I've used it and am full of regret.


Here's how I started using *=

I wanted to make a 2D platformer.  I didn't know how to build a game, so I used Game Maker. I found this guy's Script for a Mario. It was pretty good minus most of it. So I ripped apart the code to figure out the magic behind it and make it better. (Aka What happens when I change this number? What happens when I delete this condition? Who really even wants this line? Good times.)

One of the neat-o things was how the dude did friction.

MariosRunJuice *= 0.9f;

So here is *= at work. Sure it looks pretty amazing and kind of does what you want. Say Mario is running at a super fast speed of 10 blocks a second (320 pixels per second?). If you stop pressing the Run-Faster-Now button (great button btw), mario will go 10 blocks, then 9 blocks, then 8.1 blocks, then 7.29 blocks. which continues all the way down to almost 0.

So mario slows down at this sort of natural rate. Yay! You've achieved friction by going *= a percent.

It also has the added benefit of being unsigned. You don't have to worry about accidentally flipping directions, and all that craziness. Also it looks real pretty and elegant and should be asked out on dates.

The problem is that *= sucks. Why? It causes exponential change, which sucks. It's the same reason capitalizing interest sucks for those who took the student loans (me) and totally amazing for the person who handed out the loans (banks and others.) Those numbers go out of control bonkers.


I'm trying to say that it's super hard to control *= and make the motion/controls feel good.  Finding the exact curve you, another designer, or some focus testing wants is stupid hard.  If you want Mario to slow down faster but take longer to stop (Which totally doesn't make sense.) *= can't do it alone. It'd need help from ugly nested ifs or weird threshold stuff. That code just feels gross.

Instead use -=. Yes it takes more over head and more conditions. But those conditions probably involve 0 or 1. And 0 and 1 are some pretty amazing numbers. (Trying pointing to a 0 of something. Totally impossible. Go Brahmagupta! (And Google for helping me find that dude's name!))

Also probably include Time into your physics.Variable frame rates can ruin nice looking physics. Also it's nice to be able to find the position of Mario or a Goomba at a given time with networking or replays or something.

Anyways, going back to Math being absolute magic. I really think graphing calculators and linear algebra are pretty much the same stuff as Mario and Sonic but without all the fancy colors. When Mario jumps and doesn't die horribly, that's probably because your brain just used a bunch of fancy numbers to make you jump at exactly the right time. Which is pretty awesome.


Math rules.