Cross-platform physics games
Using MonoGame and Farseer Physics
Rune Andreas Grimstad @runegriCapgemini Norge
What to expect
• Lots of code!
• Intoduction to MonoGame and Farseer
• Playable demos
• Code on GitHub for you to play with :-)
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
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...
Why games programming?
• Not my job!
• Fun
• Learning
• Becoming a billionaire after creating the next Angry Birds Flappy Bird!
MonoGame
• Open Source implementation of XNA 4
• Runs everywhere
• C# / .Net
• Xamarin
Fez
Bastion
Draw a Stickman Epic
Transistor
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…
Enough background!
How MonoGame works - The Game Loop
Initialize and load content
Update game state
Draw everything
Unload content
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
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
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
Demo 1
• The basics
• Draw a sprite on screen• Content• Animation• Camera, pan & zoom• Input
Demo 1 – What did we learn?• Game loop• Content• SpriteBatch• Texture atlas• Camera• Input
All that from this little guy!
But there is more
Farseer physics
Farseer Physics Engine is a collision detection system
with realistic physics responses.
Farseer physics
• 2D Physics simulation• Collision detection• Objects with any shape• Friction and restitution• Joints• Gravity... and more
Farseer physics – getting it
• NuGet
• Download from https://farseerphysics.codeplex.com/
• Build from sources• The easiest way to combine with MonoGame?
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
Demo 2 – more Bubble Bobble!• A simple platformer
• Simulates a world• Sprites are bodies in the world• Collision detection• Movement
Bubbles!
Demo 2 – what did we learn?• Using Farseer!• Worlds• Bodies• World and display coordinates
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
Demo 3 – Exploring the cave• Stupid game with lots of physics
• Polygon objects• Complex objects• Disabling gravity• Attaching objects
Polygon shapes
Turn this... ... into this
MAGIC!
Polygon shapes
Complex objects
• Add many shapes to one body
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!
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
Finally finally
• Find my code samples on GitHub• github.com/runegri
• And my slides on SlideShare• slideshare.net/runegri
Get in touch
• I’m here for the rest of the conference
• Twitter: @runegri• Email: [email protected]