The Maths, it burns!

Whilst I am someone who enjoys a bit of maths*, would love to know more and, when playing the theoretical game of “what degree would you do if you could do another one?” would put it up there with physics or generic-interesting-science-I-just-read-about-on-slashdot, I do find I tend to struggle when it comes to aspects of 3D code I feel I should find relatively simple.

For instance, yesterday I decided to implement touch controls. Getting a reasonable framework for touch inputs, to get them from objective C into C++  and fire in my game, took a little bit of thinking about to get right, but was fairly quick. Then I got to the point of taking those inputs and turning them into pan and zoom functionality. It’s not that making the camera pan and zoom in vaguely the right direction took much time, but to get whatever was under my finger to stay exactly under my finger as I panned was a little tricky and to get zooming to work, so that it zoomed precisely between my fingers and kept what was under each finger under that finger exactly, took me about three hours.

 The order of operations I found to work after trial and error (the worst sort of programming, as it often shows you don’t really know what you’re doing, as you’re doing it) was this:

  • Take the inputs in screen space (320 * 480 resolution, not the 640 * 960 graphical resolution of the device)
  • Translate by half the screen space towards the origin
  • Divide by the camera scale
  • Divide by the screen space
  • Multiply by the relationship between the frustum and the viewport (I have this encapsulated in a function called GetMysteriousMultiplier as it’s simply multiplying by 2 and this was my best guess as to why)
  • Subtract current camera translation divided by camera scale
  • Turn this into a translation matrix
  • Translate camera by the matrix
  • Scale camera
  • Translate camera back by the matrix

Now it’s not that I didn’t work out I had to do most of these right from the off, but not quite all of them and not quite in that order and getting it right took about as long as writing everything else did in the first place. Try as I might to picture the scene in my head and do the transformations one-by-one to work out what needed to be done, after enough steps it slipped away from me. Suffice it to say, my MysteriousMultiplier still feels a little bit like this: http://blog.stackoverflow.com/wp-content/uploads/then-a-miracle-occurs-cartoon.png and I sense it will come back to bite me in the ass at some future juncture (the fact that the first place a google image search found me this image was stackoverflow tells me I may not be the first person to have this sort of issue).

*I spent Christmas 2011, at my parents’ place, writing out every aspect of the Navier Stokes equation  for fluid dynamics on 20 odd sheets of paper to try and get my head around it. I came pretty close until it came to implementing it, then the kernel smoothing functions broke my dainty little mind.

Advertisements

One thought on “The Maths, it burns!

  1. And of course I managed to botch the zooming code before checking it in. Fortunately I could get it back in working order by checking the order of operations I posted here. At least that saved me a total horror 🙂

    NOW FOR CHECK-IN!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s