Welcome to my Programming Blog...

I am currently working on a couple of projects: An original game called "Implosion" which I am porting from Flash to the iPad, and a remake of Q*bert in Python (pygame), as part of the class I am teaching. Please feel free to use the "Labels" (at right) to follow a specific project or theme. If you are one of my Python students, I recommend that you check out the Python thread.

Thursday, April 29, 2010

Coily AI

I've been working a bit on making the snake, "Coily," chase Q*bert around the pyramid. I came up with an algorithm for deciding on which way the snake should jump, based on where he is and where Q*bert (his prey) is:
Deciding whether Q*bert is above or below the snake is pretty easy - I just compared row numbers. But picking between the left and right was more of a challenge. Eventually, I came up with an expression:
leftRightCriteria = coilyRow-preyRow - 2*(coilyRowPos-preyRowPos)
I found that if this "leftRightCriteria" was less than zero, then Q*bert was to the left of Coily; if it was greater than zero, then Q*bert was to the right of Coily; if it was zero, Q*bert was directly above or below Coily. So now I could apply this to the decision tree.

                

Tuesday, April 27, 2010

The grid

I wanted to share a picture - a 7 x 7 grid of "Chips" - a random selection of four types, rendered on the iPhone simulator with OpenGL, via SIO2.
Now we're cookin! The game will involve removing some of these pieces and allowing the remaining ones to fall in.

Ah, the memories...

Boy, my C is rusty.

Last night, I was working on the Implosion project with SIO2. I am still fighting with the C++/Objective C interaction. My problem last night was that in my Objective-C class, I had an array of NSString objects. I needed to select one of these strings and send it to a method in SIO2 that was expecting a C-style "char *" variable.

It wasn't hard to find a method in NSString called "UTF8String" that does a conversion - but it converts the NSString to a "const char *" string, instead of a "char *" string. And this is where I got bogged down. This is all C/C++ code, and I haven't done that in a few years. The const keyword meant that the string can't be modified, and although there were some sites on the web that suggested that I cast it as a non-const variable, I really don't want to modify the original string in the NSString variable - I will need it again!

Eventually, the magic typing monkeys in my computer that make up the Internet gave me a series of websites that brought me closer and closer to the answer. I ended up at a site that gave me the final piece. Here is what I wound up doing:

    const char* tmp = [typeString UTF8String];
    char* typeCString = new char[strlen(tmp)+1];
    strcpy(typeCString,tmp);

... and that worked just fine. With this, I can make multiple shapes in a 7x7 grid. I have two new short-term goals:
  1. In blender, I need to reduce the size of the shapes (again) - they are still too big and overlap each other. (I also need to make sure that the triangles are centered on their own local origin.)
  2. I need to find a way that I can make the chips "fall" in when a chip is removed.

Monday, April 26, 2010

Bouncing Balls

Well, I was able to get some balls to show up on the Q*Bert grid and start bouncing down the cubes. In fact, I cranked up the drop frequency so that they fell almost every frame - it looked a little bit like water flowing. After that, though, I throttled back on the balls and let them come in multiple colors:
They now come in red, green and purple (not shown).

Next up - collisions with the balls and Q*Bert, followed by spawning other critters.

A Row of Rings

I think I'm starting to get the hang of this objective-c vs. c++ stuff. I was able to create a obj-c class that can keep an NSMutableSet of objects - I was able to clone a row of purple rings. Progress!

Sunday, April 25, 2010

Q*Bert Graphics

I had a slow moment on Friday, so I made some pictures of Q*bert:
I've got him changing his orientation as he hops around. It's good for now - he has a very light grey outline, since the graphic was originally blended with a white background. It isn't too noticeable when he is in front of a cube, but it is fairly obvious and distracting when he is in front of the black background in the game.
I'd also like to make him seem to jump more when he is in flight. Perhaps I can make his feet point downwards more.

Next up: enemies!

Cross-language issues in SIO2

There are some interesting issues trying to combine the SIO2 library with an objective-c core. Most of the SIO2 library is written in C++; using XCode, however, I am more familiar with Objective-C. My plan has been to write my main program in Objective-C and just use the SIO2 library for rendering. The two programming languages are cousins, and I knew that in theory, XCode and its gcc compiler could handle both. It certainly did a fine job of handling them separately....

Well, I was right, it can, but getting the computer to do so was more of a challenge than I expected. There is a trick - there is a setting associated with each source code file that says what language it speaks. When I wrote a file that spoke both C++ and Obj-C, I needed to specifically tell the compiler that it was a multi-lingual program - and I didn't know how to do that (or even that I needed to). Now that I've got that settled, though, things are working much better. In my Obj-C file, I can move an object to any location I want.

I find that if I move it away from the camera, it gets a lot dimmer, however. I think this is a trick of the lights in blender - perhaps if I create a "sun" light, it will work better. That's what I am going to try next. Also, I could stand to shrink the original objects a bit!

So I'm making progress, but it is largely behind-the-scenes stuff - not much to look at. But still, it is pretty important. Hopefully, I'll have cool stuff to show soon.

Thursday, April 22, 2010

Graphics on the iPad (simulator)

Hooray! I've got a screenshot here of the various shapes showing up on the iPad Simulator:
Yeah, they're a little dim right now, but they should do the job nicely. I'm going to clone them and fill a grid with these guys!

The next thing I need to do is to really get my mind around how to use these objects as object-oriented graphics. All of these graphics live in a common "_SIO2resource" variable - sort of a set of 3D objects to draw. That's where the shapes live. They have "transformations" associated with each one - they determine the location, size and orientation of the object, but that information lives with the graphics in the _SIO2resource. But I want to be able to have individual objects - instances of custom classes - that have variables and methods beyond those involving their graphical layout. How to reconcile these two schema?

I think that what I will do when I create a new piece is to first duplicate the graphic from the master graphic, which both installs the graphic in _SIO2resource and gives me a variable that I can use to directly refer to the graphic. I'll initialize an instance of a corresponding class and pass the graphic along in the constructor (or a modifier method). That way, both the _SIO2resource and the graphic can both access the transformations for the graphic - the instance object can modify them, and on the next graphical update, the _SIOresource will read the transformations and use them to position, resize and orient the graphics. It'll be great. (If it works.)

hoppin' around

I've got Q*bert moving around now, which was a fun little challenge. For a while, I was expecting to have to do some complicated collision detection- search through all the cubes, detect which one(s) Q*bert is touching on screen, and narrowing them down to see which ones might be involved in a collision, and which ones Q*bert is passing "in front of."

Then I realized a few things.

  1. I only need to worry about Q*bert when he is moving downwards on the screen - when his yVelocity is positive.
  2. If Q*bert "knows" which cube he is jumping onto, then I only need to manage collisions with that particular cube - in fact, all other cubes are problematic distractions.
  3. As far as the collision with that cube, all I need to do is figure out when Q*bert's feet have arrived at a certain height on the top face of the cube.
  4. ... and all this will work on most of the other critters on the screen.
To make Q*bert jump, then, I had to give him an initial x- and y- velocity (depending on which way he was moving); set a variable, IAmMoving, to True; and let him know which Cube he was trying to land on. It took a little bit of tinkering with the starting x- and y-velocities and the downwards acceleration to find values that looked right and got him to land on the right spot.

To make him stop, I checked periodically (while he was moving) to see whether he was moving down, and whether his feet were below the middle of the top face of the cube. If so, I set IAmMoving to False. (I also corrected his position to the center of that face, so there isn't any drift over time.)

So now Q*bert (or his rectangle, at least) is hopping around.

Shapes and pads

File under: sio2 and implosion
Just some thoughts about what I've been working on the last few evenings. (I have been advised not to work on my private project during school hours, and I intend to honor that.) I've managed to accomplish two things:
  1. I think I have created a working SIO2 project template that works on the iPad. That's important, because my implosion game is a bit too detailed for the iPod/iPhone screen, IMHO. I took the template provided by the folks at SIO2 and converted the .xcode project and the .xib file to iPad versions.
  2. I've gotten more practice with blender and have created a set of five shapes: a ring, a box, a triangle, a cross, and an X. Ok. The X is just the cross, rotated. They all look good in blender, but for some reason, my app seems to be pulling from an earlier version of object data, so my changes aren't showing up. I suspect that the file it is pulling from is not the one I think it is! A project for this evening, no doubt.

Wednesday, April 21, 2010

It's Alive!!!

I've added Q*bert to the screen - isn't he darling?
(O.k., So far, he's an orange box.) You may also notice that I have shrunk the boxes vertically, so they look more regular, and I can fit more on the screen - the "regulation" seven rows....

Tuesday, April 20, 2010

Happy Cubes

Ok. So it didn't take too long to make a Cube class, and I'm pretty happy with it. It includes a list of colors that the cubes' tops can be and a variable that indicates which of those colors is there. The Cubes look pretty good (more after the jump):


(Looking at this picture, I wonder whether the cubes are too tall - comments welcome.)

Cube Thinking

So I drew a picture of some cubes, and I added some numbers to them in a somewhat systematic way.
(The first number is which row, and the second number is the location within the row. Since I'm a computer guy, everything starts at zero.)




And I notice a few things: (more after the jump)

Monday, April 19, 2010

Python Project

I think I will work on a project to remake the game Q*bert while my students work on their games. (Here's a clearer explanation of the rules.)There are some things about it that are sort of simple, and others that should make a nice challenge.

One of the tricks will be maintaining a grid system, so that the character is always moving at a diagonal. For instance, the top row only has one square, the second row has two, the third row has three, etc. If Q*bert is on the left square in the second row, he can move left or right down to the third row, or up and to the right to the top row. He can't move up and to the left, because that is off the world, and he can't move to the right-hand cell in his row, because he only moves diagonally. So how will I be able to keep track of where Q*bert is, and where he is allowed to go?

I think I will need classes for Q*bert, the cubes, a scoreboard, one for each type of monster, the balls (generic) and the platforms.

I'll need a list of objects to draw, objects to animate, enemies, and platforms

Here are my goals:

  1. Generate the cubes
  2. Create Q*Bert and have him appear
  3. Let the user control Q*bert around the cubes
  4. Let Q*Bert change the color of the cubes
  5. Create a scoreboard
  6. Make the balls fall down
  7. Make Q*bert die (curse!) if he is hit by a purple or red ball.
  8. Make Coily and move him randomly
  9. Make Coily move towards Q*bert
  10. Let Q*Bert land on/ ride a platform
  11. Let Coily fall off if Q*bert is on a platform
  12. Create Slick & Sam
  13. Create Ugg & Wrong Way
  14. Add Sounds & Multi-levels

Sunday, April 18, 2010

Aha!

Ok, I found it. (How to dynamically clone objects in SIO2...) I'll post it here, in case I need it again. (Dear reader, don't worry about these details... just know that it works.)
In the code for SIO2's Tutorial061, in template.mm, lines 310-328 show how to do it. Specifically, provided that _SIO2Parent is not a pseudo instance, (I.e., it doesn't end with ".001"), then you can say

_SIO2Duplicate = sio2ObjectSoftCopy (_SIO2Parent, name)

...where "name" is the new name of the object, and "_SIO2Duplicate" is the variable that receives the object.

Cool. This might be doable after all.

Sio2 revelations

The more I see of SIO2, the more I understand what all the fuss is about. You really can set up a lot of aspects in blender not just the appearance, but the mass, solidity, friction, coefficient of restitution, etc. The tutorial I just watched showed the trainer creating things in blender floating in the air. He exported the file and moved to Xcode. Immediately, the objects fell down and bounced around on the contoured surface he had created. Very cool.

Still, I have to wonder whether I can clone the objects I create in Blender on the fly or not. If so, I haven't seen how to do it yet. If not, I suppose I could create all the objects i could possible need at one time and keep them off screen (behind the camera). But that would be a pain, and I wonder whether all those extraneous objects would slow the system down.
I will keep playing around with it.....

Getting started

This is the first post in a blog I am writing about the programming projects I am doing. My first goal right now is to port the "Implosion" game I wrote a few years ago in Flash over to the iPad. That's it, just a simple port, right?

Well, it's never that simple.

One of the things I am working on right now is learning OpenGL. Out of curiosity, I just went to Amazon and searched for "OpenGL for Dummies." This is what I got:

But not to worry. I found a good online tutorial by Jeff LeMarche about getting started with OpenGL for the iphone. I followed this for a while, but I eventually found a book (at an actual Borders store!) about a resource called SIO2, which works with Blender, and that is what I am pursuing at the moment.

When it works, it works quite well. The problem is, when it doesn't work, I am having a hard time telling why. I just get a black screen on the ipod simulator, which is disappointing and not too helpful. Just yesterday, though, I got a custom shape to appear on the screen, so I am feeling better about things!

Still, Blender has a large learning curve and I am just now getting started with SIO2. So while I am excited about my progress so far, I feel like there is a pretty large disconnect with the Implosion project. I may try to parallel-develop using the Quartz drawing tools, which I understand better, and then upgrade to OpenGL (which looks better) when I can.