Skip to content

Commit

Permalink
Restructured project, removed Windows demo project (will be added soo…
Browse files Browse the repository at this point in the history
…n), added common Demos library, added Mac demo project, added basic debug drawing ability.
  • Loading branch information
Arav Singhal authored and Arav Singhal committed Jul 9, 2016
1 parent ce7d466 commit 2605f50
Show file tree
Hide file tree
Showing 40 changed files with 965 additions and 562 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using System;
using PhysicsEngine2D;

using PhysicsEngine2DDemo.Demos;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using XNAPrimitives2D;

namespace PhysicsEngine2DDemo
namespace PhysicsEngine2DDemo.Demos
{
// / <summary>
// / This is the main type for your game
Expand All @@ -34,7 +30,7 @@ public class DemoGame : Game
new Demo4()
};

private const int DemoCount = 4;
private Demo SelectedDemo => demos[selectedDemo];

public DemoGame()
{
Expand All @@ -45,8 +41,8 @@ public DemoGame()
{
PreferMultiSampling = true,

PreferredBackBufferWidth = 1024,
PreferredBackBufferHeight = 768
PreferredBackBufferWidth = 1200,
PreferredBackBufferHeight = 675
};

width *= graphics.PreferredBackBufferWidth / 800f;
Expand All @@ -66,7 +62,7 @@ protected override void Initialize()
height = width / GraphicsDevice.Viewport.AspectRatio;
Primitives2D.Initialize(GraphicsDevice, height);

demos[selectedDemo].Initialize(width, height);
SelectedDemo.Initialize(width, height);

lastMouseState = Mouse.GetState();
lastKeyState = Keyboard.GetState();
Expand Down Expand Up @@ -113,7 +109,7 @@ protected override void Update(GameTime gameTime)

float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
if (/*GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||*/
Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();

Expand All @@ -129,32 +125,30 @@ protected override void Update(GameTime gameTime)
PhysicsWorld.bruteForce = !PhysicsWorld.bruteForce;

if (currentKeyState.IsKeyUp(Keys.Right) && lastKeyState.IsKeyDown(Keys.Right))
{
selectedDemo = (selectedDemo + 1) % DemoCount;
demos[selectedDemo].Initialize(width, height);
}
{
selectedDemo = (selectedDemo + 1) % demos.Length;
SelectedDemo.Initialize(width, height);
}

if (currentKeyState.IsKeyUp(Keys.Left) && lastKeyState.IsKeyDown(Keys.Left))
if (currentKeyState.IsKeyUp(Keys.Left) && lastKeyState.IsKeyDown(Keys.Left))
{
selectedDemo = Mathf.Mod(selectedDemo - 1, DemoCount);
demos[selectedDemo].Initialize(width, height);
selectedDemo = Mathf.Mod(selectedDemo - 1, demos.Length);
SelectedDemo.Initialize(width, height);
}

Vec2 mouse = new Vec2(
(float)currentMouseState.X / GraphicsDevice.Viewport.Width * width - width * 0.5f,
(1 - (float)currentMouseState.Y / GraphicsDevice.Viewport.Height) * height - height * 0.5f);


demos[selectedDemo].Update(mouse, lastMouseState.LeftButton == ButtonState.Released
SelectedDemo.Update(mouse, lastMouseState.LeftButton == ButtonState.Released
&& currentMouseState.LeftButton == ButtonState.Pressed,
lastMouseState.RightButton == ButtonState.Released
&& currentMouseState.RightButton == ButtonState.Pressed, dt);

base.Update(gameTime);
}

private float direction = Mathf.Pi;

// / <summary>
// / This is called when the game should draw itself.
// / </summary>
Expand All @@ -172,16 +166,16 @@ protected override void Draw(GameTime gameTime)
spriteBatch.DrawString(font, "(B)rute force collisions: " +
(PhysicsWorld.bruteForce ? "Yes" : "No"), new Vector2(10, 35), Color.White);

spriteBatch.DrawString(font, $"Demo {selectedDemo + 1} of {DemoCount}: " +
$"{demos[selectedDemo].description}", new Vector2(10, 60), Color.White);
spriteBatch.DrawString(font, $"Demo {selectedDemo + 1} of {demos.Length}: " +
$"{SelectedDemo.description}", new Vector2(10, 60), Color.White);

spriteBatch.End();

demos[selectedDemo].Draw(spriteBatch, font, dt);
SelectedDemo.Draw(spriteBatch, font, dt);

base.Draw(gameTime);
}


}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PhysicsEngine2D;
using XNAPrimitives2D;

namespace PhysicsEngine2DDemo.Demos
{
Expand All @@ -23,28 +22,32 @@ protected Demo()

protected static void DrawBody(Body body, Color color)
{
if (body.shape is Polygon)
DrawShape(body.shape, body.position, color);

if (body.shape is Circle)
{
Polygon polygon = body.shape as Polygon;
Vec2 r = body.transform.LocalToWorldDirection(-Vec2.UnitY * (body.shape as Circle).radius);
Primitives2D.DrawLine(body.position.ToVector2(), (body.position + r).ToVector2(), color);
}
}

protected static void DrawShape(Shape shape, Vec2 position, Color color)
{
if (shape is Polygon)
{
Polygon polygon = shape as Polygon;
Vec2[] verts = new Vec2[polygon.VertexCount];

for (int i = 0; i < polygon.VertexCount; i++)
verts[i] = polygon.transform.localToWorldRotation * polygon.vertices[i];

Primitives2D.DrawPolygon(Vec2ToVector2(body.position), Array.ConvertAll(verts, Vec2ToVector2), color);
Primitives2D.DrawPolygon(position.ToVector2(), Array.ConvertAll(verts, v => v.ToVector2()), color);
}
else if (body.shape is Circle)
else if (shape is Circle)
{
Circle circle = body.shape as Circle;
Primitives2D.DrawCircle(Vec2ToVector2(body.position), circle.radius, color);
Vec2 r = body.transform.LocalToWorldDirection(-Vec2.UnitY * circle.radius);
Primitives2D.DrawLine(Vec2ToVector2(body.position), Vec2ToVector2(body.position + r), color);
Circle circle = shape as Circle;
Primitives2D.DrawCircle(position.ToVector2(), circle.radius, color);
}
}

protected static Vector2 Vec2ToVector2(Vec2 v)
{
return new Vector2(v.x, v.y);
}
}
}
76 changes: 76 additions & 0 deletions DemoProjects/PhysicsEngine2DDemo.Demos/Demos/Demo1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using PhysicsEngine2D;

namespace PhysicsEngine2DDemo.Demos
{
public class Demo1 : Demo
{
public Demo1()
{
description = "Body Sandbox";
}

public override void Initialize(float width, float height)
{
physicsWorld.Clear();

Polygon box = new Polygon(width / 2, 0.5f);
Body b = new Body(box, new Vec2(0, -height / 2 + 0.5f));
b.SetStatic();
physicsWorld.AddBody(b);

Circle c = new Circle(width * 0.1f);
b = new Body(c, Vec2.Zero, 0, 0.3f);
physicsWorld.AddBody(b);
b.SetStatic();

box = new Polygon(0.5f, height / 2 - 0.5f);
b = new Body(box, new Vec2(-width / 2 + 1f, 0.6f));
b.SetStatic();
physicsWorld.AddBody(b);

b = new Body(box.Clone(), new Vec2(width / 2 - 1f, 0.6f));
b.SetStatic();
physicsWorld.AddBody(b);
}

public override void Update(Vec2 mouse, bool leftClick, bool rightClick, float dt)
{
if (leftClick)
{
Polygon aabb = new Polygon(.5f, .5f);
System.Random r = new System.Random();
Body b = new Body(aabb, mouse, (float)r.NextDouble(), 0.15f);

physicsWorld.AddBody(b);
}

if (rightClick)
{
Circle aabb = new Circle(0.5f);
Body b = new Body(aabb, mouse, 1);

physicsWorld.AddBody(b);
}

physicsWorld.Update(dt);
}

public override void Draw(SpriteBatch spriteBatch, SpriteFont font, float dt)
{
spriteBatch.Begin();

spriteBatch.DrawString(font, "No of bodies: " + physicsWorld.bodies.Count,
new Vector2(10, 85), Color.White);

spriteBatch.End();

foreach (Body body in physicsWorld.bodies)
DrawBody(body, Color.White);

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Microsoft.Xna.Framework.Graphics;
using PhysicsEngine2D;

using System;

namespace PhysicsEngine2DDemo.Demos
{
public class Demo2 : Demo
Expand All @@ -15,6 +17,8 @@ public override void Initialize(float width, float height)
{
physicsWorld.Clear();

physicsWorld.timeScale = 1f;

Polygon box = new Polygon(width / 2, 0.5f);
Body b = new Body(box, new Vec2(0, -height / 2 + 0.5f));
b.SetStatic();
Expand All @@ -37,7 +41,7 @@ public override void Initialize(float width, float height)
// Stacking
System.Random r = new System.Random();
for (float x = -width / 2 + w * 1.5f; x <= width / 2 - w * 1.5f; x += w * 1.5f)
for (float y = -height / 2 + 0.5f + hw ; y < 0; y += w)
for (float y = -height / 2 + 0.5f + hw; y < 0; y += w)
physicsWorld.AddBody(new Body(s.Clone(),
new Vec2(x + Mathf.Lerp(0.02f, -0.02f, (float)r.NextDouble()), y)));
}
Expand All @@ -47,7 +51,7 @@ public override void Update(Vec2 mouse, bool leftClick, bool rightClick, float d
if (leftClick)
{
Polygon aabb = new Polygon(.5f, .5f);
System.Random r = new System.Random();
Random r = new Random();
Body b = new Body(aabb, mouse, (float)r.NextDouble(), 0.15f);

physicsWorld.AddBody(b);
Expand Down Expand Up @@ -76,6 +80,41 @@ public override void Draw(SpriteBatch spriteBatch, SpriteFont font, float dt)
foreach (Body body in physicsWorld.bodies)
DrawBody(body, Color.White);

//DebugDraw();
}

private DebugDrawer debugDrawer = new DebugDrawer();

private void DebugDraw()
{
physicsWorld.DebugDraw(debugDrawer);
}

private class DebugDrawer : IDebugDrawer
{
private Random random = new Random();

private Color RandomColor { get { return new Color(random.Next(255), random.Next(255), random.Next(255)); } }

private Color[] colors = new Color[5];

public DebugDrawer()
{
for (int i = 0; i < colors.Length; i++)
{
colors[i] = RandomColor;
}
}

void IDebugDrawer.Draw(Vec2[] vertices, params object[] data)
{
Primitives2D.DrawPolygon(Vec2.Zero.ToVector2(), Array.ConvertAll(vertices, v => v.ToVector2()), colors[(int)data[0] % colors.Length]);
}

void IDebugDrawer.Draw(Vec2 center, float radius, params object[] data)
{
Primitives2D.DrawCircle(Vec2.Zero.ToVector2(), radius, colors[(int)data[0] % colors.Length]);
}
}
}
}
}
Loading

0 comments on commit 2605f50

Please sign in to comment.