Wednesday, May 2, 2012

Game Design Log - Entry #9

AHAHAHAHAHA! SUCCESS!

I took a break from Thrust calculations for the last day or two, and skipped ahead a bit to actually adjusting the trajectory with the results from the thrust calculations... Kinda backwards, but I wasn't getting anywhere with the thrust stuff.

After spending a day and a half going in circles trying to figure out what I was doing wrong(Believe it or not, all the references I could find online were wrong in some way...), I finally asked a friend, and we found two problems. One, I had the wrong operation in one place(I needed ArcTan/Atan/Tan-1, and I had Tan), and two, I was putting it into my calculator wrong.

In my LanderMotion structure, which contains just about everything relating to the motion of my Lander(How about that name, eh?), I'm storing things as polar coordinates. I didn't know that they were polar coordinates when I was setting up my structure, but now I do and that's what they are. Now, I had never worked with polar coordinates before, just cartesian. Polar coordinates, to me, seem better suited for things like this, as you're applying force in specific directions and amounts, which is exactly what polar coordinates are, direction and magnitude.

The fun part came when I got to adding multiples of these forces together. There's three forces acting on the course of the lander -- momentum, gravity, and the engine. Gravity is always in the same direction, down, and for this game will stay constant through one "game". The engine is variable -- You can change your rotation, and the throttle of the engine. Momentum is variable -- It's the result of your previous motion, and is also what the result of your trajectory calculation is calculating.

Now, to get the net result of these forces, you simply add them together. "Simply". Anyways, to add these vectors together, you have to convert each one to cartesian coordinates, add all the cartesian coordinates together, and then convert it back to polar coordinates. Converting polar coordinates to rectangular coordinates is easy, and I had actually already been doing it in my ballistic trajectory stuff(Which I just realized is horrible misnamed in my game as it stands... It's not gravity at all...). Let's walk through it real quick.

For a given vector, there's two components to the polar coordinates for it, the radius and the polar angle. The vector we're going to work with has a radius of 15 and a polar angle of 180. To convert this to Cartesian coordinates takes two calculations, one for each the X and Y axis.
To calculate the X axis, take the cosine of your polar angle, and multiply it by the radius.
To calculate the Y axis, take the sine of your polar angle, and multiple it by the radius.

Easy, no? The part I had problems with was going the other way, cartesian to polar. This is more difficult, and the best way that I can think of to show it is to link to the code that I ended up with.

See here, starting at line 69.

Rtheta is the polar angle, Rmag is the radius, Rx is the cartesian X coordinate of the rectangular coordinates you're converting, and Ry is the cartesian Y coordinate.

Quite a bit more complicated, no? After I got help and figured out what was going wrong with my calculations, this got cranked out in less than half an hour, but I spent just about two days going 'round in circles on the 'net.


So things are moving along. Next up's back to thrust calculations. Also, I should have mentioned before now... My code's being put in a Git repository, accessible at http://code.meldaire.net/lunarlander. Not much to look at yet, but it's very much a work in progress.

No comments:

Post a Comment