110
Mohammad Shaker mohammadshaker.com @ZGTRShaker 2011, 2012, 2013, 2014 XNA Game Development L08 – Amazing XNA Utilities

XNA L08–Amazing XNA Utilities

Embed Size (px)

Citation preview

Page 1: XNA L08–Amazing XNA Utilities

Mohammad Shakermohammadshaker.com

@ZGTRShaker2011, 2012, 2013, 2014

XNA Game DevelopmentL08 – Amazing XNA Utilities

Page 2: XNA L08–Amazing XNA Utilities

Collision Detection

Page 3: XNA L08–Amazing XNA Utilities

Collision Detectiondetermines whether two objects overlap and therefore have

collided in your virtual world.

Page 4: XNA L08–Amazing XNA Utilities

Collision DetectionAccurate collision detection is fundamental

to a solid game engine.

Page 5: XNA L08–Amazing XNA Utilities
Page 6: 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

Page 7: XNA L08–Amazing XNA Utilities
Page 8: XNA L08–Amazing XNA Utilities

Collision Detection

• Microsoft Book, Chapter 18, Page: 285

Page 9: XNA L08–Amazing XNA Utilities

Collision Detection

• XNA has two main types for implementing collision detection

Page 10: XNA L08–Amazing XNA Utilities

Collision Detection

• XNA has two main types for implementing collision detection

– BoundingBox

Page 11: XNA L08–Amazing XNA Utilities

Collision Detection

• XNA has two main types for implementing collision detection

– BoundingBox

– BoundingSphere

Page 12: XNA L08–Amazing XNA Utilities

Collision Detection

• XNA has two main types for implementing collision detection

– BoundingBox

• a box will better suit a rectangular high-rise building

– BoundingSphere

Page 13: XNA L08–Amazing XNA Utilities

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

Page 14: XNA L08–Amazing XNA Utilities

Collision Detection

– BoundingSphere

Page 15: XNA L08–Amazing XNA Utilities

Collision Detection

– BoundingSphere

Page 16: XNA L08–Amazing XNA Utilities

Collision Detection

– BoundingSphere

Page 17: XNA L08–Amazing XNA Utilities

Collision Detection

– BoundingSphere

Page 18: XNA L08–Amazing XNA Utilities

Collision Detection

• CONTAINMENT TYPE

– BoundingBox

– BoundingSphere

Page 19: XNA L08–Amazing XNA Utilities

Collision Detection

• CONTAINMENT TYPE

– BoundingBox

– BoundingSphere

Page 20: XNA L08–Amazing XNA Utilities

Collision Detection

• CONTAINMENT TYPE

– BoundingBox

– BoundingSphere

Page 21: XNA L08–Amazing XNA Utilities

Collision DetectionBOUNDING BOX

Page 22: XNA L08–Amazing XNA Utilities

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);

Page 23: XNA L08–Amazing XNA Utilities

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);

Page 24: XNA L08–Amazing XNA Utilities

Collision DetectionBOUNDING SPHERE

Page 25: XNA L08–Amazing XNA Utilities

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);

Page 26: XNA L08–Amazing XNA Utilities

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);

Page 27: XNA L08–Amazing XNA Utilities

Collision Detection

• Different sphere groups for varying levels of efficiency and accuracy

Page 28: XNA L08–Amazing XNA Utilities

Collision Detection

• Microsoft, Collision Detection, P: 290 - 304

Page 29: XNA L08–Amazing XNA Utilities

Check it out in “App1-CollisionDetection-

Micosoft”

Page 30: XNA L08–Amazing XNA Utilities

Collision DetectionApress book, Chapter 13, Page 367

Page 31: XNA L08–Amazing XNA Utilities

Collision Detection

Page 32: XNA L08–Amazing XNA Utilities

Collision Detection

• Also, because the default model processor doesn’t generate a bounding box

(AABB) volume, we need to generate one for the model

Page 33: XNA L08–Amazing XNA Utilities

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!

Page 34: XNA L08–Amazing XNA Utilities

Collision Detection

• Usage of bounding spheres is emphasized because spheres are easier to update

and transform than bounding boxes

Page 35: XNA L08–Amazing XNA Utilities

EARLY WARNING SYSTEMSDon’t check a 200 miles sphere collision with you!

Rbwhitaker web site: http://rbwhitaker.wikidot.com/collision-detection

Page 36: XNA L08–Amazing XNA Utilities

Collision Detection

• Approximation

Page 37: XNA L08–Amazing XNA Utilities

Collision Detection

• Approximation

Page 38: XNA L08–Amazing XNA Utilities

Collision Detection

• Approximation

Page 39: XNA L08–Amazing XNA Utilities

Collision Detection

• Approximation

When to use What?

Page 40: XNA L08–Amazing XNA Utilities

Collision Detection

• Approximation

A better way to combine both approaches

Page 41: XNA L08–Amazing XNA Utilities

Collision Detectionhierarchical modeling

Page 42: XNA L08–Amazing XNA Utilities

Collision Detection

Page 43: XNA L08–Amazing XNA Utilities

Collision Detection

Page 44: XNA L08–Amazing XNA Utilities

Collision Detection

Page 45: XNA L08–Amazing XNA Utilities

Collision DetectionIn hierarchical model you would use both the

large sphere, and the small spheres.

Page 46: XNA L08–Amazing XNA Utilities

Collision DetectionQuad Tree Approach

Page 47: XNA L08–Amazing XNA Utilities

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;

}

Page 48: XNA L08–Amazing XNA Utilities

Collision Detection Example in XNA

“App2-CollisionDetection-WebSite”

Page 49: XNA L08–Amazing XNA Utilities

XNA With Windows Form App/ WPF AppFor more info, go to:

http://www.codeproject.com/KB/game/XNA_And_Beyond.aspx

Page 50: XNA L08–Amazing XNA Utilities

XNA With Windows Form App

• 2 WAYS!

– 2D Graphics (Professional)

– Windows Forms (Ordinary)

Page 51: XNA L08–Amazing XNA Utilities

XNA With Windows Form App

• 2 WAYS!

– 2D Graphics (Professional)

• See the appendix for full tutorial pdf

– Windows Forms (Ordinary)

Page 52: XNA L08–Amazing XNA Utilities

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

Page 53: XNA L08–Amazing XNA Utilities

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

Page 54: XNA L08–Amazing XNA Utilities

XNA With Windows Form App

• Windows Forms! (See the template project in appendix)

Page 55: XNA L08–Amazing XNA Utilities

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;

}

}

Page 56: XNA L08–Amazing XNA Utilities

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;

Page 57: XNA L08–Amazing XNA Utilities

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();

}

Page 58: XNA L08–Amazing XNA Utilities

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();

}

Page 59: XNA L08–Amazing XNA Utilities

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);

}

Page 60: XNA L08–Amazing XNA Utilities

XNA With Windows Form App

• Now, ctrl+F5 and u r good to go!

Page 61: XNA L08–Amazing XNA Utilities

2D and 3D Combined

Page 62: XNA L08–Amazing XNA Utilities

2D and 3D Combined

Page 63: XNA L08–Amazing XNA Utilities

2D and 3D Combined

Page 64: XNA L08–Amazing XNA Utilities

2D and 3D Combined

Page 65: XNA L08–Amazing XNA Utilities

2D and 3D Combined

Page 66: XNA L08–Amazing XNA Utilities

2D and 3D Combined

Page 67: XNA L08–Amazing XNA Utilities

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.

Page 68: XNA L08–Amazing XNA Utilities

2D and 3D Combined

• SpriteSortMode

Page 69: XNA L08–Amazing XNA Utilities

2D and 3D Combined

• BlendState

• See more: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.blend.aspx

Page 70: XNA L08–Amazing XNA Utilities

2D and 3D Combined

• BlendState

Page 71: XNA L08–Amazing XNA Utilities

2D and 3D Combined

• BlendState

Page 72: XNA L08–Amazing XNA Utilities

2D and 3D Combined

• BlendState

Page 73: XNA L08–Amazing XNA Utilities

2D and 3D Combined

spriteBatch.Begin();

spriteBatch.Begin(SpriteSortMode.Immediate,

BlendState.Opaque);

• finally! change SpriteBatch From

• To

Page 74: XNA L08–Amazing XNA Utilities

2D and 3D Combined

spriteBatch.Begin();

spriteBatch.Begin(SpriteSortMode.Immediate,

BlendState.Opaque);

• finally! change SpriteBatch From

• To

Page 75: XNA L08–Amazing XNA Utilities

2D and 3D Combined

spriteBatch.Begin();

spriteBatch.Begin(SpriteSortMode.Immediate,

BlendState.AlphaBlend);

• finally! change SpriteBatch From

• To

Page 76: XNA L08–Amazing XNA Utilities

2D and 3D Combined

spriteBatch.Begin();

spriteBatch.Begin(SpriteSortMode.BackToFront,

BlendState.AlphaBlend);

• finally! change SpriteBatch From

• To

Page 77: XNA L08–Amazing XNA Utilities

Viewports & Split Screen Games

Page 78: XNA L08–Amazing XNA Utilities

Viewports & Split Screen Games

• Base Project

• “App1-Viewports”

Page 79: XNA L08–Amazing XNA Utilities

Viewports & Split Screen Games

• Viewports

Page 80: XNA L08–Amazing XNA Utilities

Viewports & Split Screen Games

• Viewports – Multiple ones

So, What’s the idea is all about?!

Page 81: XNA L08–Amazing XNA Utilities

Viewports & Split Screen Games

• Viewports – Multiple ones

Multiple “Camera”s

Page 82: XNA L08–Amazing XNA Utilities

Viewports & Split Screen Games

• Viewports – Multiple ones

Multiple “LookAt()”s

Page 83: XNA L08–Amazing XNA Utilities

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;

Page 84: XNA L08–Amazing XNA Utilities

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

Page 85: XNA L08–Amazing XNA Utilities

LookAt()

Page 86: XNA L08–Amazing XNA Utilities

• 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

Page 87: XNA L08–Amazing XNA Utilities

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;

}

Page 88: XNA L08–Amazing XNA Utilities

• 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

Page 89: XNA L08–Amazing XNA Utilities

• 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

Page 90: XNA L08–Amazing XNA Utilities

Viewports & Split Screen Games

Page 91: XNA L08–Amazing XNA Utilities

Viewports & Split Screen Games

Page 92: XNA L08–Amazing XNA Utilities

Viewports & Split Screen Games

Page 93: XNA L08–Amazing XNA Utilities

Viewports & Split Screen Games

Page 94: XNA L08–Amazing XNA Utilities

Viewports & Split Screen Games

Page 95: XNA L08–Amazing XNA Utilities

Viewports & Split Screen Games

Page 96: XNA L08–Amazing XNA Utilities

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);

}

Page 97: XNA L08–Amazing XNA Utilities

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);

}

Page 98: XNA L08–Amazing XNA Utilities

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);

}

Page 99: XNA L08–Amazing XNA Utilities

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);

}

Page 100: XNA L08–Amazing XNA Utilities

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);

}

Page 101: XNA L08–Amazing XNA Utilities

Viewports & Split Screen Games

• “App2-ViewportsFinished”

Page 102: XNA L08–Amazing XNA Utilities

Picking Objects

Page 103: XNA L08–Amazing XNA Utilities

Picking Objects

• How to pick?!

Page 104: XNA L08–Amazing XNA Utilities

Picking Objects

• How to pick?!

Page 105: XNA L08–Amazing XNA Utilities

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);

}

Page 106: XNA L08–Amazing XNA Utilities

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;

}

Page 107: XNA L08–Amazing XNA Utilities

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);

}

Page 108: XNA L08–Amazing XNA Utilities

Picking Objects

• “App-Picking”

Page 109: XNA L08–Amazing XNA Utilities

Take a Look on my other courses @ http://www.slideshare.net/ZGTRZGTR

Available courses to the date of this slide:

Page 110: XNA L08–Amazing XNA Utilities

http://www.mohammadshaker.com

[email protected]

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/