Upload
mohammad-shaker
View
330
Download
2
Tags:
Embed Size (px)
Citation preview
Mohammad Shakermohammadshaker.com
@ZGTRShaker2011, 2012, 2013, 2014
XNA Game DevelopmentL08 – Amazing XNA Utilities
Collision Detection
Collision Detectiondetermines whether two objects overlap and therefore have
collided in your virtual world.
Collision DetectionAccurate collision detection is fundamental
to a solid game engine.
Your cars would drive off the road, your people would walk through buildings, and your camera
would travel through cement walls :D
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 DetectionBOUNDING 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 DetectionBOUNDING 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 DetectionApress 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 SYSTEMSDon’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 Detectionhierarchical modeling
Collision Detection
Collision Detection
Collision Detection
Collision DetectionIn hierarchical model you would use both the
large sphere, and the small spheres.
Collision DetectionQuad 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 AppFor 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)
sortModeSprite drawing order.
blendStateBlending options.
samplerStateTexture sampling options.
depthStencilStateDepth and stencil options.
rasterizerStateRasterization options.
effectEffect state options.
transformMatrixTransformation 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
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/