Ramblings of a Flash game programmer

After attending FITC a couple of times now I’m really excited to not only attend, but to be a part of FITC San Fancisco! They’ve lined up an incredible group of speakers including Colin Moock, Yugo Nakamura, Jared Ficklin, Erik Natzke, Kyle Cooper, Jeremy Clark from WIRED Mag, Kevin Lynch, and Ben Fry.

My presentation is Latency Hiding in Multiplayer Games. Latency is something that cannot be avoided, and how you handle it as a developer is the difference between a fun and responsive user experience or a frustrating and jerky experience. I’ll discuss the issues that latency in multiplayer games brings up, and look at various ways to handle it. Bring your laptop since we’ll all be able to jump into a few different games and battle right in the session!

Early bird price for this event ends Friday July 2nd, so get your tickets NOW and use the code ‘electrotank’ for an extra 10% off!

Adobe invited me to participate in the prerelease programs for Flash CS5 and AIR for Android. I took some time and built a game for the iPhone called Fruit Smash Organic which is currently in the iTunes app store. While I love my iPhone, Apple sure does make you jump through some ridiculous hoops to get an app on it. Even if it is your own testing device! The biggest frustration I had was with just getting my game onto the device.

With the latest crApple news it was time to get my hands on an Android device. My Nexus One arrived yesterday (FedEx Saturday delivery FTW) about 4pm, and I had Fruit Smash Organic ported to the device and running beautifully by 7pm! That includes downloading and installing the SDKs, walking through a Hello World tutorial, charging the device for a little while, and making some minor code edits. Kudos to Adobe and Google for making the process easy instead of a nightmare.

Also, the game performance is great! It doesn’t even think about dropping below full frame rate, and easily out performs the same app running on the iPhone. I look forward to creating more games for Android using Flash!

Short 45 second video showing primary game play.

A slightly longer video showing primary game play and some secondary elements. And you can see the reflection of my and my flipcam 🙂

I recently spent a little time recreating some older spring physics code, in AS3. It is the sort of thing you’ve probably seen 100 times over the years, but working on stuff like this never gets old to me. I’m sure I’ll recode it again in a few years 🙂

In this post you’ll see a quick demo of the spring behavior, a link to the source, and a little bit of explanation of physics.

click to view example

A little explanation
Spring behavior can be coded by first understanding Hooke’s Law. Imagine that you had a spring and a mass attached to the end of that spring. Hooke’s Law specifies the force that the spring applies to the mass when displaced from it’s rest position.

F = -k*x

Where x is the amount of displacement from the rest position, k is the spring-specific constant (spring “tightness”), and F is the force exerted by the spring on the mass. The force tries to get the mass to return to the rest position, so it is in that direction.

In my code I use the Vector3D class to generalized the equation from just x to x and y. Also, I connect two equal masses with a spring rather than just having a spring with a single mass. Two masses and a single spring obey the same equation. It is just that the two masses have the force applied in opposite directions.

F = -k*displacement

Where displacement is the distance between the two masses minus their distance at rest. If F is applied to one of the masses, -F is applied to the other one. The minus sign can be manually applied to wrapped into the displacement if calculated relatively.

A single mass can be connected to N springs. To achieve the correct resultant behavior the forces acting on each mass need to be summed each iteration and then used to affect the velocity.

What is missing?
I am not properly letting the system lose energy. To properly lose energy the springs should apply a resistive force against the direction of movement of the mass in the amount b*velocity, where b is a spring-specific constant. Currently I’m just slowing all things down by a factor of ~.95 every iteration…sort of like if the whole system was in oil.

Also, I’m currently assuming all masses are equal. This could be easily enhanced to support varying masses.

What is it good for?
This type of behavior comes up from time to time in games. Off the top of my head I remember using this behavior to simulate how a car shakes due to breaking quickly (from side view). The game was Pimp My Ride: Crowd Magnet. I think I created a rectangle with 4 masses and 6 springs. The springs connected the perimeter and the two diagonals. I hid the visibility of this frame but had it move on screen along with the car. I used the two top nodes to tell how to move/rotate the frame of the car as it stopped.

There was also a non-game UI that I created years ago for a client similar to what you see at Visual Thesaurus. Nodes were connected via springs, but then the nodes themselves also had a gravitational pull toward each other and a slight magnetic repulsion. It creates an interesting interface!

You could use a spring system to mimic a blob or cell. Turn off the lines and nodes and fill it with a color and allow it to bounce off of things. Or make a chain/rope that your character can swing from. Uses for this pop up from time to time!

If you use sockets in Flash for games, virtual worlds, or streaming video you can run into a nasty problem in IE7. The open socket will not close until the entire browser is closed. Closing a tab or refreshing the page won’t do it. Luckily IE8 has been out for a while and fixes this. (Though, according to w3schools ~16% users are using IE7 and ~14% are using IE6 at the time of this post.)

The behavior that we had to deal with in our multiplayer applications is what would be viewed as ‘stuck users’. Someone comes to a game and logs in with their account information. The socket server knows that the user is logged in and allows him to chat and play games. The user refreshes the page for whatever reason, or browses away and comes back, and the socket doesn’t die. When the user comes back and tries to login via Flash it fails because the server believes the users is already logged in.

We worked around it by having the server force close the first socket if the same user tried to login a 2nd time.

In a current Electrotank project we are making heavy use of hitTestObject and hitTestPoint. Everything we were doing worked great. Then we went to build an editor using the Flex Framework and realized that some of our hitTestPoint checks were inexplicably failing.

After a decent bit of trial and error and hunches we realized it was that the UI components are using scrollRect. I then tested various conditions of scrollRect with hitTest to see how it behaves. It turns out that it always plays nice except with hitTestPoint with the shape flag property set to true and only when the point lies beyond the scrollRect boundaries!

Maybe this is common knowledge. But it is the first time I’ve ever run across it and googling didn’t easily turn up information on the topic.

The following two beautiful artistic renderings show the results of hitTest when performed with an object, point without shape flag, and point with shape flag. It shows them in the +x and +y quadrant, and the +x and -y quadrant. It then repeats with a scrollRect applied.

The code used to determine the collision reaction of a ball bouncing off of a line shown in the previous post is the same as the code required to do it in 3D! So, you can achieve a ball bouncing off of a plane. Or, when you add gravity plus a little damping on the floor you can have things rolling around.

This example was designed so the ball would hopefully roll for a long time without rolling off. But if the ball was going a little faster you’d see how it can bounce into the air a bit if it hits an incline fast. Anyway, just thought I’d share!

(marble madness anyone?)

Note: if it seems a little slow, it is probably the 5 other blogs on this page that are running more intense things.

I recently finished writing ActionScript for Multiplayer Games and Virtual Worlds, my first book since Flash 8! With the help of some amazing contributors I think this is the most useful and geeky-fun book I’ve ever been a part of. According to Amazon (shameless link) it hits shelves in 8/3/2009.

So what’s it about? If the title of the book doesn’t give it away, it is a book that focuses on multiplayer concepts and applies them to games and virtual worlds via ActionScript. Some of the coolest topics are those on dead reckoning, real-time movement and time synchronization, loads of information on avatar rendering, and other useful topics like thorough coverage of isometric concepts.

While there are many examples throughout the book, there are 3 major examples. In one chapter we build a real-time multiplayer tank game (top down). In another chapter a cooperative multiplayer games is created, and its a fun one! Then over the course of several chapters we dig into various major topics on virtual worlds to build a complete world.

Here are some screenshots, and I hope some of you check it out and find it useful 🙂