Sunday, April 12, 2015

Screw Tarzan

This week: finished implementing the grappling hook, kind of. Completed a tutorial on the basics of procedural generation (the link will probably just prompt you to buy the tutorial, unfortunately, although I'd definitely recommend it to anyone interested in developing games in Unity).

The swinging mechanic, briefly: I corrected my naive implementation (detailed in this post) with the help of the Sparknotes SAT Physics prep guide. In particular, I found this helpful diagram:

It suggests that the net force acting on the bob of a swinging pendulum is equal to the sum of the tension force (FT) and the force of gravity (mg, or mass times gravity). The force of gravity can be split into an x- and a y-component, mg sin θ and mg cos θ, respectively. The force of tension is always equal to the y-component of gravity, to maintain equilibrium along the curve of the pendulum swing. That means that the only force acting on the pendulum bob (or a swinging character!) is the x-component of the gravitational force, mg sin θ. I don't track the mass of my character controller, so I just assign it a value of 1, leaving me with a force equation of g sin θ.

Great! I can calculate the angle θ in 3D space by finding the angle between a vector pointing from the character to the tether point and a vector pointing straight down from the tether point, and pass the character controller a movement vector of g sin θ. That should simulate swinging on a grappling hook perfectly, right?

Well, no. In fact, there's a pretty big problem, which I'm sure those of you more physics-literate than me have already spotted. I'm calculating the force vector, which changes the acceleration of the pendulum. If I treat that as a velocity, as I did when I passed it to the character controller's movement vector, then I've violated Newton's First Law. In fact, the force vector approaches zero as the swinging character gets closer to the point directly under the tether point, then actually becomes negative as gravity pulls the character back towards equilibrium. This meant that I had a beautiful, extremely realistic half an animation. The character moved in the correct curve, sure, but he (or she!) came to a dead stop halfway through the swing.

The solution? I added the previous movement vector to the force vector in each frame. This immediately presented another problem, as the force vector reversed its direction after passing the mid-point of the swing, leading to a constant downward motion rather than a swing. So I wrote up some code that switched the direction of the swing force vector after it had passed the halfway point.

Then I had to fix the relative magnitudes of the two vectors to achieve a realistic-feeling swing speed, then I had to vary that speed according to the character's position along the pendulum curve, then I had to dynamically modify the length of the tether to reflect a nicer-feeling elastic rope...

Basically: it's a lot more complicated to fake physics than it would have been to just simulate the physics in the first place. In the end, though, I have a lot more control over the grappling mechanics than I would have, and I'm pretty happy with the result. It needs a few more tune-ups, but I got the hardest part taken care of.

Whew! I'm impressed you made it this far! I don't blame you if you ended up skimming most of that stuff. It gave me a headache, too. Let's look at some pretty pictures!

 



Disclaimer: I did not draw these (one is even from another video game!). But they do have a really nice atmosphere, don't you think? I've been studying pictures of cyberpunk cities, regular cities, maps, floor plans... anything to help me begin to generate my own versions. That's my current focus. I'm hoping to have an algorithm that generates buildings in a month or so, and hopefully one that put those buildings together into a city. That's a pretty ambitious goal, and I know I may have to accept something less than what I'm setting out to do. But I know that it is possible (thanks to some academic papers and a sadly unsuccessful video game) and I've already started learning the techniques to help me get there. More details on that in the next post.

I really should plan out a development schedule soon - or at least start tracking what things take the most time to implement. I'll try to get some numbers together.
I seem to have used all my best material for links about this stuff in the actual blog post. Let's see - how about an excerpt from the final Game of Thrones novel?? DON'T READ THIS IF YOU HAVEN'T READ THE BOOKS.


No comments:

Post a Comment