FINALLY!
See what happens when I focus and actually work? I finally get orbits to work about the screen center (or anywhere else I might like for that matter). It was a case of simple vector math. Even though I swear I tried this before, I'm guessing I just didn't get the vector subtraction quite right. The proper solution turned out to be as follows:
static void
planetGravityVelocityFunc(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
{
// Gravitational acceleration is proportional to the inverse square of
// distance, and directed toward the origin. The central planet is assumed
// to be massive enough that it affects the satellites but not vice versa.
// Sun position cpVect disp = cpv(160, 240);
// Point mass position cpVect p = body->p;
// Point mass relative to Sun p = cpvsub(p, disp);
cpFloat sqdist = cpvlengthsq(p); cpVect g = cpvmult(p, -gravityStrength / (sqdist * cpfsqrt(sqdist)));
cpBodyUpdateVelocity(body, g, damping, dt); }
So now that I finally have the mechanics working, I want to test collisions and how, exactly, I can manipulate the mechanics to create collisions. The collision detection itself however should be simple as it's built into Chipmunk.
Some of the "weapons" I originally imagined would require the velocity function of affected planets to either be reset or added to in complex ways so I may have to cut those. I might reduce the scope down to simply orbit changing affects such as slowing/speeding a planet and then using the result planetary shrapnel (a destroyed planet could be removed from the scene and replaced with a small number of smaller bodies set with velocities related to origin body). In this way, I might just make the game even more "Osmosis" like but with the ability to apply forces to any body in the system and use the resulting explosions to cause chain reactions. Perhaps a few one-off weapons, such as the "Rubble Maker" which simply detonates planets instantly and possibly "Anti-Gravity Beam" that breaks a planet from the star's gravitational pull and sends it off in straight line to collide with a neighbor when timed correctly. Each planet, depending on size, would have a different amount of health determining how many micro-meteorite hits it could take. The problem is if I do want more interesting planetary orbits for later levels, I'll have to craft them carefully to ensure no self-destruciton.
The next big hurdle is figuring out how to set up my game to build multiple levels/puzzles. While I still need to research example projects, I'm assuming a new scene can be built based on a specific input when an old scene/level is completed. E.g. I'll have a level builder class which will loop through an array of planetoids and pop them into a scene with corresponding sprites, velocities, masses, etc. along with the target number of moves for the solution.
The last hurdle for game play itself will be navigation. Scaling the scene up and down to allow players to see the greater picture is obviously demanded since the screen is so oblong. However, I think I'll make it so while the scale between planets will change, the planets themselves will not, to a point, so that they can be seen easily. If Chipmunk supports a pinch gesture then that's definitely the way to go, otherwise a slider will have to do. And while I originally wanted to allow some time step control, this may prove difficult. I could easily lower the amount of computations per frame but increasing would negatively effect performance. Will have to bench test that later if possible.
Furthermore, it will be important to somehow represent the path of the planets. Computing and drawing an appropriate shape would be impossibly complex since it would require either precomputing future time steps or estimating an ellipse based on the current position relative to the center and velocity vector. If I can find an easy way to do the latter, that would be optimal. However a easier short term solution is simply to draw a point at a regular interval, leaving a trail behind the planetoid that will fade over time.
Finally I'll need to implement a basic menu with a level selection and high score board. While I've procrastinated terribly up until now, I still have a month to work. I'd like to be finished with game play by the 15th at the latest so I'll have another half month to test and tweak. I've nothing else going at the moment and plan on finishing my independent study once I'm reunited with my PC in NYC so I'm confident I'm capable of this. I'm sure some things, like the art and sound, may not be as polished as I want them to be come end time but I'm sure I'll have, at the very least, a playable game. Wish me luck!
PS: The guy that told me at Alphas that he could get me an iPhone developer login credential was on the TagTanium team of PennApps (pretty sure though it might have been TagAlong). If anyone knows anyone from that team, any contact info would be greatly appreciated.
Long Haul
So I worked pretty hard all yesterday and today and I made some good progress for the alpha tomorrow. Having given up on UIKit, I dug into Cocos2D and it was great and easy. Took a little while to figure out how exactly to then tie in Chipmunk, the physics engine, but it was exhilarating when I finally got that damn ball to fall down due to gravity!
However I ran into some major problems, mainly due to lack of any good documentation on both Cocos and Chipmunk but especially the latter (most references I found were for Box2D which is also supported but seems less apt for what I want to do). It's very easy to set a global gravity in Chipmunk but setting gravitational pull independently for multiple objects is a bit more advanced.
To do this I need to create my own "Velocity Function" which the software than uses to do some Euler integration and get the result. However the documentation on this function is small at best. The function is described in the docs as follows:
void cpBodyUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
Default rigid body velocity integration function. Updates the velocity of the body using Euler integration.
From looking at the function I derived that it does essentially the following:
v*damping(gravity+f/m)dt
where v is the objects current velocity, f is the current force on the object and m is its mass. Now calculating the new gravity is easy enough yet when I use this functino nothing seems to happen. I need to play around with it a bit more. It's possible that it is working and, due to how I'm setting my body masses or initial speeds, it just appears as if nothing's moving.
Anyways excited for the Alpha Presentation today. Little worried about how it'll work out with a video, as opposed to slides. I just gave each slide equal time so hopefully if I run out of time on one slide I can just make up for it later where I have nothing to talk about with a nice segue. My slides are pretty so that's all that really matters, right? Let's hope I can stay awake through it all after pulling an all nighter. But I've been sleeping pretty well recently so I'm not too worried.
OMG Objective-C is the WORST!
So I tried following the UIKit tutorial in my book I bought and the initial step wouldn't even work! I resorted to copying code wholesale from the source code they provide on their site and still nothing. I'm getting a handle for Objective-C now but for some reason the simulator wouldn't even get to my actual code. It just got stuck in the mach_msg loop. I didn't see any differences between my code and theirs. There must be some connection to the main game loop I'm missing but for now I'm abandoning even doing this UIKit tutorial and will begin a Cocos2D one instead which is what I intended on using ultimately anyways. Below see my awesome, motionless, scrapped demo!
Development Options
So thanks to a quick chat with Karl, a friend and developer I worked with at MindSnacks over the summer, I learned about some great iPhone development resources that definitely start to lean development favor away from Unity. Here's a list and my quick first impressions of each:
- ObjectAL for iPhone : Karl's own audio development tool for the iPhone. Sounds like a great tool but sound is way down on my checklist for development.
- cocs2d for iPhone : Great 2D development tool, perfect for my project. This is definitely the way to go for my project. Karl highly recommended it and it was used in our Mindsnacks game. It included basic workflow and spirte management and should make my initial development a bit easier.
- Chipmunk : 2D physics on the iPhone! Perfect for my game. Can't wait to start implementing and see if it will be able to manage all the gravitational and collision effects for my app.
- iCode Blog : For everything else I found iCode Blog. A series of posts outline how to create all sorts of menus, tables, etc for the iPhone.
I'm hoping to have the first three tools implemented in the next few weeks and running a basic version of my idea. My design document and wireframes should be done soon, followed shortly by my mockup (just to make a bit of light at the end of the tunnel). Hoping to post the first two tomorrow. Wish me luck...lots of work to do!
