SlideShare a Scribd company logo
Mohammad Shaker
mohammadshaker.com
@ZGTRShaker
2011, 2012, 2013, 2014
XNA Game Development
L08 – Amazing XNA Utilities
Collision Detection
Collision Detection
determines whether two objects overlap and therefore have
collided in your virtual world.
Collision Detection
Accurate collision detection is fundamental
to a solid game engine.
XNA L08–Amazing XNA Utilities
Your cars would drive off the road, your people
would walk through buildings, and your camera
would travel through cement walls :D
XNA L08–Amazing XNA Utilities
Collision Detection
• Microsoft Book, Chapter 18, Page: 285
Collision Detection
• XNA has two main types for implementing collision detection
Collision Detection
• XNA has two main types for implementing collision detection
– BoundingBox
Collision Detection
• XNA has two main types for implementing collision detection
– BoundingBox
– BoundingSphere
Collision Detection
• XNA has two main types for implementing collision detection
– BoundingBox
• a box will better suit a rectangular high-rise building
– BoundingSphere
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
Collision Detection
– BoundingSphere
Collision Detection
– BoundingSphere
Collision Detection
– BoundingSphere
Collision Detection
– BoundingSphere
Collision Detection
• CONTAINMENT TYPE
– BoundingBox
– BoundingSphere
Collision Detection
• CONTAINMENT TYPE
– BoundingBox
– BoundingSphere
Collision Detection
• CONTAINMENT TYPE
– BoundingBox
– BoundingSphere
Collision Detection
BOUNDING BOX
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);
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);
Collision Detection
BOUNDING SPHERE
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);
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);
Collision Detection
• Different sphere groups for varying levels of efficiency and accuracy
Collision Detection
• Microsoft, Collision Detection, P: 290 - 304
Check it out in
“App1-CollisionDetection-
Micosoft”
Collision Detection
Apress book, Chapter 13, Page 367
Collision Detection
Collision Detection
• Also, because the default model processor doesn’t generate a bounding box
(AABB) volume, we need to generate one for the model
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!
Collision Detection
• Usage of bounding spheres is emphasized because spheres are easier to update
and transform than bounding boxes
EARLY WARNING SYSTEMS
Don’t check a 200 miles sphere collision with you!
Rbwhitaker web site: http://rbwhitaker.wikidot.com/collision-detection
Collision Detection
• Approximation
Collision Detection
• Approximation
Collision Detection
• Approximation
Collision Detection
• Approximation
When to use What?
Collision Detection
• Approximation
A better way to combine
both approaches
Collision Detection
hierarchical modeling
Collision Detection
Collision Detection
Collision Detection
Collision Detection
In hierarchical model you would use both the
large sphere, and the small spheres.
Collision Detection
Quad Tree Approach
Collision Detection Example in XNA
private bool IsCollision(Model model1, Matrix world1, Model model2, Matrix world2)
{
for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++)
{
BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere;
sphere1 = sphere1.Transform(world1);
for (int meshIndex2 = 0; meshIndex2 < model2.Meshes.Count; meshIndex2++)
{
BoundingSphere sphere2 = model2.Meshes[meshIndex2].BoundingSphere;
sphere2 = sphere2.Transform(world2);
if (sphere1.Intersects(sphere2))
return true;
}
}
return false;
}
Collision Detection Example in XNA
“App2-CollisionDetection-WebSite”
XNA With Windows Form App/ WPF App
For more info, go to:
http://www.codeproject.com/KB/game/XNA_And_Beyond.aspx
XNA With Windows Form App
• 2 WAYS!
– 2D Graphics (Professional)
– Windows Forms (Ordinary)
XNA With Windows Form App
• 2 WAYS!
– 2D Graphics (Professional)
• See the appendix for full tutorial pdf
– Windows Forms (Ordinary)
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
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
XNA With Windows Form App
• Windows Forms! (See the template project in appendix)
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;
}
}
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;
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();
}
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();
}
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);
}
XNA With Windows Form App
• Now, ctrl+F5 and u r good to go!
2D and 3D Combined
2D and 3D Combined
2D and 3D Combined
2D and 3D Combined
2D and 3D Combined
2D and 3D Combined
2D and 3D Combined
Begin Method (SpriteSortMode, BlendState, SamplerState,
DepthStencilState,
RasterizerState,
Effect,
Matrix)
Begin Method ()
Begin Method (SpriteSortMode, BlendState)
Begin Method (SpriteSortMode, BlendState, SamplerState,
DepthStencilState,
RasterizerState)
Begin Method (SpriteSortMode, BlendState, SamplerState,
DepthStencilState,
RasterizerState,
Effect)
sortMode
Sprite drawing order.
blendState
Blending options.
samplerState
Texture sampling options.
depthStencilState
Depth and stencil options.
rasterizerState
Rasterization options.
effect
Effect state options.
transformMatrix
Transformation matrix for scale, rotate,
translate options.
2D and 3D Combined
• SpriteSortMode
2D and 3D Combined
• BlendState
• See more: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.blend.aspx
2D and 3D Combined
• BlendState
2D and 3D Combined
• BlendState
2D and 3D Combined
• BlendState
2D and 3D Combined
spriteBatch.Begin();
spriteBatch.Begin(SpriteSortMode.Immediate,
BlendState.Opaque);
• finally! change SpriteBatch From
• To
2D and 3D Combined
spriteBatch.Begin();
spriteBatch.Begin(SpriteSortMode.Immediate,
BlendState.Opaque);
• finally! change SpriteBatch From
• To
2D and 3D Combined
spriteBatch.Begin();
spriteBatch.Begin(SpriteSortMode.Immediate,
BlendState.AlphaBlend);
• finally! change SpriteBatch From
• To
2D and 3D Combined
spriteBatch.Begin();
spriteBatch.Begin(SpriteSortMode.BackToFront,
BlendState.AlphaBlend);
• finally! change SpriteBatch From
• To
Viewports & Split Screen Games
Viewports & Split Screen Games
• Base Project
• “App1-Viewports”
Viewports & Split Screen Games
• Viewports
Viewports & Split Screen Games
• Viewports – Multiple ones
So, What’s the idea
is all about?!
Viewports & Split Screen Games
• Viewports – Multiple ones
Multiple “Camera”s
Viewports & Split Screen Games
• Viewports – Multiple ones
Multiple “LookAt()”s
Viewports & Split Screen Games
• Global Scope
private Matrix topView = Matrix.CreateLookAt(new Vector3(0, 0, 4),
new Vector3(0, 0, 0), new Vector3(0, 0.001f, 1f));
private Matrix frontView = Matrix.CreateLookAt(new Vector3(0, 4, 0),
new Vector3(0, 0, 0), Vector3.UnitZ);
private Matrix sideView = Matrix.CreateLookAt(new Vector3(4, 0, 0),
new Vector3(0, 0, 0), Vector3.UnitZ);
private Matrix perspectiveView = Matrix.CreateLookAt(new Vector3(4, 4, 4),
new Vector3(0, 0, 0), Vector3.UnitZ);
private Viewport topViewport;
private Viewport sideViewport;
private Viewport frontViewport;
private Viewport perspectiveViewport;
private Matrix topView = Matrix.CreateLookAt(new Vector3(0, 0, 4),
new Vector3(0, 0, 0), new Vector3(0, 0.001f, 1f));
private Matrix frontView = Matrix.CreateLookAt(new Vector3(0, 4, 0),
new Vector3(0, 0, 0), Vector3.UnitZ);
private Matrix sideView = Matrix.CreateLookAt(new Vector3(4, 0, 0),
new Vector3(0, 0, 0), Vector3.UnitZ);
private Matrix perspectiveView = Matrix.CreateLookAt(new Vector3(4, 4, 4),
new Vector3(0, 0, 0), Vector3.UnitZ);
private Viewport topViewport;
private Viewport sideViewport;
private Viewport frontViewport;
private Viewport perspectiveViewport;
Matrix.CreateLookAt(CameraPosition, CameraTarget, CameraUp);
Viewports & Split Screen Games
LookAt()
• Initialize()
protected override void Initialize()
{
base.Initialize();
int ViewportWidth = 200;
int ViewportHeight = 200;
Viewport defaultViewport = GraphicsDevice.Viewport;
topViewport = defaultViewport;
topViewport.X = 0;
topViewport.Y = 0;
topViewport.Width = ViewportWidth;
topViewport.Height = ViewportHeight;
topViewport.MinDepth = 0;
topViewport.MaxDepth = 1;
sideViewport = defaultViewport;
sideViewport.X = ViewportWidth;
sideViewport.Y = 0;
sideViewport.Width = ViewportWidth;
sideViewport.Height = ViewportHeight;
sideViewport.MinDepth = 0;
sideViewport.MaxDepth = 1;
frontViewport = defaultViewport;
frontViewport.X = 0;
frontViewport.Y = ViewportHeight;
frontViewport.Width = ViewportWidth;
frontViewport.Height = ViewportHeight;
frontViewport.MinDepth = 0;
frontViewport.MaxDepth = 1;
perspectiveViewport = defaultViewport;
perspectiveViewport.X = ViewportWidth;
perspectiveViewport.Y = ViewportHeight;
perspectiveViewport.Width = ViewportWidth;
perspectiveViewport.Height =
ViewportHeight;
perspectiveViewport.MinDepth = 0;
perspectiveViewport.MaxDepth = 1;
}
Viewports & Split Screen Games
Viewports & Split Screen Games
• Initialize()
protected override void Initialize()
{
base.Initialize();
int ViewportWidth = 200;
int ViewportHeight = 200;
Viewport defaultViewport = GraphicsDevice.Viewport;
topViewport = defaultViewport;
topViewport.X = 0;
topViewport.Y = 0;
topViewport.Width = ViewportWidth;
topViewport.Height = ViewportHeight;
topViewport.MinDepth = 0;
topViewport.MaxDepth = 1;
sideViewport = defaultViewport;
sideViewport.X = ViewportWidth;
sideViewport.Y = 0;
sideViewport.Width = ViewportWidth;
sideViewport.Height = ViewportHeight;
sideViewport.MinDepth = 0;
sideViewport.MaxDepth = 1;
frontViewport = defaultViewport;
frontViewport.X = 0;
frontViewport.Y = ViewportHeight;
frontViewport.Width = ViewportWidth;
frontViewport.Height = ViewportHeight;
frontViewport.MinDepth = 0;
frontViewport.MaxDepth = 1;
perspectiveViewport = defaultViewport;
perspectiveViewport.X = ViewportWidth;
perspectiveViewport.Y = ViewportHeight;
perspectiveViewport.Width = ViewportWidth;
perspectiveViewport.Height =
ViewportHeight;
perspectiveViewport.MinDepth = 0;
perspectiveViewport.MaxDepth = 1;
}
• Initialize()
protected override void Initialize()
{
base.Initialize();
int ViewportWidth = 200;
int ViewportHeight = 200;
Viewport defaultViewport = GraphicsDevice.Viewport;
topViewport = defaultViewport;
topViewport.X = 0;
topViewport.Y = 0;
topViewport.Width = ViewportWidth;
topViewport.Height = ViewportHeight;
topViewport.MinDepth = 0;
topViewport.MaxDepth = 1;
sideViewport = defaultViewport;
sideViewport.X = ViewportWidth;
sideViewport.Y = 0;
sideViewport.Width = ViewportWidth;
sideViewport.Height = ViewportHeight;
sideViewport.MinDepth = 0;
sideViewport.MaxDepth = 1;
frontViewport = defaultViewport;
frontViewport.X = 0;
frontViewport.Y = ViewportHeight;
frontViewport.Width = ViewportWidth;
frontViewport.Height = ViewportHeight;
frontViewport.MinDepth = 0;
frontViewport.MaxDepth = 1;
perspectiveViewport = defaultViewport;
perspectiveViewport.X = ViewportWidth;
perspectiveViewport.Y = ViewportHeight;
perspectiveViewport.Width = ViewportWidth;
perspectiveViewport.Height =
ViewportHeight;
perspectiveViewport.MinDepth = 0;
perspectiveViewport.MaxDepth = 1;
}
Viewports & Split Screen Games
• Initialize()
protected override void Initialize()
{
base.Initialize();
int ViewportWidth = 200;
int ViewportHeight = 200;
Viewport defaultViewport = GraphicsDevice.Viewport;
topViewport = defaultViewport;
topViewport.X = 0;
topViewport.Y = 0;
topViewport.Width = ViewportWidth;
topViewport.Height = ViewportHeight;
topViewport.MinDepth = 0;
topViewport.MaxDepth = 1;
sideViewport = defaultViewport;
sideViewport.X = ViewportWidth;
sideViewport.Y = 0;
sideViewport.Width = ViewportWidth;
sideViewport.Height = ViewportHeight;
sideViewport.MinDepth = 0;
sideViewport.MaxDepth = 1;
frontViewport = defaultViewport;
frontViewport.X = 0;
frontViewport.Y = ViewportHeight;
frontViewport.Width = ViewportWidth;
frontViewport.Height = ViewportHeight;
frontViewport.MinDepth = 0;
frontViewport.MaxDepth = 1;
perspectiveViewport = defaultViewport;
perspectiveViewport.X = ViewportWidth;
perspectiveViewport.Y = ViewportHeight;
perspectiveViewport.Width = ViewportWidth;
perspectiveViewport.Height =
ViewportHeight;
perspectiveViewport.MinDepth = 0;
perspectiveViewport.MaxDepth = 1;
}
Viewports & Split Screen Games
Viewports & Split Screen Games
Viewports & Split Screen Games
Viewports & Split Screen Games
Viewports & Split Screen Games
Viewports & Split Screen Games
Viewports & Split Screen Games
Viewports & Split Screen Games
• Draw()protected override void Draw(GameTime gameTime)
{ GraphicsDevice.Clear(Color.CornflowerBlue);
Viewport original = graphics.GraphicsDevice.Viewport;
graphics.GraphicsDevice.Viewport = topViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, topView, projection);
graphics.GraphicsDevice.Viewport = sideViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, sideView, projection);
graphics.GraphicsDevice.Viewport = frontViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, frontView, projection);
graphics.GraphicsDevice.Viewport = perspectiveViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, perspectiveView, projection);
GraphicsDevice.Viewport = original;
base.Draw(gameTime);
}
Viewports & Split Screen Games
• Draw()protected override void Draw(GameTime gameTime)
{ GraphicsDevice.Clear(Color.CornflowerBlue);
Viewport original = graphics.GraphicsDevice.Viewport;
graphics.GraphicsDevice.Viewport = topViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, topView, projection);
graphics.GraphicsDevice.Viewport = sideViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, sideView, projection);
graphics.GraphicsDevice.Viewport = frontViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, frontView, projection);
graphics.GraphicsDevice.Viewport = perspectiveViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, perspectiveView, projection);
GraphicsDevice.Viewport = original;
base.Draw(gameTime);
}
Viewports & Split Screen Games
• Draw()protected override void Draw(GameTime gameTime)
{ GraphicsDevice.Clear(Color.CornflowerBlue);
Viewport original = graphics.GraphicsDevice.Viewport;
graphics.GraphicsDevice.Viewport = topViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, topView, projection);
graphics.GraphicsDevice.Viewport = sideViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, sideView, projection);
graphics.GraphicsDevice.Viewport = frontViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, frontView, projection);
graphics.GraphicsDevice.Viewport = perspectiveViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, perspectiveView, projection);
GraphicsDevice.Viewport = original;
base.Draw(gameTime);
}
Viewports & Split Screen Games
• Draw()protected override void Draw(GameTime gameTime)
{ GraphicsDevice.Clear(Color.CornflowerBlue);
Viewport original = graphics.GraphicsDevice.Viewport;
graphics.GraphicsDevice.Viewport = topViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, topView, projection);
graphics.GraphicsDevice.Viewport = sideViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, sideView, projection);
graphics.GraphicsDevice.Viewport = frontViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, frontView, projection);
graphics.GraphicsDevice.Viewport = perspectiveViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, perspectiveView, projection);
GraphicsDevice.Viewport = original;
base.Draw(gameTime);
}
Viewports & Split Screen Games
• Draw()protected override void Draw(GameTime gameTime)
{ GraphicsDevice.Clear(Color.CornflowerBlue);
Viewport original = graphics.GraphicsDevice.Viewport;
graphics.GraphicsDevice.Viewport = topViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, topView, projection);
graphics.GraphicsDevice.Viewport = sideViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, sideView, projection);
graphics.GraphicsDevice.Viewport = frontViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, frontView, projection);
graphics.GraphicsDevice.Viewport = perspectiveViewport;
//GraphicsDevice.Clear(Color.Black);
DrawModel(model, world, perspectiveView, projection);
GraphicsDevice.Viewport = original;
base.Draw(gameTime);
}
Viewports & Split Screen Games
• “App2-ViewportsFinished”
Picking Objects
Picking Objects
• How to pick?!
Picking Objects
• How to pick?!
Picking Objects
public Ray CalculateRay(Vector2 mouseLocation, Matrix view, Matrix projection,
Viewport viewport)
{
Vector3 nearPoint = viewport.Unproject(
new Vector3(mouseLocation.X, mouseLocation.Y, 0.0f),
projection,
view,
Matrix.Identity);
Vector3 farPoint = viewport.Unproject(
new Vector3(mouseLocation.X, mouseLocation.Y, 1.0f),
projection,
view,
Matrix.Identity);
Vector3 direction = farPoint - nearPoint;
direction.Normalize();
return new Ray(nearPoint, direction);
}
Picking Objects
public bool Intersects(Vector2 mouseLocation,
Model model, Matrix world, Matrix view, Matrix projection,
Viewport viewport)
{
for (int index = 0; index < model.Meshes.Count; index++)
{
BoundingSphere sphere = model.Meshes[index].BoundingSphere;
sphere = sphere.Transform(world);
float? distance = IntersectDistance(sphere, mouseLocation, view, projection, viewport);
if (distance != null)
{
return true;
}
}
return false;
}
Picking Objects
public float? IntersectDistance(BoundingSphere sphere, Vector2 mouseLocation,
Matrix view, Matrix projection, Viewport viewport)
{
Ray mouseRay = CalculateRay(mouseLocation, view, projection, viewport);
return mouseRay.Intersects(sphere);
}
Picking Objects
• “App-Picking”
Take a Look on my other courses
@ http://www.slideshare.net/ZGTRZGTR
Available courses to the date of this slide:
http://www.mohammadshaker.com
mohammadshakergtr@gmail.com
https://twitter.com/ZGTRShaker @ZGTRShaker
https://de.linkedin.com/pub/mohammad-shaker/30/122/128/
http://www.slideshare.net/ZGTRZGTR
https://www.goodreads.com/user/show/11193121-mohammad-shaker
https://plus.google.com/u/0/+MohammadShaker/
https://www.youtube.com/channel/UCvJUfadMoEaZNWdagdMyCRA
http://mohammadshakergtr.wordpress.com/

More Related Content

PDF
XNA L03–Models, Basic Effect and Animation
PDF
XNA L02–Basic Matrices and Transformations
PDF
XNA L04–Primitives, IndexBuffer and VertexBuffer
PDF
XNA L10–Shaders Part 1
PDF
XNA L11–Shaders Part 2
PDF
XNA L09–2D Graphics and Particle Engines
PDF
WPF L03-3D Rendering and 3D Animation
DOC
Learn Java 3D
XNA L03–Models, Basic Effect and Animation
XNA L02–Basic Matrices and Transformations
XNA L04–Primitives, IndexBuffer and VertexBuffer
XNA L10–Shaders Part 1
XNA L11–Shaders Part 2
XNA L09–2D Graphics and Particle Engines
WPF L03-3D Rendering and 3D Animation
Learn Java 3D

What's hot (20)

PDF
SA09 Realtime education
PPTX
Trident International Graphics Workshop 2014 2/5
PPT
Geometry Shader-based Bump Mapping Setup
PPTX
Computer Vision harris
PDF
Graphicsand animations devoxx2010 (1)
PPTX
Company of Heroes 2 (COH2) Rendering Technology: The cold facts of recreating...
PPTX
Maximizing performance of 3 d user generated assets in unity
PPTX
A practical intro to BabylonJS
PDF
Cg lab cse-v (1) (1)
DOC
Anschp34
DOCX
Computer graphics
PDF
Games 3 dl4-example
KEY
Artdm170 week12 user_interaction
PDF
10 linescan
PDF
Ch32 ssm
DOC
Java applet handouts
PDF
Computer graphics lab manual
PDF
Computer graphics lab manual
SA09 Realtime education
Trident International Graphics Workshop 2014 2/5
Geometry Shader-based Bump Mapping Setup
Computer Vision harris
Graphicsand animations devoxx2010 (1)
Company of Heroes 2 (COH2) Rendering Technology: The cold facts of recreating...
Maximizing performance of 3 d user generated assets in unity
A practical intro to BabylonJS
Cg lab cse-v (1) (1)
Anschp34
Computer graphics
Games 3 dl4-example
Artdm170 week12 user_interaction
10 linescan
Ch32 ssm
Java applet handouts
Computer graphics lab manual
Computer graphics lab manual
Ad

Similar to XNA L08–Amazing XNA Utilities (20)

PPTX
Trident International Graphics Workshop 2014 1/5
PPTX
Chapter ii(coding)
PDF
Introduction to Coding
PPTX
CHAPTER - 4 for software engineering (1).pptx
PDF
Lecture 2: C# Programming for VR application in Unity
PDF
Object-oriented Basics
PPT
Gdc09 Minigames
PPTX
Fact, Fiction, and FP
PPTX
Clean Code Development
PDF
IL2CPP: Debugging and Profiling
PPT
OOP v3
PDF
Visual sedimentation - IEEE VIS 2013 Atlanta
PPT
Virtual Reality Modeling Language
PDF
Getting Started with 3D Game Development on Nokia Series 40 Asha Phones
PPTX
Vrml Language (Virtual Reality)
PPT
Augmented Reality With FlarToolkit and Papervision3D
PPTX
UNIT_3-Two-Dimensional-Geometric-Transformations.pptx
PDF
Rainbow Over the Windows: More Colors Than You Could Expect
PDF
The Ring programming language version 1.8 book - Part 53 of 202
PDF
Design Patterns Reconsidered
Trident International Graphics Workshop 2014 1/5
Chapter ii(coding)
Introduction to Coding
CHAPTER - 4 for software engineering (1).pptx
Lecture 2: C# Programming for VR application in Unity
Object-oriented Basics
Gdc09 Minigames
Fact, Fiction, and FP
Clean Code Development
IL2CPP: Debugging and Profiling
OOP v3
Visual sedimentation - IEEE VIS 2013 Atlanta
Virtual Reality Modeling Language
Getting Started with 3D Game Development on Nokia Series 40 Asha Phones
Vrml Language (Virtual Reality)
Augmented Reality With FlarToolkit and Papervision3D
UNIT_3-Two-Dimensional-Geometric-Transformations.pptx
Rainbow Over the Windows: More Colors Than You Could Expect
The Ring programming language version 1.8 book - Part 53 of 202
Design Patterns Reconsidered
Ad

More from Mohammad Shaker (20)

PDF
12 Rules You Should to Know as a Syrian Graduate
PDF
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
PDF
Interaction Design L06 - Tricks with Psychology
PDF
Short, Matters, Love - Passioneers Event 2015
PDF
Unity L01 - Game Development
PDF
Android L07 - Touch, Screen and Wearables
PDF
Interaction Design L03 - Color
PDF
Interaction Design L05 - Typography
PDF
Interaction Design L04 - Materialise and Coupling
PDF
Android L05 - Storage
PDF
Android L04 - Notifications and Threading
PDF
Android L09 - Windows Phone and iOS
PDF
Interaction Design L01 - Mobile Constraints
PDF
Interaction Design L02 - Pragnanz and Grids
PDF
Android L10 - Stores and Gaming
PDF
Android L06 - Cloud / Parse
PDF
Android L08 - Google Maps and Utilities
PDF
Android L03 - Styles and Themes
PDF
Android L02 - Activities and Adapters
PDF
Android L01 - Warm Up
12 Rules You Should to Know as a Syrian Graduate
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Interaction Design L06 - Tricks with Psychology
Short, Matters, Love - Passioneers Event 2015
Unity L01 - Game Development
Android L07 - Touch, Screen and Wearables
Interaction Design L03 - Color
Interaction Design L05 - Typography
Interaction Design L04 - Materialise and Coupling
Android L05 - Storage
Android L04 - Notifications and Threading
Android L09 - Windows Phone and iOS
Interaction Design L01 - Mobile Constraints
Interaction Design L02 - Pragnanz and Grids
Android L10 - Stores and Gaming
Android L06 - Cloud / Parse
Android L08 - Google Maps and Utilities
Android L03 - Styles and Themes
Android L02 - Activities and Adapters
Android L01 - Warm Up

Recently uploaded (20)

PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
1. Introduction to Computer Programming.pptx
PDF
project resource management chapter-09.pdf
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Approach and Philosophy of On baking technology
PPTX
Tartificialntelligence_presentation.pptx
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Hybrid model detection and classification of lung cancer
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Getting Started with Data Integration: FME Form 101
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
cloud_computing_Infrastucture_as_cloud_p
Assigned Numbers - 2025 - Bluetooth® Document
1. Introduction to Computer Programming.pptx
project resource management chapter-09.pdf
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Approach and Philosophy of On baking technology
Tartificialntelligence_presentation.pptx
Univ-Connecticut-ChatGPT-Presentaion.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Hybrid model detection and classification of lung cancer
DP Operators-handbook-extract for the Mautical Institute
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Web App vs Mobile App What Should You Build First.pdf
Heart disease approach using modified random forest and particle swarm optimi...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Getting Started with Data Integration: FME Form 101
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
cloud_computing_Infrastucture_as_cloud_p

XNA L08–Amazing XNA Utilities

  • 1. Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 XNA Game Development L08 – Amazing XNA Utilities
  • 3. Collision Detection determines whether two objects overlap and therefore have collided in your virtual world.
  • 4. Collision Detection Accurate collision detection is fundamental to a solid game engine.
  • 6. Your cars would drive off the road, your people would walk through buildings, and your camera would travel through cement walls :D
  • 8. Collision Detection • Microsoft Book, Chapter 18, Page: 285
  • 9. Collision Detection • XNA has two main types for implementing collision detection
  • 10. Collision Detection • XNA has two main types for implementing collision detection – BoundingBox
  • 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
  • 18. Collision Detection • CONTAINMENT TYPE – BoundingBox – BoundingSphere
  • 19. Collision Detection • CONTAINMENT TYPE – BoundingBox – BoundingSphere
  • 20. Collision Detection • CONTAINMENT TYPE – BoundingBox – BoundingSphere
  • 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);
  • 27. Collision Detection • Different sphere groups for varying levels of efficiency and accuracy
  • 28. Collision Detection • Microsoft, Collision Detection, P: 290 - 304
  • 29. Check it out in “App1-CollisionDetection- Micosoft”
  • 30. Collision Detection Apress book, Chapter 13, Page 367
  • 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
  • 40. Collision Detection • Approximation A better way to combine both approaches
  • 45. Collision Detection In hierarchical model you would use both the large sphere, and the small spheres.
  • 47. Collision Detection Example in XNA private bool IsCollision(Model model1, Matrix world1, Model model2, Matrix world2) { for (int meshIndex1 = 0; meshIndex1 < model1.Meshes.Count; meshIndex1++) { BoundingSphere sphere1 = model1.Meshes[meshIndex1].BoundingSphere; sphere1 = sphere1.Transform(world1); for (int meshIndex2 = 0; meshIndex2 < model2.Meshes.Count; meshIndex2++) { BoundingSphere sphere2 = model2.Meshes[meshIndex2].BoundingSphere; sphere2 = sphere2.Transform(world2); if (sphere1.Intersects(sphere2)) return true; } } return false; }
  • 48. Collision Detection Example in XNA “App2-CollisionDetection-WebSite”
  • 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); }
  • 60. XNA With Windows Form App • Now, ctrl+F5 and u r good to go!
  • 61. 2D and 3D Combined
  • 62. 2D and 3D Combined
  • 63. 2D and 3D Combined
  • 64. 2D and 3D Combined
  • 65. 2D and 3D Combined
  • 66. 2D and 3D Combined
  • 67. 2D and 3D Combined Begin Method (SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix) Begin Method () Begin Method (SpriteSortMode, BlendState) Begin Method (SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState) Begin Method (SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect) sortMode Sprite drawing order. blendState Blending options. samplerState Texture sampling options. depthStencilState Depth and stencil options. rasterizerState Rasterization options. effect Effect state options. transformMatrix Transformation matrix for scale, rotate, translate options.
  • 68. 2D and 3D Combined • SpriteSortMode
  • 69. 2D and 3D Combined • BlendState • See more: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.blend.aspx
  • 70. 2D and 3D Combined • BlendState
  • 71. 2D and 3D Combined • BlendState
  • 72. 2D and 3D Combined • BlendState
  • 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
  • 77. Viewports & Split Screen Games
  • 78. Viewports & Split Screen Games • Base Project • “App1-Viewports”
  • 79. Viewports & Split Screen Games • Viewports
  • 80. Viewports & Split Screen Games • Viewports – Multiple ones So, What’s the idea is all about?!
  • 81. Viewports & Split Screen Games • Viewports – Multiple ones Multiple “Camera”s
  • 82. Viewports & Split Screen Games • Viewports – Multiple ones Multiple “LookAt()”s
  • 83. Viewports & Split Screen Games • Global Scope private Matrix topView = Matrix.CreateLookAt(new Vector3(0, 0, 4), new Vector3(0, 0, 0), new Vector3(0, 0.001f, 1f)); private Matrix frontView = Matrix.CreateLookAt(new Vector3(0, 4, 0), new Vector3(0, 0, 0), Vector3.UnitZ); private Matrix sideView = Matrix.CreateLookAt(new Vector3(4, 0, 0), new Vector3(0, 0, 0), Vector3.UnitZ); private Matrix perspectiveView = Matrix.CreateLookAt(new Vector3(4, 4, 4), new Vector3(0, 0, 0), Vector3.UnitZ); private Viewport topViewport; private Viewport sideViewport; private Viewport frontViewport; private Viewport perspectiveViewport;
  • 84. private Matrix topView = Matrix.CreateLookAt(new Vector3(0, 0, 4), new Vector3(0, 0, 0), new Vector3(0, 0.001f, 1f)); private Matrix frontView = Matrix.CreateLookAt(new Vector3(0, 4, 0), new Vector3(0, 0, 0), Vector3.UnitZ); private Matrix sideView = Matrix.CreateLookAt(new Vector3(4, 0, 0), new Vector3(0, 0, 0), Vector3.UnitZ); private Matrix perspectiveView = Matrix.CreateLookAt(new Vector3(4, 4, 4), new Vector3(0, 0, 0), Vector3.UnitZ); private Viewport topViewport; private Viewport sideViewport; private Viewport frontViewport; private Viewport perspectiveViewport; Matrix.CreateLookAt(CameraPosition, CameraTarget, CameraUp); Viewports & Split Screen Games
  • 86. • Initialize() protected override void Initialize() { base.Initialize(); int ViewportWidth = 200; int ViewportHeight = 200; Viewport defaultViewport = GraphicsDevice.Viewport; topViewport = defaultViewport; topViewport.X = 0; topViewport.Y = 0; topViewport.Width = ViewportWidth; topViewport.Height = ViewportHeight; topViewport.MinDepth = 0; topViewport.MaxDepth = 1; sideViewport = defaultViewport; sideViewport.X = ViewportWidth; sideViewport.Y = 0; sideViewport.Width = ViewportWidth; sideViewport.Height = ViewportHeight; sideViewport.MinDepth = 0; sideViewport.MaxDepth = 1; frontViewport = defaultViewport; frontViewport.X = 0; frontViewport.Y = ViewportHeight; frontViewport.Width = ViewportWidth; frontViewport.Height = ViewportHeight; frontViewport.MinDepth = 0; frontViewport.MaxDepth = 1; perspectiveViewport = defaultViewport; perspectiveViewport.X = ViewportWidth; perspectiveViewport.Y = ViewportHeight; perspectiveViewport.Width = ViewportWidth; perspectiveViewport.Height = ViewportHeight; perspectiveViewport.MinDepth = 0; perspectiveViewport.MaxDepth = 1; } Viewports & Split Screen Games
  • 87. Viewports & Split Screen Games • Initialize() protected override void Initialize() { base.Initialize(); int ViewportWidth = 200; int ViewportHeight = 200; Viewport defaultViewport = GraphicsDevice.Viewport; topViewport = defaultViewport; topViewport.X = 0; topViewport.Y = 0; topViewport.Width = ViewportWidth; topViewport.Height = ViewportHeight; topViewport.MinDepth = 0; topViewport.MaxDepth = 1; sideViewport = defaultViewport; sideViewport.X = ViewportWidth; sideViewport.Y = 0; sideViewport.Width = ViewportWidth; sideViewport.Height = ViewportHeight; sideViewport.MinDepth = 0; sideViewport.MaxDepth = 1; frontViewport = defaultViewport; frontViewport.X = 0; frontViewport.Y = ViewportHeight; frontViewport.Width = ViewportWidth; frontViewport.Height = ViewportHeight; frontViewport.MinDepth = 0; frontViewport.MaxDepth = 1; perspectiveViewport = defaultViewport; perspectiveViewport.X = ViewportWidth; perspectiveViewport.Y = ViewportHeight; perspectiveViewport.Width = ViewportWidth; perspectiveViewport.Height = ViewportHeight; perspectiveViewport.MinDepth = 0; perspectiveViewport.MaxDepth = 1; }
  • 88. • Initialize() protected override void Initialize() { base.Initialize(); int ViewportWidth = 200; int ViewportHeight = 200; Viewport defaultViewport = GraphicsDevice.Viewport; topViewport = defaultViewport; topViewport.X = 0; topViewport.Y = 0; topViewport.Width = ViewportWidth; topViewport.Height = ViewportHeight; topViewport.MinDepth = 0; topViewport.MaxDepth = 1; sideViewport = defaultViewport; sideViewport.X = ViewportWidth; sideViewport.Y = 0; sideViewport.Width = ViewportWidth; sideViewport.Height = ViewportHeight; sideViewport.MinDepth = 0; sideViewport.MaxDepth = 1; frontViewport = defaultViewport; frontViewport.X = 0; frontViewport.Y = ViewportHeight; frontViewport.Width = ViewportWidth; frontViewport.Height = ViewportHeight; frontViewport.MinDepth = 0; frontViewport.MaxDepth = 1; perspectiveViewport = defaultViewport; perspectiveViewport.X = ViewportWidth; perspectiveViewport.Y = ViewportHeight; perspectiveViewport.Width = ViewportWidth; perspectiveViewport.Height = ViewportHeight; perspectiveViewport.MinDepth = 0; perspectiveViewport.MaxDepth = 1; } Viewports & Split Screen Games
  • 89. • Initialize() protected override void Initialize() { base.Initialize(); int ViewportWidth = 200; int ViewportHeight = 200; Viewport defaultViewport = GraphicsDevice.Viewport; topViewport = defaultViewport; topViewport.X = 0; topViewport.Y = 0; topViewport.Width = ViewportWidth; topViewport.Height = ViewportHeight; topViewport.MinDepth = 0; topViewport.MaxDepth = 1; sideViewport = defaultViewport; sideViewport.X = ViewportWidth; sideViewport.Y = 0; sideViewport.Width = ViewportWidth; sideViewport.Height = ViewportHeight; sideViewport.MinDepth = 0; sideViewport.MaxDepth = 1; frontViewport = defaultViewport; frontViewport.X = 0; frontViewport.Y = ViewportHeight; frontViewport.Width = ViewportWidth; frontViewport.Height = ViewportHeight; frontViewport.MinDepth = 0; frontViewport.MaxDepth = 1; perspectiveViewport = defaultViewport; perspectiveViewport.X = ViewportWidth; perspectiveViewport.Y = ViewportHeight; perspectiveViewport.Width = ViewportWidth; perspectiveViewport.Height = ViewportHeight; perspectiveViewport.MinDepth = 0; perspectiveViewport.MaxDepth = 1; } Viewports & Split Screen Games
  • 90. Viewports & Split Screen Games
  • 91. Viewports & Split Screen Games
  • 92. Viewports & Split Screen Games
  • 93. Viewports & Split Screen Games
  • 94. Viewports & Split Screen Games
  • 95. Viewports & Split Screen Games
  • 96. Viewports & Split Screen Games • Draw()protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Viewport original = graphics.GraphicsDevice.Viewport; graphics.GraphicsDevice.Viewport = topViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, topView, projection); graphics.GraphicsDevice.Viewport = sideViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, sideView, projection); graphics.GraphicsDevice.Viewport = frontViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, frontView, projection); graphics.GraphicsDevice.Viewport = perspectiveViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, perspectiveView, projection); GraphicsDevice.Viewport = original; base.Draw(gameTime); }
  • 97. Viewports & Split Screen Games • Draw()protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Viewport original = graphics.GraphicsDevice.Viewport; graphics.GraphicsDevice.Viewport = topViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, topView, projection); graphics.GraphicsDevice.Viewport = sideViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, sideView, projection); graphics.GraphicsDevice.Viewport = frontViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, frontView, projection); graphics.GraphicsDevice.Viewport = perspectiveViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, perspectiveView, projection); GraphicsDevice.Viewport = original; base.Draw(gameTime); }
  • 98. Viewports & Split Screen Games • Draw()protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Viewport original = graphics.GraphicsDevice.Viewport; graphics.GraphicsDevice.Viewport = topViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, topView, projection); graphics.GraphicsDevice.Viewport = sideViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, sideView, projection); graphics.GraphicsDevice.Viewport = frontViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, frontView, projection); graphics.GraphicsDevice.Viewport = perspectiveViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, perspectiveView, projection); GraphicsDevice.Viewport = original; base.Draw(gameTime); }
  • 99. Viewports & Split Screen Games • Draw()protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Viewport original = graphics.GraphicsDevice.Viewport; graphics.GraphicsDevice.Viewport = topViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, topView, projection); graphics.GraphicsDevice.Viewport = sideViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, sideView, projection); graphics.GraphicsDevice.Viewport = frontViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, frontView, projection); graphics.GraphicsDevice.Viewport = perspectiveViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, perspectiveView, projection); GraphicsDevice.Viewport = original; base.Draw(gameTime); }
  • 100. Viewports & Split Screen Games • Draw()protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Viewport original = graphics.GraphicsDevice.Viewport; graphics.GraphicsDevice.Viewport = topViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, topView, projection); graphics.GraphicsDevice.Viewport = sideViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, sideView, projection); graphics.GraphicsDevice.Viewport = frontViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, frontView, projection); graphics.GraphicsDevice.Viewport = perspectiveViewport; //GraphicsDevice.Clear(Color.Black); DrawModel(model, world, perspectiveView, projection); GraphicsDevice.Viewport = original; base.Draw(gameTime); }
  • 101. Viewports & Split Screen Games • “App2-ViewportsFinished”
  • 105. Picking Objects public Ray CalculateRay(Vector2 mouseLocation, Matrix view, Matrix projection, Viewport viewport) { Vector3 nearPoint = viewport.Unproject( new Vector3(mouseLocation.X, mouseLocation.Y, 0.0f), projection, view, Matrix.Identity); Vector3 farPoint = viewport.Unproject( new Vector3(mouseLocation.X, mouseLocation.Y, 1.0f), projection, view, Matrix.Identity); Vector3 direction = farPoint - nearPoint; direction.Normalize(); return new Ray(nearPoint, direction); }
  • 106. Picking Objects public bool Intersects(Vector2 mouseLocation, Model model, Matrix world, Matrix view, Matrix projection, Viewport viewport) { for (int index = 0; index < model.Meshes.Count; index++) { BoundingSphere sphere = model.Meshes[index].BoundingSphere; sphere = sphere.Transform(world); float? distance = IntersectDistance(sphere, mouseLocation, view, projection, viewport); if (distance != null) { return true; } } return false; }
  • 107. Picking Objects public float? IntersectDistance(BoundingSphere sphere, Vector2 mouseLocation, Matrix view, Matrix projection, Viewport viewport) { Ray mouseRay = CalculateRay(mouseLocation, view, projection, viewport); return mouseRay.Intersects(sphere); }
  • 109. Take a Look on my other courses @ http://www.slideshare.net/ZGTRZGTR Available courses to the date of this slide: