| By Berndt Hamboeck | Article Rating: |
|
| November 12, 2008 01:50 PM EST | Reads: |
1,516 |
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 November 12, 2008 Reads 1,516
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.
- Why SOA Needs Cloud Computing - Part 1
- Cloud Expo and The End of Tech Recession
- The Transition to Cloud Computing: What Does It Mean For You?
- A Rules Engine Built in PowerBuilder
- Sybase Named “Silver Sponsor” of iPhone Developer Summit
- How PowerBuilder Got Its Groove Back
- The Cloud Has Cross-Border Ambitions
- Ulitzer Names The World's 30 Most Influential Virtualization Bloggers
- Ulitzer Named "New Media" Partner of Greatly Anticipated iStrategy Event in Berlin
- Risks and Enterprise Mobility?
- Steps for Success in Enterprise Mobility?
- Are Mobile Luddites Resisting Mobility?
- The Difference Between Web Hosting and Cloud Computing
- Sybase CTO to Speak at 4th International Cloud Computing Expo
- Why SOA Needs Cloud Computing - Part 1
- Cloud Expo and The End of Tech Recession
- The Transition to Cloud Computing: What Does It Mean For You?
- Five Reasons to Choose a Private Cloud
- Seeding The Cloud: The Future of Data Management
- The Threat Behind the Firewall
- Economy Drives Adoption of Virtual Lab Technology
- Tips for Efficient PaaS Application Design
- A Rules Engine Built in PowerBuilder
- Sybase Named “Silver Sponsor” of iPhone Developer Summit
- Where Are RIA Technologies Headed in 2008?
- PowerBuilder History - How Did It Evolve?
- The Top 250 Players in the Cloud Computing Ecosystem
- Custom Common Dialogs Using SetWindowsHookEx
- DDDW Tips and Tricks
- OLE - Extending the Capabilities of PowerBuilder
- DataWindow.NET How To: Data Entry Form
- Book Excerpt: Sybase Adaptive Server Anywhere
- Sybase ASE 12.5 Performance and Tuning
- Working with SOA & Web Services in PowerBuilder
- Office 2003 Toolbar: A New Look For Your Old PowerBuilder App
- Dynamically Creating DataWindow Objects


































