Collision Detection.

Slides:



Advertisements
Similar presentations
Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
Advertisements

Constructor and New Fields // Don't synch draw() with vertical retrace of monitor graphics.SynchronizeWithVerticalRetrace = false; IsFixedTimeStep = true;
COMPUTER PROGRAMMING 2 Chapter 7 Sound. Objectives Find out how to prepare sounds for inclusion in Microsoft XNA projects. Incorporate sounds into XNA.
Asteroids Games and Simulations O-O Programming in Java The Walker School The Walker School – Games and Simulations
2D Graphics in XNA Game Studio Express (Modeling a Class in UML) Game Design Experience Professor Jim Whitehead February 5, 2008 Creative Commons Attribution.
Week 1 - Friday.  What did we talk about last time?  C#  SharpDX.
Creating a Hand-Drawn Animated Sprite. Sketch and Scan Create the initial image of your animated character by sketching in (possibly blue) pencil. Outline.
Display 480x800 QVGA 320x480 HVGA Capacitive touch 4 or more contact points Camera 5 mega pixels or more Dedicated camera button Hardware buttons.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience January 14, 2010 Arnav Jhala.
AI & 2D Development COSC 315 Fall 2014 Bridget M. Blodgett.
CHAPTER 4 Images XNA Game Studio 4.0. Objectives Find out how the Content Manager lets you add pictures to Microsoft XNA games. Discover how pictures.
Rob Miles Microsoft MVP University of Hull Fun Programming with Visual Studio.
Getting Started. XNA Game Studio 4.0 To download XNA Game Studio 4.0 itself, go to XNA Game.
Using Namepsaces  This section lists the namespaces that the application will be using frequently. Saves the programmer from specifying a fully qualified.
Developing the Game User Interface (UI) Lesson 5.
1 Useful Tools for Making Video Games Part V An overview of.
11 Adding Sounds Session 7.1. Session Overview  Find out how to capture and manipulate sound on a Windows PC  Show how sound is managed as an item of.
User Input and Collisions COSC 315 Fall 2014 Bridget M. Blodgett.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
CHAPTER 10 Using C# Methods to Solve Problem XNA Game Studio 4.0.
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
XNA An Introduction. What XNA is… Microsoft® XNA™ is composed of industry- leading software, services, resources, and communities focused on enabling.
Object Oriented Design COSC 315 Fall 2014 Bridget M. Blodgett.
XNA Game Studio 4.0 Keyboard and Mouse Controls + more on Animated Sprites.
11 Making a Sprite Session 4.2. Session Overview  Describe the principle of a game sprite, and see how to create a sprite in an XNA game  Learn more.
Sprites, User Input, and Collision COSC 315 Fall 2014 Bridget M. Blodgett.
Guide to Programming with Python Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
Guide to Programming with Python Week 15 Chapter Twelve Sound, Animation, and Program Development: The Astrocrash Game.
Aaron Yuen 1 Outline 9.1Storyboarding & Charting Method for Game Design 9.2Game State in Game Development 9.3Game State Management 9.4GUI Assets for Gameplay.
CHAPTER 3 Getting Player Input XNA Game Studio 4.0.
XNA Basic Displaying Image & Collision Detect. What’s format image that XNA support? XNA support only.bmp.png and.jpg image..PNG have transparent region.
EGR 115 Introduction to Computing for Engineers 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers.
CHAPTER 6 Multiplayer XNA Game Studio 4.0. Objectives Discover how to detect and use individual button-press events in a game. Learn how to create and.
CHAPTER 2 The Game Loop - Variables, Types, Classes and Objects in XNA XNA Game Studio 4.0.
11 Writing Text Session 5.1. Session Overview  Show how fonts are managed in computers  Discover the difference between bitmap fonts and vector fonts.
Playing with Sprites. XNA Game Lifecycle In the faceBall demo program we bounced a smiley face around the graphical display against a background image.
XNA ● Proprietary Microsoft framework ● C#. Interface.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
11 Using the Keyboard in XNA Session 9.1. Session Overview  Discover more detail on how the XNA keyboard is implemented  Find out how to use arrays.
XNA Tutorial 1 For CS134 Lecture. Overview Some of the hard work has already been done for you. If you build and run your game now, the GraphicsDeviceManager.
XNA Sound Effects. float volume, pitch, pan; SoundEffect boing1; SoundEffect boing2; boing1 = Content.Load ("boing1"); boing2 = Content.Load ("boing2");
Rob Miles. Creating a Broken MoodLight An XNA game contains game data which is used by the Draw and Update methods – Update updates the game data – Draw.
Lecture 6: Basic Entities TEALS MINECRAFT PROJECT.
The Stingray Example Program CMT3311. Stingray - an example 2D game May be useful as a simple case study Most 2D games need to solve generic problems.
2D Graphics CMT3311. This covers... How to make a transparent sprite How to add a sprite to your project and draw it Properties of sprites and how to.
CHAPTER 5 Text XNA Game Studio 4.0. Objectives Discover how text is drawn using Microsoft XNA. Add some font resources to your XNA program. Draw some.
Sprites (Images) and Sounds
Sound and more Animations
XNA 4.0 Side Scrolling.
Modern Programming Tools And Techniques-I
Lecture 4 Using SpriteFonts
Contacts: Intro to game programming in Java (with almost-drawingpanel)
MOM! Phineas and Ferb are … Aims:
Object-Oriented Programming Concepts
Week 2 - Monday CS361.
Lecture 5 Sounds & Sound Effects
Using and Creating Sprites
Example: Card Game Create a class called “Card”
Animated Sprites.
Chapter 10 Algorithms.
Side Scrolling Game Development.
Lecture 2 - Building a Monogame Windows Project
Chapter 10 Algorithms.
Breakout in Greenfoot Barb Ericson
MTA-4201 Game Programming Chapter 8 : Scrolling Background & Font
Chapter 10 Algorithms.
Development of a particle system
Chapter 7 The Game Loop and Animation
Collisions with Static Objects
Presentation transcript:

Collision Detection

Collision Detection Concept When two fireflies collide we tag them for removal and add an explosion to the blasts list. The position and velocity of the explosion is set to the average of the two colliding flies. fireflies list blasts graphical display Blast objects are removed from the blasts list when their animation sequence is completed.

Managing Generic Lists A generic list can hold any type of object, so they are especially well suited for holding structured data types. In Object Oriented Programming we can define a structured data type as an instance class. The class has the additional advantage of holding methods and properties related to the class. In this application we will create a class for the firefly and a class for the blast (explosion). Each firefly will have its own position and velocity. To add more natural appearance to their flight we can randomize the rate of the wing flapping animation for each fly and add a random element to their flight. Warning! MS Visual Studio .NET has a quirky random number generator. When a new Random object is created it's initial seed is determined by the clock time, which means that if multiple instances of a Random object are created in a short time (within a few milliseconds of each other) they will have the same initial seed and will therefore may generate the same pseudo-random sequence.

firefly Class public class firefly { public Vector2 Pos; public Vector2 Vel; public int MaxX; public int MaxY; public Rectangle Rect = new Rectangle(); public int AnimCount; public int AnimTimer; public int Time; public int TotCount; public bool Hit; Random rand = new Random();   public firefly(int maxX, int maxY, int totcount, int sheetwidth, int sheetheight, int animtimer, int seed) Random rnd = new Random(seed); MaxX = maxX; MaxY = maxY; Pos.X = rnd.Next(maxX); Pos.Y = rnd.Next(maxY); Vel.X = rnd.Next(10) - 5; Vel.Y = rnd.Next(10) - 5; Rect.X = 0; Rect.Y = 0; Rect.Width = sheetwidth / totcount; Rect.Height = sheetheight; AnimTimer = animtimer; AnimCount = 0; TotCount = totcount; Time = 0; Hit = false; } OK, just so you know, this is a stripped-down version of an instance class which would distress any OOP purist. In this course we use OOP to program, we DO NOT program to use OOP. Which means we will readily ignore any OOP protocol and we will break an OOP rule that interferes with our primary goal to build an efficiently running and fun game project. Hopping off the soapbox, the constructor for firefly creates a random object which needs its a unique seed. We will use a random number generator in the main program to provide this seed.

Move( ) Method for firefly Class public void Move(int deltime) { Time += deltime; Pos.X += Vel.X; if (Pos.X > MaxX) Pos.X = 0; if (Pos.X < 0) Pos.X = MaxX;   Pos.Y += Vel.Y; if (Pos.Y > MaxY) Pos.Y = 0; if (Pos.Y < 0) Pos.Y = MaxY; if (rand.Next(1000) > 990) Vel.X += rand.Next(3) - 1; if (rand.Next(1000) > 990) Vel.Y += rand.Next(3) - 1; if (Vel.X > 5) Vel.X = 5; if (Vel.X <-5) Vel.X = -5; if (Vel.Y > 5) Vel.Y = 5; if (Vel.Y <-5) Vel.Y = -5; if (Time >= AnimTimer) AnimCount = (AnimCount + 1) % TotCount; Time = 0; } Rect.X = AnimCount * Rect.Width; The Move( ) method updates the position of the firefly and increments the AnimCount (frame # being displayed from the firefly spritesheet. Position (Pos) is updated using the velocity (Vel) and velocity is randomly updated with a 10/1000 probability. Magnitude of velocity is limited to 5 units. AnimCount is updated each time accumulated Time exceeds the randomly set number of milliseconds per frame, AnimTimer. The location of Rect is the region of the spritesheet being displayed for the current frame of the animation. flies.png

blast Class public Vector2 Pos; public Vector2 Vel; public Rectangle Rect = new Rectangle(); public int AnimCount; public bool Done; public int AnimTimer; int TotCount; public int Width; public int Height; public int Time; int rownum; int colnum;   public blast(Vector2 pos, Vector2 vel1, Vector2 vel2, int totcount, int width, int height, int animtimer) { Pos = pos; Vel.X = (vel1.X + vel2.X) / 2; Vel.Y = (vel1.Y + vel2.Y) / 2; AnimCount = 0; Rect.X = 0; Rect.Y = 0; Rect.Width = width; Rect.Height = height; Width = width; Height = height; TotCount = totcount; AnimTimer = animtimer; rownum = 0; colnum = 0; Done = false; } When two fireflies collide they are tagged for removal and an explosion (an instance the the blast class) is created. The blast will exist for 16 display frames as the 16 frames of the explosion sequence is used to animate the blast. At the end of the animation, the completed instance of blast is tagged for removal. An interesting feature of a blast is that its velocity is set to the average of the velocities of the two colliding fireflies. This gives a more realistic appearance than a static explosion sequence. In this demo we set the rate of animation to 60 milliseconds per frame (AnimTimer = 60).

Move( ) Method for blast Class The individual sprites blits are 64x64 pixels. The rownum and colnum are in the range 0 through and specify the sprite frame 0 through 15. When all frames have been displayed the Boolean field Done is set to TRUE. 256 x 256 pixels public void Move(int deltime) { Time += deltime; Pos.X += Vel.X; Pos.Y += Vel.Y; if (Time >= AnimTimer) AnimCount += 1; Time = 0; } colnum = AnimCount % 4; rownum = AnimCount / 4; Rect.X = colnum * Width; Rect.Y = rownum * Height; if (AnimCount >= TotCount) Done = true; explosion.bmp

Main Program Atomic Fireflies Collision Detection Demo GraphicsDeviceManager graphics; List<firefly> flies = new List<firefly>(); List<blast> blasts = new List<blast>(); SpriteBatch spriteBatch; SoundEffect[] explosion = new SoundEffect[4]; Vector2 spos; Vector2 bpos; Texture2D bkg; Texture2D flysprite; Texture2D expsprite; Rectangle rect = new Rectangle(); KeyboardState kbstate; SpriteFont font; Vector2 txtpos = new Vector2(10, 10); Random rnd = new Random(); int minDist = 10;   protected override void Initialize() { base.Initialize(); spos.X = 26; spos.Y = 25; bpos.X = 32; bpos.Y = 32; graphics.IsFullScreen = true; graphics.ApplyChanges(); } Generic lists are created for firefly and blast objects. Four different explosion sounds will be called randomly for each collision. The evening sky background, the firefly sprite sheet and the explosion sprite sheet are loaded as Texture2D data types. An instance of the Random number generator rnd is created to be used to generated unique seeds for the generation of Random generators in the firefly class. This application runs in fullscreen mode comment out the call to graphics.ApplyChanges( ) to convert this application to Windows UI mode.

Atomic Fireflies LoadContent( ) Method protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); explosion[0] = Content.Load<SoundEffect>("explosion-01"); explosion[1] = Content.Load<SoundEffect>("explosion-02"); explosion[2] = Content.Load<SoundEffect>("explosion-03"); explosion[3] = Content.Load<SoundEffect>("explosion-04"); bkg = Content.Load<Texture2D>("bkg"); flysprite = Content.Load<Texture2D>("flies"); expsprite = Content.Load<Texture2D>("explosion"); graphics.PreferredBackBufferWidth = bkg.Width; graphics.PreferredBackBufferHeight = bkg.Height; font = Content.Load<SpriteFont>("SpriteFont1"); rect.X = 0; rect.Y = 0; rect.Width = bkg.Width; rect.Height = bkg.Height; graphics.ApplyChanges(); for (int i = 0; i < 10; i++) flies.Add(new firefly(bkg.Width, bkg.Height, 4, 208, 50, rnd.Next(60)+20, rnd.Next(100000))); } To start the demo 10 fireflies are instantiated and added to the flies list. Note the use of rnd.Next(100000) to generate a different seed for each instance of firefly. Also the AnimTimer is set to a random range of values between 20 and 79 milliseconds.

Collision Detection Method This method demonstrates the versatility of the generic list. Although we do not have to managed the placement of items in a generic list when new items are added and/or old items are removed, we can control the order of access of individual elements in a generic list by index value. The nested for loops compare the locations of every pair of fireflies in the list. When two files are found at or near (within 10 pixels) the same location they are tagged for removal from the list, a new blast object is added to the blasts list and an explosion sound is played. When the number of items to be compared becomes very large we will need to partition the list of items into bins and compare only those items in the same or neighboring bins. For 2D games, a two-dimensional array will be used to partition the objects by their index values. public void collision() { for(int i=0;i<flies.Count-1;i++) for (int j = i + 1; j < flies.Count; j++) if ((Math.Abs(flies[i].Pos.X - flies[j].Pos.X) + Math.Abs(flies[i].Pos.Y - flies[j].Pos.Y)) < minDist) blasts.Add(new blast(flies[i].Pos,flies[i].Vel,flies[j].Vel,16,64,64,60)); flies[i].Hit = true; flies[j].Hit = true; explosion[rnd.Next(4)].Play(); }

Atomic Fireflies Update( ) Method protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); kbstate = Keyboard.GetState(); if (kbstate.IsKeyDown(Keys.Left) && kbstate.IsKeyDown(Keys.Right))   if (kbstate.IsKeyDown(Keys.Right)) flies.Add(new firefly(bkg.Width, bkg.Height, 4, 208, 50, rnd.Next(20) + 40, rnd.Next(100000))); if (kbstate.IsKeyDown(Keys.Left) && flies.Count > 0) flies[flies.Count-1].Hit = true; collision(); foreach (firefly fly in flies) fly.Move(gameTime.ElapsedGameTime.Milliseconds); foreach (blast pow in blasts) pow.Move(gameTime.ElapsedGameTime.Milliseconds); flies.RemoveAll(delegate(firefly fly) return fly.Hit; }); blasts.RemoveAll(delegate(blast pow) return pow.Done; base.Update(gameTime); } Fireflies can be removed or added to the flies list by pressing the Left-Arrow and Right-Arrow keys. The Move( ) methods for each firefly and each blast are called to update the position and animation frame for each object. At the end of Update( ) flies that have been involved in a collision and blasts that have completed their animation sequence are removed from their respective lists using delegate functions as shown.

Atomic Fireflies Draw( ) Method protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.White); spriteBatch.Begin(); spriteBatch.Draw(bkg, rect, Color.White); foreach (firefly fly in flies) spriteBatch.Draw(flysprite, fly.Pos, fly.Rect, Color.White, 0, spos, (float)0.5, 0, 0); foreach (blast pow in blasts) spriteBatch.Draw(expsprite, pow.Pos, pow.Rect, Color.White, 0, bpos,1, 0, 0); spriteBatch.DrawString(font, Convert.ToString(flies.Count), txtpos, Color.White); spriteBatch.End(); base.Draw(gameTime); } For each frame the Draw( ) method draws the background images followed by the fireflies and finally the active blasts being displayed. The fireflies.zip file contains the complete project