170 likes | 397 Views
MTA-4201 Game Programming Chapter 7 : Collision Detection & Multimedia Elements. Outline 7.1 Collision Detection in 2D & 3D 7.2 Types of Collision Detection 7.3 Implementing Collision Detection in a Project 7.4 Problem Solving 7.5 Audio with 2D game and 3D game with XACT
E N D
MTA-4201 Game ProgrammingChapter 7 : Collision Detection & Multimedia Elements Outline 7.1 Collision Detection in 2D & 3D 7.2 Types of Collision Detection 7.3 Implementing Collision Detection in a Project 7.4 Problem Solving 7.5 Audio with 2D game and 3D game with XACT 7.6 Implementing Sounds and Effects in the Game 7.7 Playing Opening Movie in XNA
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.
7.2 Types of Collision Detection (1) Rectangle Detection Contains & Intersects method
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 per-pixel collision.
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.
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
7.4 Problem Solving How do you make the triangles randomly drops from the top of the screen?
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:
Wave Banks – holds raw audio data. Sound Banks – contains instructions on how to play audio data.
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.
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.
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.
7.6 Ready to Play Sound with Coding In Game1.cs : public class Game1 : Microsoft.Xna.Framework.Game { ……. AudioEngine audioEngine; SoundBank soundBank; WaveBank waveBank; ……. } Protected override void LoadContent() { ………… // 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”); ………….. } protected override void Update(GameTime gameTime) { …………. audioEngine.Update(); ………….. } To play a wave, type this code : soundBank.PlayCue(“missilelaunch”);
7.7 Playing Opening Movie in XNA Video Support in XNA Game Studio 3.1: http://klucher.com/blog/video-support-in-xna-game-studio-3-1/ Method with 3.0 which does not support in XBOX360: http://www.codeplex.com/XNADSPlayer/Release/ProjectReleases.aspx?ReleaseId=11066