Skip to content

Instantly share code, notes, and snippets.

@Clancey
Created March 17, 2011 17:21
Show Gist options
  • Save Clancey/874741 to your computer and use it in GitHub Desktop.
Save Clancey/874741 to your computer and use it in GitHub Desktop.
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
namespace FarseerPhysics.HelloWorld
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
private GraphicsDeviceManager _graphics;
private SpriteBatch _batch;
private SpriteFont _font;
Texture2D texture, caracter;
private Color _alphaColor = Color.Gray;
bool jumpReleased;
private World _world;
private Body _circleBody;
private Body _groundBody;
private Texture2D _circleSprite;
private Texture2D _groundSprite;
// Simple camera controls
private Matrix _view;
private Vector2 _cameraPosition;
private Vector2 _screenCenter;
// Farseer expects objects to be scaled to MKS (meters, kilos, seconds)
// 1 meters equals 64 pixels here
// (Objects should be scaled to be between 0.1 and 10 meters in size)
private const float MeterInPixels = 64f;
public Game1 ()
{
_alphaColor.A = 100;
_graphics = new GraphicsDeviceManager (this);
//_graphics.PreferredBackBufferWidth = 800;
//_graphics.PreferredBackBufferHeight = 480;
Content.RootDirectory = "Content";
_world = new World (new Vector2 (0, 20));
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent ()
{
// Initialize camera controls
_view = Matrix.Identity;
_cameraPosition = Vector2.Zero;
_screenCenter = new Vector2 (_graphics.GraphicsDevice.Viewport.Width / 2f, _graphics.GraphicsDevice.Viewport.Height / 2f);
_batch = new SpriteBatch (_graphics.GraphicsDevice);
_font = Content.Load<SpriteFont> ("SpriteFont1");
// Load sprites
_circleSprite = Content.Load<Texture2D> ("circleSprite");
// 96px x 96px => 1.5m x 1.5m
_groundSprite = Content.Load<Texture2D> ("groundSprite");
// 512px x 64px => 8m x 1m
texture = Content.Load<Texture2D> ("gamepad");
/* Circle */
// Convert screen center from pixels to meters
Vector2 circlePosition = (_screenCenter / MeterInPixels) + new Vector2 (0, -1.5f);
// Create the circle fixture
_circleBody = BodyFactory.CreateCircle (_world, 96f / (2f * MeterInPixels), 1f, circlePosition);
_circleBody.BodyType = BodyType.Dynamic;
// Give it some bounce and friction
_circleBody.Restitution = 0.3f;
_circleBody.Friction = 0.5f;
/* Ground */
Vector2 groundPosition = (_screenCenter / MeterInPixels) + new Vector2 (0, 1.25f);
// Create the ground fixture
_groundBody = BodyFactory.CreateRectangle (_world, 512f / MeterInPixels, 64f / MeterInPixels, 1f, groundPosition);
//_groundBody.Rotation = .1f;
_groundBody.IsStatic = true;
_groundBody.Restitution = 0.3f;
_groundBody.Friction = 0.5f;
// Set the virtual GamePad
ButtonDefinition BButton = new ButtonDefinition ();
BButton.Texture = texture;
BButton.Position = new Vector2 (200, 350);
BButton.Type = Buttons.B;
BButton.TextureRect = new Rectangle (72, 77, 36, 36);
ButtonDefinition AButton = new ButtonDefinition ();
AButton.Texture = texture;
AButton.Position = new Vector2 (150, 350);
AButton.Type = Buttons.A;
AButton.TextureRect = new Rectangle (73, 114, 36, 36);
GamePad.ButtonsDefinitions.Add (BButton);
GamePad.ButtonsDefinitions.Add (AButton);
ThumbStickDefinition thumbStick = new ThumbStickDefinition ();
thumbStick.Position = new Vector2 (180, 400);
thumbStick.Texture = texture;
thumbStick.TextureRect = new Rectangle (2, 2, 68, 68);
GamePad.LeftThumbStickDefinition = thumbStick;
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update (GameTime gameTime)
{
//Use the joystick to move the camera;
GamePadState gamepadStatus = GamePad.GetState (PlayerIndex.One);
_cameraPosition.Y += (int)(gamepadStatus.ThumbSticks.Left.Y * 4);
_cameraPosition.X += (int)(gamepadStatus.ThumbSticks.Left.X * -4);
_view = Matrix.CreateTranslation (new Vector3 (_cameraPosition - _screenCenter, 0f)) * Matrix.CreateTranslation (new Vector3 (_screenCenter, 0f));
//Move based on Accelerometer
var accel = Accelerometer.GetState ();
if (Math.Abs (accel.Acceleration.X) > 0.10f)
{
if (accel.Acceleration.X > 0.0f)
_circleBody.ApplyTorque (accel.Acceleration.X * 4);
else
_circleBody.ApplyTorque (accel.Acceleration.X * -4);
}
//Jump
if (GamePad.GetState (PlayerIndex.One).Buttons.A == ButtonState.Pressed && jumpReleased)
_circleBody.ApplyLinearImpulse (new Vector2 (0, -10));
//Reset
if (GamePad.GetState (PlayerIndex.One).Buttons.B == ButtonState.Pressed)
{
_circleBody.Position = (_screenCenter / MeterInPixels) + new Vector2 (0, -1.5f);
_circleBody.ResetDynamics ();
}
jumpReleased = gamepadStatus.IsButtonUp (Buttons.A);
//We update the world
_world.Step ((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f);
base.Update (gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw (GameTime gameTime)
{
GraphicsDevice.Clear (Color.CornflowerBlue);
/* Circle position and rotation */
// Convert physics position (meters) to screen coordinates (pixels)
Vector2 circlePos = _circleBody.Position * MeterInPixels;
float circleRotation = _circleBody.Rotation;
/* Ground position and origin */
Vector2 groundPos = _groundBody.Position * MeterInPixels;
Vector2 groundOrigin = new Vector2 (_groundSprite.Width / 2f, _groundSprite.Height / 2f);
// Align sprite center to body position
Vector2 circleOrigin = new Vector2 (_circleSprite.Width / 2f, _circleSprite.Height / 2f);
_batch.Begin (SpriteSortMode.Deferred, SpriteBlendMode.AlphaBlend, SaveStateMode.None, _view);
//Draw circle
_batch.Draw (_circleSprite, circlePos, null, Color.White, circleRotation, circleOrigin, 1f, SpriteEffects.None, 0f);
//Draw ground
_batch.Draw (_groundSprite, groundPos, null, Color.White, _groundBody.Rotation, groundOrigin, 1f, SpriteEffects.None, 0f);
_batch.End ();
_batch.Begin ();
// Display instructions
_batch.DrawString (_font, circlePos.ToString (), Vector2.One, Color.Black);
GamePad.Draw (gameTime, _batch);
_batch.End ();
base.Draw (gameTime);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment