Monday, April 4, 2011

Fun with Turrets

If you'd look over yonder ==> at the Big List for Paper Zeppelin, you'll notice that the Turrets aren't all orange yet, mostly because they aren't done. Oh they work all right. They shoot bullets at the player just like the Flying Fortress type enemies do and they are hard-ish to kill on account of their ground based orientation. So one may be tempted to consider the thing all done and just go ahead and add a plash of color. I am not one of those, um, ones.
Instead I'm driving myself to distraction because I can't quite yet figure out the math involved to make the turret aim. Visualize for a moment a cannon firing. The most basic of things that it does (other than the loudness and the explosion) is be pointed at something. A cannon isn't just pointed at something and then the ball goes wherever the heck it thinks looks nice this time of year. The direction the cannon is pointed has specific implications on the resulting trajectory of said flaming ball.
But now, the Turrets in PZ do not do that. At best I've gotten them to point in vaguely the direction, but it still looks awful. From a design standpoint it would be nice to have a visual way to let the player know where a shot may be coming from. Granted most shooters of this type usually forgo the whole thing. Consider a game like 1943, the big plane enemy bosses just have guns and bullets spray out. There is no visual and it works out okay. The difference is that PZ is designed from the get go as multiplayer. The distinction is where an enemy is shooting is important to hopefully help make sense of the chaos. Similarly the Fighter Enemies will probably spawn in different colors so the players can know who they are after.
The problem with all of this comes from a coding standpoint. Going back to the post a while ago involving trigonometry, I know that when a bullet fires it has a certain, measurable, change in its X and its Y. This in turn is used to calculate the trajectory of the bullet. The odd thing is that the rotate function of C# uses something called a Radian. Mathematically speaking (oh no, here we go again) a Radian is the amount of distance along the outside of a circle equal to the Radius of that circle.
I'll explain. Let's say we are on a floating ball in space. We know that the whole ball is exactly 20 steps across. So if we drew a circle on the ground (of a different surface) that is the same size as the ball we could walk across in 20 steps. So, if we all remember 7th grade math again, that means that the Radius, or distance to the center is 10 steps.
If we go back to the ball in space again, a Radian would be if we walked 10 steps along the surface of the ball. Since the Circumference is 2RTT squared, we know that it takes 2TT Radians or 2 * 3.14 * 10 steps to walk all the way around our space ball.
So yeah, it's kind of a funny way to measure. Especially so after all the Astronomy lessons involving angles and arc minutes and seconds.
But I digress. I suppose that since I know the distances it's really just another way to draw a triangle. So I could use the Sine of the Triangle and...oh noes, more explanation incoming.
Assuming that we have all forgotten what I am talking about, a right triangle has specific names for the different sides and the angles that live inside of them. We're dealing only with right triangles (the kind with a 90 degree corner) because everything in the game is measured in X and Y, and the changes in those numbers. It can be graphed pretty well coincidentally. Anyway, the Sine is the internal angle of the triangle that is next to the long side. Since we were dealing with bullets, that would be the side of the triangle that the bullet would travel along. I can calculate that angle with this bit of math :

sin A = Opposite / Hypotenuse

Or to convert that into something that looks like it belongs in Paper Zeppelin

Shot Angle = Y Trajectory / Shot Length

So let's plug in some numbers now. Say the shot is 3 high and the length is 4.

Shot Angle = 3/4 = 13 degrees or .68 Radians.

Thankfully, after all of that it turns out that C# does all of its angle math using Radians, so I probably don't ever have a convert a damn thing, which would be nice.

- Now then, how do we apply of this back to the bloody titles? I'm thinking that maybe the turret that is tracking a player will always do that angle math. Currently the system only does those calculations when it's thinking that it may want to shoot. If I did the math that calculates the shot trajectory I can use the results to find the angle that the Turret should be pointed in. I then just change the rotation value of the turret to match. I feel an experiment coming on.

..and the results are in. The math works, but the code isn't being nearly so cooperative. For some reason the Turrets just sort of wiggle a little when you get close. It's like they're all excited or something, or maybe cold. I like to think that they're thinking, "Oh, oh I'm gonna get to shoot now. Here it goes! BOOMIES!" Of course I know better, but I now I can think I've given the little bastards personality while I try to figure this out.

-I've noticed a distinct increase in the amount of math involved with Paper Zeppelin, especially compared with The Thief's Tale. I've come to realize that the gameplay of a shooter involved way more geometry than the pretty quick and loose gameplay of a platformer. I mean if the math is just a little off in a platform game it's not all too bad. Things in it seem to be more about calculating certain numbers after a certain time, like the arc of a jump. These things of course work just as well when the math is wrong. It's not like the whole thing falls apart or sucks. You can tweak the numbers until it fits. On the other hand a shooter is almost entirely math and geometry. If the math isn't working there, then the game is just broken and stupid. I think that I'm past the part where I get to constantly explain mathematics. The stuff left on the List seems pretty basic, but then I'm sure I'll surprise myself.

No comments: