Archive for the ‘game’ Category

Bresenham’s line algorithm is a fairly simple algorithm that takes two tile positions in a grid and returns a list of all tile positions that a line drawn between the two positions touches. It is well described along with easy-to-read pseudo code here.

When is this useful? According to what I’ve read, this algorithm and variations of it are useful in graphics programs. I’m sure you’ve seen programs that let you draw icons in a zoomed-in view. While you draw lines it just colors in boxes. It is likely this algorithm was used for that. However, I use this algorithm for game-related purposes. The last time I used it was probably 5 or 6 years ago in a Flash pool game. I set up a grid with an arbitrary tile size on the pool table (that the user can’t see). Every frame I determined in memory which tiles a ball covered as it moved (assuming no collisions). Then I would only run collision detection against balls that touched the same tiles. This let me cut down on the number of collision checks per frame.

I’m coming back to this algorithm again now because of a first person shooter game I’m working on. I hope to create a blog post on how the algorithm is used in that game (on the server) to keep the collision detection calculations to a minimum.

I created a Flash demo of Bresenham’s line algorithm that you can play with by clicking on the image. Or you can download the source files here.

click to view example (drag dots around)


Read Full Post »

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 🙂

Read Full Post »

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!

Read Full Post »

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 🙂
Virtual world
[image missing]
Real-time tank game
Cooperative multiplayer game

Read Full Post »

Predictive Shooting

It has been quite a while since my last blog post. I have a lot of useful ideas that would make great posts but some super cool Electrotank projects have been sucking up my life.

One of our smaller projects is a tower defense game due out soon. For this game we developed a way to precisely answer this question: If a tower wants to fire off a bullet right now and hit a specific moving target, at what angle should it shoot?

The end result is a function that can find that angle for you. If you use it then you’ll get a direct hit every time! If that makes your AI too perfect, then tack on some random variation to the angle and it will be less precise the further away it is from the target.

The known information are these things:

  • Current position of the target
  • The target’s velocity (acceleration not supported here)
  • Position of the tower
  • Shot speed
  • Length of tower muzzle, if any

The equations of motion for the target are:

x1 = x1o + v1*cos(theta)*t
y1 = y1o + v1*sin(theta)*t

  • x1o – Current x position of target
  • y1o – Current y position of target
  • v1 – Speed of target
  • theta – Angular direction of target
  • t – Time variable

The equations of motion for the bullet are:

x2 = (x2o+n*cos(alpha)) + v2*cos(alpha)*t
y2 = (y20+n*sin(alpha)) + v2*sin(alpha)*t

  • y1o – Y position of tower
  • x2o – X position of tower
  • v2 – Bullet speed
  • alpha – The unknown angle to shoot
  • n – The length of the muzzle
  • t – Time variable

You can download the source code which contains the solution here:
Download source

click to view example

Read Full Post »

As some of you may know, my company Electrotank sells a very popular multiplayer server called ElectroServer. Mattel has been using ElectroServer for a year or two now to power their Barbie Chat and Rebellion Racer, a real-time racing game.

Today they released a new fun and addictive multiplayer card game called Finish Line Card Race (programmed by Alan Donnelly of Mattel). Players control their cars by playing cards to try to get to the finish line first. The terrain can change based on a card that is played, and cards played move your car based on the current terrain and the value of the card. It is a simple and fun game.

It works on the Wii too! You can read more about it here.

Check it out!

Read Full Post »

Wow. It has been over a month since my last post. Things have been pretty crazy at Electrotank with no signs of letting up. We’ve created 5 new game titles in the last month alone and more than that scheduled this month.

In this post I thought I’d just talk about something we’ve been working on for much of the year. It is an on-going VH1 project for a companion site to the popular TV show World Series of Pop Culture. The site is http://www.worldofpop.com/.

So far we’ve created 7 trivia-based games for this site. One of them is called Trivia Dome and was tied to a big on-air push during the latest season of the World Series of Pop Culture. Trivia Dome is a multiplayer game. People queue up and chat while they wait for a game that starts at a specific time once a day. When the game starts everyone gets a trivia question. If you answer correctly you move on. As soon as you get one wrong, you’re out. On one day we had more than 12,000 simultaneous people play!

We’re creating a few more games for this site both single and multiplayer.

Anyway, no source files this time 🙂

Read Full Post »

Older Posts »