| By Berndt Hamboeck | Article Rating: |
|
| June 13, 2008 12:30 PM EDT | Reads: |
3,574 |
In Part 1 of this article, we started to develop a small racing game using XNA Game Studio Express 2.0. We learned about the game loop and how it’s implemented by the XNA (by using the Update and Draw methods) framework. We also created our first track on the screen and four cars started moving on the screen, but, sadly enough, they left the track and weren’t seen again. What does that mean? It means we should take a closer look at collision detection. In addition we should design a menu with different options. You might remember that we want to have a racing game with three computer-driven cars or a network game with one or more other humans playing against us. Sounds easy? Well, it’s not that hard, but we’ll see that a few small changes in the behavior will lead to a few other problems that have to be solved (i.e., a game menu means we have to use so-called game states). Are you ready? Great, let’s go!
Collision Detection
Our cars simply leave the track and disappear from the screen or one car drives through another one, which shouldn’t happen. When the cars leave the track or crash into one another, we need to react within the racing game. This involves two different checks.
Leaving the Track
We definitely need a way to check if our cars are still on track. This can be done easily if we take our track (see Figure 1) and open it with our favorite image processing tool (e.g., .NET Paint or Photoshop). Change the image so it looks like the one in Figure 2. I think you can see the difference. The road – where the cars are allowed to drive – is gray; everything else – where the cars are not allowed to drive – is painted white.
This gives us exactly what we need. We are able to compare the current position of any of our cars against the position in the picture. If the color is gray, everything is fine, and the car is on track. If the current position’s color is white, then the car is leaving the track, which means we will stop the car immediately. What are the black and red lines good for? The black one is the start and finish line so you can count the laps for every single player. The red one determines when the car really finished a lap. You might remember that the XNA framework updates and redraws the scenery several times a second; it wouldn’t be correct if we determined that we are on the black line several times a second and updated the lap counter more than once. The green line is there to prevent cheating. One of the first things we tried in the original game was to cross the start line, turn around, drive back, and turn around again and finally cross the start line again to get another lap counted. This is not supposed to work and is handled by the green line. Only when the green line is crossed can you pass the start line later on and get the lap counted.
Published June 13, 2008 Reads 3,574
Copyright © 2008 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Berndt Hamboeck
Berndt Hamboeck is a senior consultant for BHITCON (www.bhitcon.net). He's a CSI, SCAPC8, EASAC, SCJP2, and started his Sybase development using PB5. You can reach him under admin@bhitcon.net.
- Kindle 2 vs Nook
- Confessions of a Ulitzer Addict
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Windows 7 – Microsoft’s First Step to the Cloud
- Cloud Expo and the End of Tech Recession
- Jill Tummler Singer, Deputy CIO of CIA, Keynotes at GovIT Expo
- Reality Check at the Cloud Computing Expo
- Visual Studio 2010 Is Cloud Friendly
- Fired SCO CEO Fires Back
- Kindle 2 vs Nook
- The Difference Between Web Hosting and Cloud Computing
- Ajax in RichFaces 3.3, JSF 2 and RichFaces 4
- Confessions of a Ulitzer Addict
- Wave on Ulitzer: Confessions of a Google Wave Fanboy
- IBM Hardware Chief, Intel VC Exec Arrested in Insider Trading Scam
- Cloud Computing Best Practices
- Tactical Cloud Computing Panel at 1st Annual GovIT Expo
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Infrastructure-as-a-Service Will Mature in 2010: Microsoft's David Chou
- Eval JavaScript in a Global Context
- Windows 7 – Microsoft’s First Step to the Cloud
- Google Maps and ASP.NET
- Crystal Reports XI & How It Has Changed
- Converting VB6 to VB.NET, Part I
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Where Are RIA Technologies Headed in 2008?
- How to Write High-Performance C# Code
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Implementing Tab Navigation with ASP.NET 2.0
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Viewpoint: "SOA Sucks"
- Programmatically Posting Data to ASP .NET Web Applications




























