Math is hard, let's go iOS programming!

by midge 19. April 2014 02:59


Good progress today. I probably put in about 2.5 hours. I'd like to explain what I finished, what I learned and all that, but it is getting late and I'm pretty tired. (So I said, then I went and did it anyways)

First off, I did get the helicopter animation in place for the character. That was very cool, and very immediately satisfying. Not that I was that far off from getting there last time.

What I immediately realized though, was that the image was probably too big to use and still have interesting game play. So I got to work on scaling stuff. Today I mostly worked on image transforms. I did some very modest refactorizations as well.

I noticed that I had been storing primitives (floats or ints) for a lot of rectangle sizes, but then I realized there was a struct for that (CGsize), so I started using that all over the place. Pretty simple change.

For scaling, there are built in easy to use scaling stuff for UIImage, but they kind of suck (I read). They leave you with some very blurry or jagged lines, just not nice things when scaling down. I found a piece of code online that scaled down pretty cleanly, so I used that.

I'm not doing scaling on the fly, but once at startup and then holding onto those UIImages in memory. Same thing for rotations.

Rotations were harder. My math is rusty, and I wasn't super interested in learning how to do the math for rotations, I just wanted them to work. I was guilty of some googling and pasting code here. However, the big block of rotation code I tried just didn't work.

I realized though, I could modify the existing scaling code for rotations by changing the CGTransform. I wish I knew this math and graphics code better, but this was not the problem I wanted to focus on. Fortunately, modifying the scaling code worked great (except it was upside down, so I flipped it =P). The only problem with it now is that the helicopter rotates outside of its original box, so some parts get cut off. This should be pretty easy to fix but I just called it a night because it was getting late and I was tired.

All this rotation stuff is just for the tilt effect when the helicopter climbs or dives. A lot of work for a stupid little effect that I think will look cool. And yes, I know I could have done this by just modifying the resources and saving a bunch more image frames. I didn't want to do that because it's tedious work, and it's less flexible. With scaling and rotation on the fly (I pun so hard) this is a lot more flexible. And I can tweak for what looks good on a whim, instead of spending all this time making resources only to realize I want more angles, more frames or a different size or something. Minimizing resources should hopefully keep the download very small.

So I guess the first thing to fix next is make sure the rotations don't get cut off on the edges. Then I'll get back to making the collision detection work with the new art, and slightly harder part, getting the collision detection to work with rotations... although I just had an idea to simplify the rotation collisions. I can test a big outer box area first (the actual animation frame) and only if collisions are detected there continue with the more fine grained collision detection.

I'm realizing that a lot of this code will be reusable for other personal projects, and that its awesome.

That's all for tonight!






Add comment

  Country flag

  • Comment
  • Preview

About the author

Hi, I'm Midge.

Month List

Page List