This blog has moved. Visit Groundswell Games for the latest. Remember to update your bookmarks and RSS feeds.

Monday, September 24, 2007

My camera, vanquished

After three days of wrangling with my code and muttering to myself, I have managed to get my camera system to a place where it mostly behaves. Having accomplished this, I feel a new sense of admiration for games with cameras that gracefully avoid passing through floors and walls. It's still possible to make it pop through a wall occasionally or to find just the right spot where it will completely freak out, but it'll do for now.

Here's the gist of how it works:
  • Just before every frame is drawn on screen, the camera casts a series of rays to detect objects around it. The primary ray starts at the camera's target (the player) and casts back toward the camera, ending a short distance behind the camera. A group of secondary rays are also cast pointing outward from the camera itself, like spines sticking out in several directions.
  • In normal circumstances (when no offending objects are near the camera), the camera can be moved around by clicking and dragging the left mouse button.
  • If an object interrupts the primary ray (i.e., passes between the camera and its target), the camera jumps to a point in front of the object. Once the object is clear, the camera slides back to its preferred distance from the player.
  • If the camera detects an object below it (like the ground), it will begin sliding toward its target. In this mode, the camera's distance to the target is proportional to its own distance from the ground.
  • If an object gets too close on the left or right side of the camera, the camera will slide along this object toward the target until it slides clear and can move along unobstructed.
So why was I having so much trouble? My camera didn't have enough feelers sticking out of it. My initial setup only cast rays backward, left, right, and down. With so few, it was easy to make the camera think it should be moving forward and backward at the same time. Once I added a few more feelers--essentially increasing the resolution of my collision detection--the camera started behaving more like I planned.

Then the muttering diminished, and I cracked open a beer.

1 comment:

  1. nice. good work. thanks. Yes even Tomb Raider Anniversary doesn't have a perfect camera following script and that game as be out in various forms since the mid-90s!

    ReplyDelete