1
MTA-4201 Game Programming
Chapter 7 : Collision Detection & Multimedia Elements
Outline
7.1
7.2
7.3
7.4
7.5
7.6
7.7
Collision Detection in 2D & 3D
Types of Collision Detection
Implementing Collision Detection in a Project
Problem Solving
Audio with 2D game and 3D game with XACT
Implementing Sounds and Effects in the Game
Playing Opening Movie in XNA
Aaron Yuen
2
7.1 Collision Detection in 2D & 3D
• Contains and Intersects Methods
• Bounding volume classes have methods to support two
types of collision tests: intersection tests and
containment tests. The intersects methods check
whether the two objects being tested overlap in any way.
As soon as the test finds that the objects do intersect, it
returns without trying to determine the degree of the
intersection. The contains methods determine whether
the objects simply intersect or whether one of the
objects is completely contained by the other. Since the
intersects methods need only determine whether an
intersection occurred, they tend to be faster than the
contains methods. Use the contains methods only when
the degree of intersection is relevant.
Aaron Yuen
7.2 Types of Collision Detection
(1) Rectangle Detection
Contains & Intersects method
Aaron Yuen
3
4
7.2 Types of Collision Detection
(2) Per-Pixel Collision (Pixel Perfect)
Per-pixel collision requires examining individual pixels of a given texture.
If there is overlapping pixel, collision happens. This is called perpixel collision.
Aaron Yuen
5
7.2 Types of Collision Detection
(2) Per-Pixel Collision (Pixel Perfect)
Rectangle Collision Detection has exclusive use of rectangles resulted in
imprecise behavior for non-rectangular objects.
In order to achieve the desired behavior, the code must examine every
overlapping pixel to determine if there is a collision. This is called
per-pixel collision.
Aaron Yuen
6
7.3 Implementing Per-Pixel Collision Detection
Step 1 : Get Texture Data
Per-pixel collision requires examining individual pixels of a given texture. To
access the individual pixels, you must call Texture2D.GetData. This method
copies the pixels into an array of pixel data of type Color.
Step 2 : Write Per-Pixel Collision Method
A method should be written to perform the per-pixel collision test.
Step 3 : Invoke the Per-pixel Collision Test
The method should be called in the Update( ) method.
If you needs to implement Per-Pixel Collision Detection in your game project, follow the
detailed instructions in the following link :
PerPixelCollision/PerPixelCollision.htm
Aaron Yuen
7
7.3 Implementing Per-Pixel Collision Detection
Aaron Yuen
8
7.4 Problem Solving
How do you make the triangles randomly drops from the top of the screen?
Aaron Yuen
7.5 Audio with 2D Game -- Microsoft Cross-Platform
Audio Creation Tool (XACT)
• Music sets the atmosphere for our games and
sound effects adds to the realism of our games.
• In XNA, we need to use Microsoft Cross-Platform
Audio Creation Tool (XACT), which Microsoft
provides for us.
• The tool is in the programs menu:
Aaron Yuen
9
10
Wave Banks – holds raw audio data.
Sound Banks – contains instructions
on how to play audio data.
Aaron Yuen
11
Wave Bank
To insert wave into the Wave Bank, the steps are :
Step 1 :
Open a new Wave Bank, right click on the Wave Bank window;
Step 2 :
Select the wave files to be added into the game.
Aaron Yuen
12
Sound Banks
Then you need to import the raw data from Wave Bank to
Sound Bank to allow the XNA to use it.
Step 1 :
Select the files from the Wave bank window;
Step 2 :
Drag the files to the bottom of the Sound Bank. It is done.
Aaron Yuen
13
7.6 Ready to Play Sounds
Open a new XNA project
with C#
Step 1 :
Import the XACT project
to the XNA project.
The sounds are then added
to the project.
Aaron Yuen
7.6 Ready to Play Sound with Coding
14
In Game1.cs :
public class Game1 : Microsoft.Xna.Framework.Game
{
protected override void Update(GameTime
gameTime)
{
…….
………….
AudioEngine audioEngine;
SoundBank
audioEngine.Update();
soundBank;
WaveBank
…………..
waveBank;
…….
}
}
To play a wave, type this code :
Protected override void LoadContent()
soundBank.PlayCue(“missilelaunch”);
{
…………
// Inilization of the Wave files with the AudioEngine
audioEngine = new AudioEngine(“Content\\Audio\\TutorialAudio.xgs”);
waveBank = new WaveBank(audioEngine, “Content\\Audio\\Wave Bank.xwb”);
soundBank = new SoundBank(audioEngine, “Content\\Audio\\Sound Bank.xsb”);
…………..
}
Aaron Yuen
15
7.7 Playing Opening Movie in XNA
Video Support in XNA Game Studio 3.1:
http://klucher.com/blog/video-support-in-xna-gamestudio-3-1/
Method with 3.0 which does not support in XBOX360:
http://www.codeplex.com/XNADSPlayer/Release/ProjectRel
eases.aspx?ReleaseId=11066
Aaron Yuen
© Copyright 2026 Paperzz