cross platform physics games - ndc 2014

37
Cross-platform physics games Using MonoGame and Farseer Physics Rune Andreas Grimstad @runegri Capgemini Norge

Upload: runegri

Post on 25-Dec-2014

613 views

Category:

Technology


2 download

DESCRIPTION

My slides from NDC 2014 regarding how to build games using MonoGame and Farseer Physics.

TRANSCRIPT

Page 1: Cross platform physics games - NDC 2014

Cross-platform physics games

Using MonoGame and Farseer Physics

Rune Andreas Grimstad @runegriCapgemini Norge

Page 2: Cross platform physics games - NDC 2014

What to expect

• Lots of code!

• Intoduction to MonoGame and Farseer

• Playable demos

• Code on GitHub for you to play with :-)

Page 3: Cross platform physics games - NDC 2014

What NOT to expect

• Deep dive into the features• That would take days...

• 3D games• Visit Richard Garside’s talk tomorrow at 11:40

• A complete game

Page 4: Cross platform physics games - NDC 2014

Who am I?

• Rune Andreas Grimstad

• Lives in Trondheim

• Works for Cagemini

• Focus on .Net and anything mobile

• Programming since I was 8 years old...

Page 5: Cross platform physics games - NDC 2014

Why games programming?

• Not my job!

• Fun

• Learning

• Becoming a billionaire after creating the next Angry Birds Flappy Bird!

Page 6: Cross platform physics games - NDC 2014
Page 7: Cross platform physics games - NDC 2014

MonoGame

• Open Source implementation of XNA 4

• Runs everywhere

• C# / .Net

• Xamarin

Page 8: Cross platform physics games - NDC 2014

Fez

Page 9: Cross platform physics games - NDC 2014

Bastion

Page 10: Cross platform physics games - NDC 2014

Draw a Stickman Epic

Page 11: Cross platform physics games - NDC 2014

Transistor

Page 12: Cross platform physics games - NDC 2014

MonoGame – getting it

• Download from monogame.net

• Clone from GitHub

• NuGet

Page 13: Cross platform physics games - NDC 2014

MonoGame – features

• Supports all features of XNA4 (more or less)• 2D• 3D• Content pipeline• Input• Sound and music• Networking

• It’s a bare-bones framework…

Page 14: Cross platform physics games - NDC 2014

Enough background!

Page 15: Cross platform physics games - NDC 2014

How MonoGame works - The Game Loop

Initialize and load content

Update game state

Draw everything

Unload content

Page 16: Cross platform physics games - NDC 2014

How MonoGame works - Creating content• Most file types are supported

• Either use an XNA content project• Requires XNA Game Studio, so Windows only• Create a MonoGame Content project

• Or use the MonoGame Content Builder• Runs everywhere• Not 100% done yet, but getting close

Page 17: Cross platform physics games - NDC 2014

How MonoGame works - Loading content• Using the ContentManager.Load method• Usually Content.Load<Texture2D>("spaceship");

• You can also load other resources this way• SpriteFont, SoundEffect, Song, and so on

• Store the files in the Content folder in your project• Build Action – Content • Copy to Output Directory – Copy if newer

Page 18: Cross platform physics games - NDC 2014

How MonoGame works - Drawing stuff• Use the SpriteBatch class

• Start with SpriteBatch.Begin• Do your SpriteBatch.Draw calls• End with SpriteBatch.End

• Try to limit how many SpriteBatches you use

Page 19: Cross platform physics games - NDC 2014

Demo 1

• The basics

• Draw a sprite on screen• Content• Animation• Camera, pan & zoom• Input

Page 20: Cross platform physics games - NDC 2014

Demo 1 – What did we learn?• Game loop• Content• SpriteBatch• Texture atlas• Camera• Input

Page 21: Cross platform physics games - NDC 2014

All that from this little guy!

Page 22: Cross platform physics games - NDC 2014

But there is more

Page 23: Cross platform physics games - NDC 2014

Farseer physics

Farseer Physics Engine is a collision detection system

with realistic physics responses.

Page 24: Cross platform physics games - NDC 2014

Farseer physics

• 2D Physics simulation• Collision detection• Objects with any shape• Friction and restitution• Joints• Gravity... and more

Page 25: Cross platform physics games - NDC 2014

Farseer physics – getting it

• NuGet

• Download from https://farseerphysics.codeplex.com/

• Build from sources• The easiest way to combine with MonoGame?

Page 26: Cross platform physics games - NDC 2014

One thing to be aware of

• Farseer uses the KMS system (Kilo, Meters, Seconds)

• Use the ConvertUnits class

• Init with SetDisplayUnitToSimUnitRatio

• Use ToDisplayUnits and ToSimUnits

Your screen

Display coordinates: 1920 x 1080 pixelsFarseer coordinates: 19.2 x 10.8 meters

Page 27: Cross platform physics games - NDC 2014

Demo 2 – more Bubble Bobble!• A simple platformer

• Simulates a world• Sprites are bodies in the world• Collision detection• Movement

Bubbles!

Page 28: Cross platform physics games - NDC 2014

Demo 2 – what did we learn?• Using Farseer!• Worlds• Bodies• World and display coordinates

Page 29: Cross platform physics games - NDC 2014

More features of Farseer Physics• Polygon shapes• Turning an image into a polygon in the game world

• Complex objects• Combining many shapes into a single object

• Joints• Attaching bodies to each other

Page 30: Cross platform physics games - NDC 2014

Demo 3 – Exploring the cave• Stupid game with lots of physics

• Polygon objects• Complex objects• Disabling gravity• Attaching objects

Page 31: Cross platform physics games - NDC 2014

Polygon shapes

Turn this... ... into this

MAGIC!

Page 32: Cross platform physics games - NDC 2014

Polygon shapes

Page 33: Cross platform physics games - NDC 2014

Complex objects

• Add many shapes to one body

Page 34: Cross platform physics games - NDC 2014

Complex objects

• Join bodies using joints

• Revolute joints can be used to create a chain or rope• Use the RopeJoint to set the length of the rope

• The possibilities are endless!

Page 35: Cross platform physics games - NDC 2014

Finally

• There is much more to MonoGame!

• 3D – Richard Garside will talk about this tomorrow!• Pixel shaders and effects• Sound and music

• Use the SoundEffect and Song classes

• Input beyond the keyboard• Mouse • Touch • Joystick

Page 36: Cross platform physics games - NDC 2014

Finally finally

• Find my code samples on GitHub• github.com/runegri

• And my slides on SlideShare• slideshare.net/runegri

Page 37: Cross platform physics games - NDC 2014

Get in touch

• I’m here for the rest of the conference

• Twitter: @runegri• Email: [email protected]