11. Collision Detection
• XNA has two main types for implementing collision detection
– BoundingBox
– BoundingSphere
12. Collision Detection
• XNA has two main types for implementing collision detection
– BoundingBox
• a box will better suit a rectangular high-rise building
– BoundingSphere
13. Collision Detection
• XNA has two main types for implementing collision detection
– BoundingBox
• a box will better suit a rectangular high-rise building
– BoundingSphere
• a bounding sphere offers a better fit for rounded objects such as a domed arena
22. Collision Detection - BOUNDING BOX
• Initializing the Bounding BOX
• Intersects() common overloads
BoundingBox boundingBox = new BoundingBox(Vector3 min, Vector3 max);
public bool Intersects(BoundingBox box);
public bool Intersects(BoundingSphere sphere);
23. Collision Detection - BOUNDING BOX
• Comparing one bounding box with another:
• Comparing a bounding box with a bounding sphere:
• Comparing a bounding box with a single three-dimensional position:
public void Contains(ref BoundingBox box, out ContainmentType result);
public void Contains(ref BoundingSphere sphere, out ContainmentType result);
public void Contains(ref Vector3 point, out ContainmentType result);
25. Collision Detection - BOUNDING SPHERE
• Initializing the Bounding Sphere
• Intersects() Common Overloads
• Contains()
public BoundingSphere(Vector3 center, float radius);
public bool Intersects(BoundingBox box);
public bool Intersects(BoundingSphere sphere);
public void Contains(ref BoundingSphere sphere,out ContainmentType result);
public void Contains(ref BoundingBox box, out ContainmentType result);
public void Contains(ref Vector3 point, out ContainmentType result);
26. Collision Detection - BOUNDING SPHERE
• Initializing the Bounding Sphere
• Intersects() Common Overloads
• Contains()
public bool Intersects(BoundingBox box);
public bool Intersects(BoundingSphere sphere);
public void Contains(ref BoundingSphere sphere,out ContainmentType result);
public void Contains(ref BoundingBox box, out ContainmentType result);
public void Contains(ref Vector3 point, out ContainmentType result);
a three-dimensional position
public BoundingSphere(Vector3 center, float radius);
32. Collision Detection
• Also, because the default model processor doesn’t generate a bounding box
(AABB) volume, we need to generate one for the model
33. Collision Detection
• Avoid testing the collision with each mesh of the model, by creating a global
bounding sphere (or global bounding Box) for the entire model!
34. Collision Detection
• Usage of bounding spheres is emphasized because spheres are easier to update
and transform than bounding boxes
35. EARLY WARNING SYSTEMS
Don’t check a 200 miles sphere collision with you!
Rbwhitaker web site: http://rbwhitaker.wikidot.com/collision-detection
49. XNA With Windows Form App/ WPF App
For more info, go to:
http://www.codeproject.com/KB/game/XNA_And_Beyond.aspx
50. XNA With Windows Form App
• 2 WAYS!
– 2D Graphics (Professional)
– Windows Forms (Ordinary)
51. XNA With Windows Form App
• 2 WAYS!
– 2D Graphics (Professional)
• See the appendix for full tutorial pdf
– Windows Forms (Ordinary)
52. XNA With Windows Form App
• 2 WAYS!
– 2D Graphics (Professional)
• See the appendix for full tutorial pdf
– Windows Forms (Ordinary)
• Use the appended project directly
53. XNA With Windows Form App
• 2 WAYS!
– 2D Graphics (Professional)
• See the appendix for full tutorial pdf
– Windows Forms (Ordinary)
• Use the appended project directly
• OR, do it your self as the following slides
54. XNA With Windows Form App
• Windows Forms! (See the template project in appendix)
55. XNA With Windows Form App
• Steps
– Add new Form in the same XNA Project (“Form1”);
– Add Panel to the form you have just created (“Form1”);
– Add to the top of “Form1”
public IntPtr PanelHandle
{
get
{
return this.panel1.IsHandleCreated? this.panel1.Handle : IntPtr.Zero;
}
}
56. XNA With Windows Form App
• Now, In Game1 class do the following
– Add the following at the top (using, renaming namespace)
– Create a reference to Form1 to handle the instance at runtime;using SysWinForms = System.Windows.Forms;
Form1 myForm;
57. XNA With Windows Form App
• Now, In Game1 class do the following
– Add the following two methods
– Create a reference to Form1 to handle the instance at runtime;
– Initialize
void myForm_HandleDestroyed(object sender, EventArgs e)
{
this.Exit();
}
void gameWindowForm_Shown(object sender, EventArgs e)
{
((SysWinForms.Form)sender).Hide();
}
58. XNA With Windows Form App
• Now, In Game1 class do the following
– Add the following code to Initialize() method
– Create a reference to Form1 to handle the instance at runtime;
– Initialize
protected override void Initialize()
{
SysWinForms.Form gameWindowForm =
(SysWinForms.Form)SysWinForms.Form.FromHandle(this.Window.Handle);
gameWindowForm.Shown += new EventHandler(gameWindowForm_Shown);
this.myForm = new Form1();
myForm.HandleDestroyed += new EventHandler(myForm_HandleDestroyed);
myForm.Show();
base.Initialize();
}
59. XNA With Windows Form App
• Now, In Game1 class do the following
– Add the following code to Draw() method so it looks like this
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
// This one will do the trick we are looking for!
this.GraphicsDevice.Present(null, null, myForm.PanelHandle);
}
73. 2D and 3D Combined
spriteBatch.Begin();
spriteBatch.Begin(SpriteSortMode.Immediate,
BlendState.Opaque);
• finally! change SpriteBatch From
• To
74. 2D and 3D Combined
spriteBatch.Begin();
spriteBatch.Begin(SpriteSortMode.Immediate,
BlendState.Opaque);
• finally! change SpriteBatch From
• To
75. 2D and 3D Combined
spriteBatch.Begin();
spriteBatch.Begin(SpriteSortMode.Immediate,
BlendState.AlphaBlend);
• finally! change SpriteBatch From
• To
76. 2D and 3D Combined
spriteBatch.Begin();
spriteBatch.Begin(SpriteSortMode.BackToFront,
BlendState.AlphaBlend);
• finally! change SpriteBatch From
• To