Skip to content

Commit

Permalink
Latest version of Jitter2D...still not fully working but getting closer.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbettcher committed Oct 29, 2012
1 parent 2bd86e3 commit 478bd9e
Show file tree
Hide file tree
Showing 29 changed files with 1,576 additions and 1,265 deletions.
2 changes: 1 addition & 1 deletion Other/Jitter2D/Collision Demo/Collision Demo/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private void ProcessInput(float amountOfMovement)

private void UpdateProjection()
{
projection = Matrix.CreateOrthographic(40 * zoom * aspectRatio, 40 * zoom, 0, 1);
projection = Matrix.CreateOrthographic(20 * zoom * aspectRatio, 20 * zoom, 0, 1);
}

private void UpdateView()
Expand Down
161 changes: 123 additions & 38 deletions Other/Jitter2D/Collision Demo/Collision Demo/CollisionDemo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Jitter2D.LinearMath;
using Jitter2D.Dynamics;
using System.Diagnostics;
using Jitter2D.Collision.Narrowphase;

namespace CollisionDemo
{
Expand All @@ -26,27 +27,41 @@ public class CollisionDemo : Microsoft.Xna.Framework.Game
public DebugDrawer DebugDrawer;
public Camera Camera;

RigidBody body1 = new RigidBody(new ConvexHullShape(new List<JVector> { new JVector(0, -1), new JVector(-1, 1), new JVector(1, 1), }));
RigidBody body2 = new RigidBody(new ConvexHullShape(new List<JVector> { new JVector(0, -1), new JVector(-1, 1), new JVector(1, 1), }));

JVector point, normal;
float penetration;
bool hit;
bool hit, collide;
int iterations;

SpriteFont font;
Stopwatch sw = new Stopwatch();
long ticks;

// A
private BoxShape A;
private JVector PA;
private JVector VA;
private float OA;
private List<JVector> AV;

// B
private BoxShape B;
private JVector PB;
private JVector VB;
private float OB;
private List<JVector> BV;

public CollisionDemo()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferMultiSampling = true;

Content.RootDirectory = "Content";

graphics.PreferredBackBufferHeight = 720;
graphics.PreferredBackBufferWidth = 1280;
IsFixedTimeStep = false;
graphics.SynchronizeWithVerticalRetrace = false;

graphics.PreferredBackBufferHeight = 550;
graphics.PreferredBackBufferWidth = 600;
}

protected override void Initialize()
Expand All @@ -57,7 +72,15 @@ protected override void Initialize()
Camera = new Camera(this);
this.Components.Add(Camera);

body2.Position = new JVector(4, 2);
A = new BoxShape(4, 1);
PA = new JVector(-1, 2);
VA = new JVector(0, 0);
OA = MathHelper.ToRadians(0);

B = new BoxShape(1, 4);
PB = new JVector(-3, 0);
VB = new JVector(0, 0);
OB = 0;

base.Initialize();
}
Expand Down Expand Up @@ -89,48 +112,68 @@ protected override void Update(GameTime gameTime)
if (keys.IsKeyDown(Keys.Up))
moveVector.Y += amountOfMovement;

body1.Position += moveVector;

//body1.Orientation += 0.001f;
//body2.Orientation -= 0.001f;
PB += moveVector;
OA = (float)gameTime.TotalGameTime.TotalSeconds * 0.1f;
OB = (float)gameTime.TotalGameTime.TotalSeconds * -0.1f;

var c1 = body1.Shape as CircleShape;
var c2 = body2.Shape as CapsuleShape;
DrawBox(A, PA, OA, Color.Blue * 0.25f);
DrawBox(B, PB, OB, Color.Green * 0.25f);

var o = new JVector((float)-Math.Sin(body2.Orientation), (float)Math.Cos(body2.Orientation));
float t = 0.0f;
JVector[] CA = new JVector[2], CB = new JVector[2];
int NumContacts = 0;

JVector point1, point2;

sw.Start();
//hit = Collision.CircleCapsuleTest(body1.Position, c1.Radius, body2.Position, o, c2.Length, c2.Radius, out point1, out point2, out normal, out penetration);
//hit = SAT.PolyToPoly(body1.Shape, body1.Orientation, body1.Position, body2.Shape, body2.Orientation, body2.Position);
sw.Stop();

ticks = sw.ElapsedTicks;
sw.Reset();
JMatrix OAM = JMatrix.CreateRotationZ(OA);
JMatrix OBM = JMatrix.CreateRotationZ(OB);

for (int i = 0; i < 1; i++)
{
A.UpdateAxes(OA);
B.UpdateAxes(OB);

//hit = Collision.BoxBoxTest(ref A, ref PA, ref B, ref PB);
//AV = new List<JVector> { A.GetCorner(0), A.GetCorner(1), A.GetCorner(2), A.GetCorner(3) };
//BV = new List<JVector> { B.GetCorner(0), B.GetCorner(1), B.GetCorner(2), B.GetCorner(3) };

//DebugDrawer.DrawLine(point1, point1 + normal);
//hit = SAT.Collide(ref AV, ref PA, ref VA, ref OAM,
// ref BV, ref PB, ref VB, ref OBM,
// ref normal, ref t);

//DebugDrawer.DrawPoint(point2);
// DebugDrawer.DrawPoint(point1);
DebugDrawer.Color = Color.Red;
//if (hit)
//{
// SAT.FindContacts(ref AV, ref PA, ref VA, ref OAM,
// ref BV, ref PB, ref VB, ref OBM,
// ref normal, t, out CA, out CB, out NumContacts);

DebugDrawer.Color = Color.Black;
DebugDrawer.DrawLine(JVector.Up, JVector.Down);
DebugDrawer.DrawLine(JVector.Left, JVector.Right);
// normal.Normalize();
//}

hit = Collision.BoxBoxTestContact(ref A, ref PA, ref OAM, ref B, ref PB, ref OBM,
out normal, out t, out CA, out CB, out NumContacts);

penetration = t;
iterations = NumContacts;
}

sw.Stop();
ticks = sw.ElapsedTicks / 1;
sw.Reset();

body1.DebugDraw(DebugDrawer);
body2.DebugDraw(DebugDrawer);

if (hit)
{
var oldPosition = body1.Position;

body1.Position += normal * penetration;
body1.DebugDraw(DebugDrawer);
body1.Position = oldPosition;
//DrawBox(A, PA + normal * (t * 0.5f), OA, Color.Blue);
//DrawBox(B, PB - normal * (t * 0.5f), OB, Color.Green);

for (int i = 0; i < NumContacts; i++)
{
DebugDrawer.DrawPoint(CA[i]);// + normal * (t * 0.5f));
DebugDrawer.DrawPoint(CB[i]);// - normal * (t * 0.5f));
}
}

base.Update(gameTime);
}

Expand All @@ -144,12 +187,54 @@ protected override void Draw(GameTime gameTime)

spriteBatch.DrawString(font, "Collided: " + hit.ToString(), new Vector2(10, line++ * 20), Color.Black);
spriteBatch.DrawString(font, "Penetration: " + penetration.ToString(), new Vector2(10, line++ * 20), Color.Black);
spriteBatch.DrawString(font, "MPR Iterations: " + iterations.ToString(), new Vector2(10, line++ * 20), Color.Black);
spriteBatch.DrawString(font, "MPR Ticks: " + ticks.ToString(), new Vector2(10, line++ * 20), Color.Black);
spriteBatch.DrawString(font, "Contacts: " + iterations.ToString(), new Vector2(10, line++ * 20), Color.Black);
spriteBatch.DrawString(font, "Ticks: " + ticks.ToString(), new Vector2(10, line++ * 20), Color.Black);

spriteBatch.End();

base.Draw(gameTime);
}

void DrawBox(BoxShape box, JVector pos, float orientation, Color color)
{
List<JVector> poly = new List<JVector>
{
new JVector(-box.Size.X * 0.5f, -box.Size.Y * 0.5f),
new JVector(box.Size.X * 0.5f, -box.Size.Y * 0.5f),
new JVector(box.Size.X * 0.5f, box.Size.Y * 0.5f),
new JVector(-box.Size.X * 0.5f,box.Size.Y * 0.5f)
};

DrawPoly(poly, pos, JMatrix.CreateRotationZ(orientation), color);
}

void DrawPoly(List<JVector> poly, JVector pos, JMatrix o, Color color)
{
for (int i = 0; i < poly.Count - 1; i++)
{
JVector a = JVector.Transform(poly[i], o * JMatrix.CreateTranslation(pos));
JVector b = JVector.Transform(poly[i + 1], o * JMatrix.CreateTranslation(pos));
DebugDrawer.DrawLine(a, b, color);
}
JVector c = JVector.Transform(poly[0], o * JMatrix.CreateTranslation(pos));
JVector d = JVector.Transform(poly[poly.Count - 1], o * JMatrix.CreateTranslation(pos));
DebugDrawer.DrawLine(c, d, color);
}

List<JVector> BuildBlob(int iNumVertices, float radius)
{
List<JVector> axVertices = new List<JVector>(iNumVertices);

float a = (float)Math.PI / iNumVertices;
float da = (float)(Math.PI * 2.0f) / iNumVertices;

for (int i = 0; i < iNumVertices; i++)
{
a += da;

axVertices.Add(new JVector((float)Math.Cos(a) * radius, (float)Math.Sin(a) * radius));
}
return axVertices;
}
}
}
2 changes: 1 addition & 1 deletion Other/Jitter2D/Collision Demo/Collision Demo/LD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace SAT
namespace CollisionDemo
{
public class LD
{
Expand Down
98 changes: 25 additions & 73 deletions Other/Jitter2D/Jitter2D.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,13 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JitterDemo", "JitterDemo\Ji
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JitterDemoContent", "JitterDemo\JitterDemoContent\JitterDemoContent.contentproj", "{A8C2D99B-05AB-4BF8-87E8-3B7FF779A22E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MPR Collision Demo", "MPR Collision Demo", "{DDFAD3F4-C202-4F52-8E1E-CCC9D3862B88}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CollisionDemo", "CollisionDemo", "{3644A86D-752F-43E9-B020-E11796F2FDEB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MPRCollisionDemo", "MPRCollisionDemo\MPRCollisionDemo\MPRCollisionDemo.csproj", "{34DF8FFB-2A01-43A8-9641-34E32767EB7F}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CollisionDemo", "CollisionDemo\CollisionDemo\CollisionDemo.csproj", "{D8B513AF-C28D-4595-A205-09D9D52272F7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MPRCollisionDemoContent", "MPRCollisionDemo\MPRCollisionDemoContent\MPRCollisionDemoContent.contentproj", "{54D407F3-3A3C-41AE-9C7B-CF80055A67DF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "GJK Collision Demo", "GJK Collision Demo", "{E85A1704-7430-427A-89CE-40EDB0B8620F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GJKCollisionDemo", "GJKCollisionDemo\GJKCollisionDemo\GJKCollisionDemo.csproj", "{F037942C-5A94-498A-91A5-89BA72C2588E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GJKCollisionDemoContent", "GJKCollisionDemo\GJKCollisionDemoContent\GJKCollisionDemoContent.contentproj", "{3A7A8A16-27FF-427E-AA5A-B7C77A8B8421}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Collision Demo", "Collision Demo", "{32DC3354-7888-45AD-B218-BEEAC9D5B41A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Collision Demo", "Collision Demo\Collision Demo\Collision Demo.csproj", "{BF4D84F7-0143-4F76-BD10-B9EF4FB67210}"
EndProject
Project("{96E2B04D-8817-42C6-938A-82C39BA4D311}") = "Collision DemoContent", "Collision Demo\Collision DemoContent\Collision DemoContent.contentproj", "{D729A9F9-AA28-4D3B-9105-4ED6E1354F48}"
Project("{96E2B04D-8817-42C6-938A-82C39BA4D311}") = "CollisionDemoContent", "CollisionDemo\CollisionDemoContent\CollisionDemoContent.contentproj", "{A63E08EB-2C7E-47D3-8988-08D8D1B9BD4E}"
EndProject
Global
GlobalSection(SubversionScc) = preSolution
Svn-Managed = True
Manager = AnkhSVN - Subversion Support for Visual Studio
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|Mixed Platforms = Debug|Mixed Platforms
Expand Down Expand Up @@ -69,66 +53,34 @@ Global
{A8C2D99B-05AB-4BF8-87E8-3B7FF779A22E}.Release|Any CPU.ActiveCfg = Release|x86
{A8C2D99B-05AB-4BF8-87E8-3B7FF779A22E}.Release|Mixed Platforms.ActiveCfg = Release|x86
{A8C2D99B-05AB-4BF8-87E8-3B7FF779A22E}.Release|x86.ActiveCfg = Release|x86
{34DF8FFB-2A01-43A8-9641-34E32767EB7F}.Debug|Any CPU.ActiveCfg = Debug|x86
{34DF8FFB-2A01-43A8-9641-34E32767EB7F}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{34DF8FFB-2A01-43A8-9641-34E32767EB7F}.Debug|Mixed Platforms.Build.0 = Debug|x86
{34DF8FFB-2A01-43A8-9641-34E32767EB7F}.Debug|x86.ActiveCfg = Debug|x86
{34DF8FFB-2A01-43A8-9641-34E32767EB7F}.Debug|x86.Build.0 = Debug|x86
{34DF8FFB-2A01-43A8-9641-34E32767EB7F}.Release|Any CPU.ActiveCfg = Release|x86
{34DF8FFB-2A01-43A8-9641-34E32767EB7F}.Release|Mixed Platforms.ActiveCfg = Release|x86
{34DF8FFB-2A01-43A8-9641-34E32767EB7F}.Release|Mixed Platforms.Build.0 = Release|x86
{34DF8FFB-2A01-43A8-9641-34E32767EB7F}.Release|x86.ActiveCfg = Release|x86
{34DF8FFB-2A01-43A8-9641-34E32767EB7F}.Release|x86.Build.0 = Release|x86
{54D407F3-3A3C-41AE-9C7B-CF80055A67DF}.Debug|Any CPU.ActiveCfg = Debug|x86
{54D407F3-3A3C-41AE-9C7B-CF80055A67DF}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{54D407F3-3A3C-41AE-9C7B-CF80055A67DF}.Debug|x86.ActiveCfg = Debug|x86
{54D407F3-3A3C-41AE-9C7B-CF80055A67DF}.Release|Any CPU.ActiveCfg = Release|x86
{54D407F3-3A3C-41AE-9C7B-CF80055A67DF}.Release|Mixed Platforms.ActiveCfg = Release|x86
{54D407F3-3A3C-41AE-9C7B-CF80055A67DF}.Release|x86.ActiveCfg = Release|x86
{F037942C-5A94-498A-91A5-89BA72C2588E}.Debug|Any CPU.ActiveCfg = Debug|x86
{F037942C-5A94-498A-91A5-89BA72C2588E}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{F037942C-5A94-498A-91A5-89BA72C2588E}.Debug|Mixed Platforms.Build.0 = Debug|x86
{F037942C-5A94-498A-91A5-89BA72C2588E}.Debug|x86.ActiveCfg = Debug|x86
{F037942C-5A94-498A-91A5-89BA72C2588E}.Debug|x86.Build.0 = Debug|x86
{F037942C-5A94-498A-91A5-89BA72C2588E}.Release|Any CPU.ActiveCfg = Release|x86
{F037942C-5A94-498A-91A5-89BA72C2588E}.Release|Mixed Platforms.ActiveCfg = Release|x86
{F037942C-5A94-498A-91A5-89BA72C2588E}.Release|Mixed Platforms.Build.0 = Release|x86
{F037942C-5A94-498A-91A5-89BA72C2588E}.Release|x86.ActiveCfg = Release|x86
{F037942C-5A94-498A-91A5-89BA72C2588E}.Release|x86.Build.0 = Release|x86
{3A7A8A16-27FF-427E-AA5A-B7C77A8B8421}.Debug|Any CPU.ActiveCfg = Debug|x86
{3A7A8A16-27FF-427E-AA5A-B7C77A8B8421}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{3A7A8A16-27FF-427E-AA5A-B7C77A8B8421}.Debug|x86.ActiveCfg = Debug|x86
{3A7A8A16-27FF-427E-AA5A-B7C77A8B8421}.Release|Any CPU.ActiveCfg = Release|x86
{3A7A8A16-27FF-427E-AA5A-B7C77A8B8421}.Release|Mixed Platforms.ActiveCfg = Release|x86
{3A7A8A16-27FF-427E-AA5A-B7C77A8B8421}.Release|x86.ActiveCfg = Release|x86
{BF4D84F7-0143-4F76-BD10-B9EF4FB67210}.Debug|Any CPU.ActiveCfg = Debug|x86
{BF4D84F7-0143-4F76-BD10-B9EF4FB67210}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{BF4D84F7-0143-4F76-BD10-B9EF4FB67210}.Debug|Mixed Platforms.Build.0 = Debug|x86
{BF4D84F7-0143-4F76-BD10-B9EF4FB67210}.Debug|x86.ActiveCfg = Debug|x86
{BF4D84F7-0143-4F76-BD10-B9EF4FB67210}.Debug|x86.Build.0 = Debug|x86
{BF4D84F7-0143-4F76-BD10-B9EF4FB67210}.Release|Any CPU.ActiveCfg = Release|x86
{BF4D84F7-0143-4F76-BD10-B9EF4FB67210}.Release|Mixed Platforms.ActiveCfg = Release|x86
{BF4D84F7-0143-4F76-BD10-B9EF4FB67210}.Release|Mixed Platforms.Build.0 = Release|x86
{BF4D84F7-0143-4F76-BD10-B9EF4FB67210}.Release|x86.ActiveCfg = Release|x86
{BF4D84F7-0143-4F76-BD10-B9EF4FB67210}.Release|x86.Build.0 = Release|x86
{D729A9F9-AA28-4D3B-9105-4ED6E1354F48}.Debug|Any CPU.ActiveCfg = Debug|x86
{D729A9F9-AA28-4D3B-9105-4ED6E1354F48}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{D729A9F9-AA28-4D3B-9105-4ED6E1354F48}.Debug|x86.ActiveCfg = Debug|x86
{D729A9F9-AA28-4D3B-9105-4ED6E1354F48}.Release|Any CPU.ActiveCfg = Release|x86
{D729A9F9-AA28-4D3B-9105-4ED6E1354F48}.Release|Mixed Platforms.ActiveCfg = Release|x86
{D729A9F9-AA28-4D3B-9105-4ED6E1354F48}.Release|x86.ActiveCfg = Release|x86
{D8B513AF-C28D-4595-A205-09D9D52272F7}.Debug|Any CPU.ActiveCfg = Debug|x86
{D8B513AF-C28D-4595-A205-09D9D52272F7}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{D8B513AF-C28D-4595-A205-09D9D52272F7}.Debug|Mixed Platforms.Build.0 = Debug|x86
{D8B513AF-C28D-4595-A205-09D9D52272F7}.Debug|x86.ActiveCfg = Debug|x86
{D8B513AF-C28D-4595-A205-09D9D52272F7}.Debug|x86.Build.0 = Debug|x86
{D8B513AF-C28D-4595-A205-09D9D52272F7}.Release|Any CPU.ActiveCfg = Release|x86
{D8B513AF-C28D-4595-A205-09D9D52272F7}.Release|Mixed Platforms.ActiveCfg = Release|x86
{D8B513AF-C28D-4595-A205-09D9D52272F7}.Release|Mixed Platforms.Build.0 = Release|x86
{D8B513AF-C28D-4595-A205-09D9D52272F7}.Release|x86.ActiveCfg = Release|x86
{D8B513AF-C28D-4595-A205-09D9D52272F7}.Release|x86.Build.0 = Release|x86
{A63E08EB-2C7E-47D3-8988-08D8D1B9BD4E}.Debug|Any CPU.ActiveCfg = Debug|x86
{A63E08EB-2C7E-47D3-8988-08D8D1B9BD4E}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
{A63E08EB-2C7E-47D3-8988-08D8D1B9BD4E}.Debug|x86.ActiveCfg = Debug|x86
{A63E08EB-2C7E-47D3-8988-08D8D1B9BD4E}.Release|Any CPU.ActiveCfg = Release|x86
{A63E08EB-2C7E-47D3-8988-08D8D1B9BD4E}.Release|Mixed Platforms.ActiveCfg = Release|x86
{A63E08EB-2C7E-47D3-8988-08D8D1B9BD4E}.Release|x86.ActiveCfg = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{75374A2E-27DE-4FF7-A1ED-C995D013EE7C} = {40035013-6972-4444-8CF9-C0BD5A37A635}
{A8C2D99B-05AB-4BF8-87E8-3B7FF779A22E} = {40035013-6972-4444-8CF9-C0BD5A37A635}
{34DF8FFB-2A01-43A8-9641-34E32767EB7F} = {DDFAD3F4-C202-4F52-8E1E-CCC9D3862B88}
{54D407F3-3A3C-41AE-9C7B-CF80055A67DF} = {DDFAD3F4-C202-4F52-8E1E-CCC9D3862B88}
{F037942C-5A94-498A-91A5-89BA72C2588E} = {E85A1704-7430-427A-89CE-40EDB0B8620F}
{3A7A8A16-27FF-427E-AA5A-B7C77A8B8421} = {E85A1704-7430-427A-89CE-40EDB0B8620F}
{BF4D84F7-0143-4F76-BD10-B9EF4FB67210} = {32DC3354-7888-45AD-B218-BEEAC9D5B41A}
{D729A9F9-AA28-4D3B-9105-4ED6E1354F48} = {32DC3354-7888-45AD-B218-BEEAC9D5B41A}
{D8B513AF-C28D-4595-A205-09D9D52272F7} = {3644A86D-752F-43E9-B020-E11796F2FDEB}
{A63E08EB-2C7E-47D3-8988-08D8D1B9BD4E} = {3644A86D-752F-43E9-B020-E11796F2FDEB}
EndGlobalSection
GlobalSection(SubversionScc) = preSolution
Svn-Managed = True
Manager = AnkhSVN - Subversion Support for Visual Studio
EndGlobalSection
EndGlobal
Loading

0 comments on commit 478bd9e

Please sign in to comment.