public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private Texture2D simpleBackground;
private Texture2D wheelTexture;
private Texture2D bodyTexture;
private World physicsWorld;
private Body FrontWheelBody;
private Body BackWheelBody;
private Body boardBody;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
graphics.IsFullScreen = true;
graphics.SupportedOrientations = DisplayOrientation.LandscapeRight;
graphics.ApplyChanges();
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
physicsWorld = new World(new Vector2(0,10),true);
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
simpleBackground = Content.Load<Texture2D>("carbackground");
wheelTexture = Content.Load<Texture2D>("wheelCopy");
bodyTexture = Content.Load<Texture2D>("board");
CreateGround();
CreateCar();
}
private RevoluteJoint frontWheelJoint;
private RevoluteJoint backWheelJoint;
private void CreateCar()
{
var bodyDef = new BodyDef {type = BodyType.Dynamic};
var wheelShape = new CircleShape {_radius = wheelTexture.Width/2f*0.01f};
var wheelFixture = new FixtureDef {shape = wheelShape, density =1f,restitution=.2f,friction=.5f};
FrontWheelBody = physicsWorld.CreateBody(bodyDef);
BackWheelBody = physicsWorld.CreateBody(bodyDef);
FrontWheelBody.Position = new Vector2(2.9f, 1.41f);
BackWheelBody.Position = new Vector2(3.9f, 1.41f);
FrontWheelBody.CreateFixture(wheelFixture);
BackWheelBody.CreateFixture(wheelFixture);
boardBody = physicsWorld.CreateBody(bodyDef);
var bodyShape = new PolygonShape();
bodyShape.SetAsBox(bodyTexture.Width/2f*0.01f, bodyTexture.Height/2f*0.01f);
var bodyFix = new FixtureDef() {friction = 1.0f, shape = bodyShape, density = 1.0f, restitution = .1f};
boardBody.Position = new Vector2(3.4f, 1.41f);
boardBody.CreateFixture(bodyFix);
var rj = new RevoluteJointDef();
rj.Initialize(boardBody, FrontWheelBody, FrontWheelBody.GetWorldCenter());
rj.enableMotor = true;
rj.maxMotorTorque = .1f;
var rj2 = new RevoluteJointDef();
rj2.Initialize(boardBody, BackWheelBody, BackWheelBody.GetWorldCenter());
rj2.enableMotor = true;
rj2.maxMotorTorque = .1f;
frontWheelJoint = (RevoluteJoint) physicsWorld.CreateJoint(rj);
backWheelJoint = (RevoluteJoint)physicsWorld.CreateJoint(rj2);
}
private void CreateGround()
{
var bodyDef = new BodyDef();
bodyDef.type = BodyType.Static;
var bodyShape = new PolygonShape();
var bodyFix = new FixtureDef();
bodyFix.density = 0.0f;
bodyFix.friction = 1.0f;
var topLeft = physicsWorld.CreateBody(bodyDef);
bodyShape.SetAsEdge(new Vector2(0, 4.0f), new Vector2(2.0f, 4.0f));
bodyFix.shape = bodyShape;
topLeft.CreateFixture(bodyFix);
var sideLeft = physicsWorld.CreateBody(bodyDef);
bodyShape.SetAsEdge(new Vector2(2.0f,4.0f), new Vector2(2.0f,4.8f));
bodyFix.shape = bodyShape;
sideLeft.CreateFixture(bodyFix);
var ground = physicsWorld.CreateBody(bodyDef);
bodyShape.SetAsEdge(new Vector2(2.0f,4.6f), new Vector2(6.0f,4.6f));
bodyFix.shape = bodyShape;
ground.CreateFixture(bodyFix);
var topRight = physicsWorld.CreateBody(bodyDef);
bodyShape.SetAsEdge(new Vector2(6.0f,4.0f), new Vector2(6.0f,4.8f));
bodyFix.shape = bodyShape;
topRight.CreateFixture(bodyFix);
var sideRight = physicsWorld.CreateBody(bodyDef);
bodyShape.SetAsEdge(new Vector2(6.0f,4.0f), new Vector2(8.0f,4.0f));
bodyFix.shape = bodyShape;
sideRight.CreateFixture(bodyFix);
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
physicsWorld.Step(1/30f, 10, 10);
HandleKeyDown();
base.Update(gameTime);
}
private KeyboardState prevKeyboardState;
private void HandleKeyDown()
{
var state = Keyboard.GetState();
if (state.IsKeyDown(Keys.Left) && prevKeyboardState.IsKeyDown(Keys.Left))
{
frontWheelJoint.SetMotorSpeed(-10f);
backWheelJoint.SetMotorSpeed(-10f);
}
if (state.IsKeyDown(Keys.Right) && prevKeyboardState.IsKeyDown(Keys.Right))
{
frontWheelJoint.SetMotorSpeed(10f);
backWheelJoint.SetMotorSpeed(10f);
}
prevKeyboardState = state;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(simpleBackground, Vector2.Zero, Color.White);
spriteBatch.Draw(wheelTexture, FrontWheelBody.Position/0.01f, null, Color.White, FrontWheelBody.Rotation,
new Vector2(wheelTexture.Width/2f, wheelTexture.Height/2f), 1, SpriteEffects.None, 0);
spriteBatch.Draw(wheelTexture, BackWheelBody.Position / 0.01f, null, Color.White, BackWheelBody.Rotation,
new Vector2(wheelTexture.Width / 2f, wheelTexture.Height / 2f), 1, SpriteEffects.None, 0);
spriteBatch.Draw(bodyTexture, boardBody.Position/0.01f, null, Color.White, boardBody.Rotation,
new Vector2(bodyTexture.Width/2f, bodyTexture.Height/2f), 1, SpriteEffects.None, 0);
spriteBatch.End();
base.Draw(gameTime);
}