/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private World world;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
// Frame rate is 30 fps by default for Windows Phone.
TargetElapsedTime = TimeSpan.FromTicks(333333);
// Pre-autoscale settings.
graphics.PreferredBackBufferWidth = 480;
graphics.PreferredBackBufferHeight = 800;
}
protected override void Initialize()
{
// TODO: Add your initialization logic here
world = new World(new Vector2(0, 10), true);
base.Initialize();
}
private Texture2D headTexture;
private Texture2D torsoTexture;
private Texture2D upperArmTexture;
private Texture2D lowerArmTexture;
private Texture2D upperLegTexture;
private Texture2D lowerLegTexture;
private float scaleFormat = 100f;
private Ground ground;
private Body headBody;
private List<Body> ballBodies = new List<Body>();
/// <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);
headTexture = Content.Load<Texture2D>("head");
torsoTexture = Content.Load<Texture2D>("torso");
upperArmTexture = Content.Load<Texture2D>("upperarm");
lowerArmTexture = Content.Load<Texture2D>("arm2");
upperLegTexture = Content.Load<Texture2D>("leg");
lowerLegTexture = Content.Load<Texture2D>("leg2");
ground = new Ground(world);
var box = new PolygonShape();
var bd = new BodyDef();
var jd = new RevoluteJointDef();
var fixtureDef = new FixtureDef();
// BODIES
// Set these to dynamic bodies
bd.type = BodyType.Dynamic;
var circ = new CircleShape();
circ._radius = 12.5f/scaleFormat;
fixtureDef.shape = circ;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.3f;
bd.position = new Vector2(100/scaleFormat, 105/scaleFormat);
headBody = world.CreateBody(bd);
headBody.CreateFixture(fixtureDef);
headBody.SetUserData(headTexture);
ballBodies.Add(headBody);
//Torso1
box = new PolygonShape();
box.SetAsBox(15/scaleFormat, 10/scaleFormat);
fixtureDef.shape = box;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.1f;
bd.position = new Vector2(100/scaleFormat, 128/scaleFormat);
var torso1 = world.CreateBody(bd);
torso1.CreateFixture(fixtureDef);
torso1.SetUserData(torsoTexture);
ballBodies.Add(torso1);
//Torso2
box = new PolygonShape();
box.SetAsBox(15/scaleFormat, 10/scaleFormat);
fixtureDef.shape = box;
bd.position = new Vector2(100/scaleFormat, 143/scaleFormat);
var torso2 = world.CreateBody(bd);
torso2.CreateFixture(fixtureDef);
torso2.SetUserData(torsoTexture);
ballBodies.Add(torso2);
//torso3
box = new PolygonShape();
box.SetAsBox(15/scaleFormat, 10/scaleFormat);
fixtureDef.shape = box;
bd.position = new Vector2(100/scaleFormat, 158/scaleFormat);
var torso3 = world.CreateBody(bd);
torso3.CreateFixture(fixtureDef);
torso3.SetUserData(torsoTexture);
ballBodies.Add(torso3);
//upperarm
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.1f;
box = new PolygonShape();
box.SetAsBox(18/scaleFormat, 6.5f/scaleFormat);
fixtureDef.shape = box;
bd.position = new Vector2(70/scaleFormat, 120/scaleFormat);
var upperArmL = world.CreateBody(bd);
upperArmL.CreateFixture(fixtureDef);
upperArmL.SetUserData(upperArmTexture);
ballBodies.Add(upperArmL);
//r
box = new PolygonShape();
box.SetAsBox(18/scaleFormat, 6.5f/scaleFormat);
fixtureDef.shape = box;
bd.position = new Vector2(130/scaleFormat, 120/scaleFormat);
var upperArmR = world.CreateBody(bd);
upperArmR.CreateFixture(fixtureDef);
upperArmR.SetUserData(upperArmTexture);
ballBodies.Add(upperArmR);
// LowerArm
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.1f;
//l
box = new PolygonShape();
box.SetAsBox(17/scaleFormat, 6f/scaleFormat);
fixtureDef.shape = box;
bd.position = new Vector2(43/scaleFormat, 120/scaleFormat);
var lowerArmL = world.CreateBody(bd);
lowerArmL.CreateFixture(fixtureDef);
lowerArmL.SetUserData(lowerArmTexture);
ballBodies.Add(lowerArmL);
//r
box = new PolygonShape();
box.SetAsBox(17/scaleFormat, 6f/scaleFormat);
fixtureDef.shape = box;
bd.position = new Vector2(157/scaleFormat, 120/scaleFormat);
var lowerArmR = world.CreateBody(bd);
lowerArmR.CreateFixture(fixtureDef);
lowerArmR.SetUserData(lowerArmTexture);
ballBodies.Add(lowerArmR);
// UpperLeg
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.1f;
// L
box = new PolygonShape();
box.SetAsBox(7.5f/scaleFormat, 22f/scaleFormat);
fixtureDef.shape = box;
bd.position = new Vector2(92/scaleFormat, 185/scaleFormat);
var upperLegL = world.CreateBody(bd);
upperLegL.CreateFixture(fixtureDef);
upperLegL.SetUserData(upperLegTexture);
ballBodies.Add(upperLegL);
box = new PolygonShape();
box.SetAsBox(7.5f/scaleFormat, 22f/scaleFormat);
fixtureDef.shape = box;
bd.position = new Vector2(108/scaleFormat, 185/scaleFormat);
var upperLegR = world.CreateBody(bd);
upperLegR.CreateFixture(fixtureDef);
upperLegR.SetUserData(upperLegTexture);
ballBodies.Add(upperLegR);
// LowerLeg
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.4f;
fixtureDef.restitution = 0.1f;
box = new PolygonShape();
box.SetAsBox(6f/scaleFormat, 20/scaleFormat);
fixtureDef.shape = box;
bd.position = new Vector2(92/scaleFormat, 220/scaleFormat);
var lowerLegL = world.CreateBody(bd);
lowerLegL.CreateFixture(fixtureDef);
lowerLegL.SetUserData(lowerLegTexture);
ballBodies.Add(lowerLegL);
box = new PolygonShape();
box.SetAsBox(6f/scaleFormat, 20/scaleFormat);
fixtureDef.shape = box;
bd.position = new Vector2(108/scaleFormat, 220/scaleFormat);
var lowerLegR = world.CreateBody(bd);
lowerLegR.CreateFixture(fixtureDef);
lowerLegR.SetUserData(lowerLegTexture);
ballBodies.Add(lowerLegR);
// JOINTS
jd.enableLimit = true;
// Head to shoulders
jd.lowerAngle = (float) (-40f/(180/Math.PI));
jd.upperAngle = (float) (40/(180/Math.PI));
jd.Initialize(torso1, headBody, new Vector2(100/scaleFormat, (100 + 15)/scaleFormat));
world.CreateJoint(jd);
// Upper arm to shoulders
// L
jd.lowerAngle = (float) (-85/(180/Math.PI));
jd.upperAngle = (float) (130/(180/Math.PI));
jd.Initialize(torso1, upperArmL, new Vector2((74)/scaleFormat, (120)/scaleFormat));
world.CreateJoint(jd);
// R
jd.lowerAngle = (float) (-130/(180/Math.PI));
jd.upperAngle = (float) (85/(180/Math.PI));
jd.Initialize(torso1, upperArmR, new Vector2((118)/scaleFormat, (120)/scaleFormat));
world.CreateJoint(jd);
// Lower arm to upper arm
// L
jd.lowerAngle = (float) (-130/(180/Math.PI));
jd.upperAngle = (float) (10/(180/Math.PI));
jd.Initialize(upperArmL, lowerArmL, new Vector2((55)/scaleFormat, (120)/scaleFormat));
world.CreateJoint(jd);
// R
jd.lowerAngle = (float) (-10/(180/Math.PI));
jd.upperAngle = (float) (130/(180/Math.PI));
jd.Initialize(upperArmR, lowerArmR, new Vector2((145)/scaleFormat, (120)/scaleFormat));
world.CreateJoint(jd);
// Shoulders/stomach
jd.lowerAngle = (float) (-15/(180/Math.PI));
jd.upperAngle = (float) (15/(180/Math.PI));
jd.Initialize(torso1, torso2, new Vector2(100/scaleFormat, (135)/scaleFormat));
world.CreateJoint(jd);
// Stomach/hips
jd.Initialize(torso2, torso3, new Vector2(100/scaleFormat, (150)/scaleFormat));
world.CreateJoint(jd);
// Torso to upper leg
// L
jd.lowerAngle = (float) (-25/(180/Math.PI));
jd.upperAngle = (float) (45/(180/Math.PI));
jd.Initialize(torso3, upperLegL, new Vector2((92)/scaleFormat, (172)/scaleFormat));
world.CreateJoint(jd);
// R
jd.lowerAngle = (float) (-45/(180/Math.PI));
jd.upperAngle = (float) (25/(180/Math.PI));
jd.Initialize(torso3, upperLegR, new Vector2((108)/scaleFormat, (172)/scaleFormat));
world.CreateJoint(jd);
// Upper leg to lower leg
// L
jd.lowerAngle = (float) (-25/(180/Math.PI));
jd.upperAngle = (float) (115/(180/Math.PI));
jd.Initialize(upperLegL, lowerLegL, new Vector2((92)/scaleFormat, (205)/scaleFormat));
world.CreateJoint(jd);
// R
jd.lowerAngle = (float) (-115/(180/Math.PI));
jd.upperAngle = (float) (25/(180/Math.PI));
jd.Initialize(upperLegR, lowerLegR, new Vector2((108)/scaleFormat, (205)/scaleFormat));
world.CreateJoint(jd);
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
world.Step((float)1 / 60, 10, 10);
HandleClick();
base.Update(gameTime);
}
private MouseState prevState = Mouse.GetState();
private MouseJoint joint = null;
private void HandleClick()
{
MouseState state = Mouse.GetState();
var mouseVector = new Vector2(state.X, state.Y) * 0.01f;
if (prevState.LeftButton == ButtonState.Pressed && state.LeftButton == ButtonState.Pressed)
{
if(joint==null)
{
var mouseDef = new MouseJointDef();
mouseDef.bodyA = ground.GroundBody();
mouseDef.bodyB = headBody;
mouseDef.maxForce = 1000;
mouseDef.collideConnected = true;
mouseDef.target = mouseVector;
joint = world.CreateJoint(mouseDef) as MouseJoint;
}
else
{
joint.SetTarget(mouseVector);
}
}
if (state.LeftButton == ButtonState.Released && prevState.LeftButton == ButtonState.Pressed)
{
if(joint!=null)
{
world.DestroyJoint(joint);
joint = null;
}
}
prevState = state;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
// TODO: Add your drawing code here
foreach (var body in ballBodies)
{
var myTexture = body.GetUserData() as Texture2D;
spriteBatch.Draw(myTexture as Texture2D, body.Position * scaleFormat, null, Color.Blue, body.Rotation,
new Vector2(myTexture.Width/2f, myTexture.Height/2f), 1, SpriteEffects.None, 0);
}
spriteBatch.End();
base.Draw(gameTime);
}
public class Ground
{
private Body _groundBody;
public Ground(World world)
{
var bottomGrounDef = new BodyDef();
bottomGrounDef.type = BodyType.Static;
var grounBottomShape = new PolygonShape();
grounBottomShape.SetAsEdge(new Vector2(0, 8), new Vector2(4.8f, 8.0f));
_groundBody = world.CreateBody(bottomGrounDef);
_groundBody.CreateFixture(grounBottomShape, 0.0f);
var leftGrounDef = new BodyDef();
leftGrounDef.type = BodyType.Static;
var leftGrounShape = new PolygonShape();
leftGrounShape.SetAsEdge(new Vector2(0, -8), new Vector2(0f, 8.0f));
var leftBody = world.CreateBody(leftGrounDef);
leftBody.CreateFixture(leftGrounShape, 0.0f);
var rightGrounDef = new BodyDef();
rightGrounDef.type = BodyType.Static;
var rightGrounShape = new PolygonShape();
rightGrounShape.SetAsEdge(new Vector2(4.8f, -8), new Vector2(4.8f, 8.0f));
var rightBody = world.CreateBody(rightGrounDef);
rightBody.CreateFixture(rightGrounShape, 0.0f);
}
public Body GroundBody()
{
return _groundBody;
}
}