Added Box2D

This commit is contained in:
Julian Nießner
2018-05-06 12:43:20 +02:00
parent cb5469bb89
commit d175d433a8
351 changed files with 123545 additions and 1 deletions

View File

@@ -0,0 +1,220 @@
namespace TestBed.Net
{
partial class MainWindow
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.panel1 = new System.Windows.Forms.Panel();
this.OpenGLControl = new Tao.Platform.Windows.SimpleOpenGlControl();
this.panel2 = new System.Windows.Forms.Panel();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.numericUpDown2 = new System.Windows.Forms.NumericUpDown();
this.numericUpDown1 = new System.Windows.Forms.NumericUpDown();
this.checkBox2 = new System.Windows.Forms.CheckBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.TestsComboBox = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.RedrawTimer = new System.Windows.Forms.Timer(this.components);
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).BeginInit();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.OpenGLControl);
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(372, 242);
this.panel1.TabIndex = 3;
//
// OpenGLControl
//
this.OpenGLControl.AccumBits = ((byte)(0));
this.OpenGLControl.AutoCheckErrors = true;
this.OpenGLControl.AutoFinish = true;
this.OpenGLControl.AutoMakeCurrent = true;
this.OpenGLControl.AutoSwapBuffers = true;
this.OpenGLControl.BackColor = System.Drawing.Color.Black;
this.OpenGLControl.ColorBits = ((byte)(32));
this.OpenGLControl.DepthBits = ((byte)(16));
this.OpenGLControl.Location = new System.Drawing.Point(106, 62);
this.OpenGLControl.Name = "OpenGLControl";
this.OpenGLControl.Size = new System.Drawing.Size(137, 103);
this.OpenGLControl.StencilBits = ((byte)(0));
this.OpenGLControl.TabIndex = 1;
this.OpenGLControl.PreviewKeyDown += new System.Windows.Forms.PreviewKeyDownEventHandler(this.OpenGLControl_PreviewKeyDown);
this.OpenGLControl.MouseDown += new System.Windows.Forms.MouseEventHandler(this.OpenGLControl_MouseDown);
this.OpenGLControl.MouseMove += new System.Windows.Forms.MouseEventHandler(this.OpenGLControl_MouseMove);
this.OpenGLControl.MouseUp += new System.Windows.Forms.MouseEventHandler(this.OpenGLControl_MouseUp);
//
// panel2
//
this.panel2.Controls.Add(this.label3);
this.panel2.Controls.Add(this.label2);
this.panel2.Controls.Add(this.numericUpDown2);
this.panel2.Controls.Add(this.numericUpDown1);
this.panel2.Controls.Add(this.checkBox2);
this.panel2.Controls.Add(this.checkBox1);
this.panel2.Controls.Add(this.TestsComboBox);
this.panel2.Controls.Add(this.label1);
this.panel2.Location = new System.Drawing.Point(390, 12);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(170, 242);
this.panel2.TabIndex = 4;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(18, 81);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(32, 13);
this.label3.TabIndex = 10;
this.label3.Text = "Hertz";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(18, 55);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(50, 13);
this.label2.TabIndex = 9;
this.label2.Text = "Iterations";
//
// numericUpDown2
//
this.numericUpDown2.Location = new System.Drawing.Point(71, 79);
this.numericUpDown2.Name = "numericUpDown2";
this.numericUpDown2.Size = new System.Drawing.Size(71, 20);
this.numericUpDown2.TabIndex = 8;
this.numericUpDown2.Value = new decimal(new int[] {
60,
0,
0,
0});
//
// numericUpDown1
//
this.numericUpDown1.Location = new System.Drawing.Point(71, 53);
this.numericUpDown1.Name = "numericUpDown1";
this.numericUpDown1.Size = new System.Drawing.Size(71, 20);
this.numericUpDown1.TabIndex = 7;
this.numericUpDown1.Value = new decimal(new int[] {
60,
0,
0,
0});
//
// checkBox2
//
this.checkBox2.AutoSize = true;
this.checkBox2.Checked = true;
this.checkBox2.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox2.Location = new System.Drawing.Point(21, 128);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(93, 17);
this.checkBox2.TabIndex = 6;
this.checkBox2.Text = "Warm Starting";
this.checkBox2.UseVisualStyleBackColor = true;
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Checked = true;
this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox1.Location = new System.Drawing.Point(21, 105);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(114, 17);
this.checkBox1.TabIndex = 5;
this.checkBox1.Text = "Position Correction";
this.checkBox1.UseVisualStyleBackColor = true;
//
// TestsComboBox
//
this.TestsComboBox.FormattingEnabled = true;
this.TestsComboBox.Location = new System.Drawing.Point(21, 26);
this.TestsComboBox.Name = "TestsComboBox";
this.TestsComboBox.Size = new System.Drawing.Size(121, 21);
this.TestsComboBox.TabIndex = 3;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(67, 10);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(33, 13);
this.label1.TabIndex = 4;
this.label1.Text = "Tests";
//
// RedrawTimer
//
this.RedrawTimer.Enabled = true;
this.RedrawTimer.Interval = 30;
this.RedrawTimer.Tag = "";
this.RedrawTimer.Tick += new System.EventHandler(this.RedrawTimer_Tick);
//
// MainWindow
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(572, 457);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
this.Name = "MainWindow";
this.Text = "MainWindow";
this.Resize += new System.EventHandler(this.MainWindow_Resize);
this.Load += new System.EventHandler(this.MainWindow_Load);
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.numericUpDown1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private Tao.Platform.Windows.SimpleOpenGlControl OpenGLControl;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox TestsComboBox;
private System.Windows.Forms.Timer RedrawTimer;
private System.Windows.Forms.CheckBox checkBox2;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.NumericUpDown numericUpDown2;
private System.Windows.Forms.NumericUpDown numericUpDown1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
}
}

View File

@@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Tao.OpenGl;
using Box2D.Net;
using System.Reflection;
namespace TestBed.Net
{
public partial class MainWindow : Form
{
private Settings Settings = new Settings();
private Test mCurrentTest;
public Test CurrentTest
{
get
{
return mCurrentTest;
}
set
{
//Call code to reset current test
mCurrentTest = value;
}
}
public MainWindow()
{
InitializeComponent();
//Find all Tests in this module
foreach (Type type in Assembly.GetExecutingAssembly().GetExportedTypes())
{
Test test = new Test();
if (type.IsSubclassOf(test.GetType()))
{
TestsComboBox.Items.Add(System.Activator.CreateInstance(type));
}
}
TestsComboBox.SelectedIndex = 0;
CurrentTest = (Test)System.Activator.CreateInstance(TestsComboBox.SelectedItem.GetType());
OpenGLControl.Dock = DockStyle.Fill;
}
private void MainWindow_Load(object sender, EventArgs e)
{
OpenGLControl.InitializeContexts();
panel1.Dock = DockStyle.Fill;
panel2.Dock = DockStyle.Right;
MainWindow_Resize(null, null);
RedrawTimer.Interval = (int)(1000.0f / (float)Settings.Hz);
RedrawTimer.Start();
}
private void MainWindow_Resize(object sender, EventArgs e)
{
Size size = new Size(OpenGLControl.Size.Width - panel2.Width, OpenGLControl.Size.Height);
Renderer.InitOpenGL(size, CurrentTest.Zoom, CurrentTest.ViewOffset);
Renderer.OpenGLDraw(CurrentTest);
}
private void OpenGLControl_Paint(object sender, PaintEventArgs e)
{
Renderer.OpenGLDraw(CurrentTest);
}
private Vector RelativeCoordinates(Point MousePoint)
{
float Height = (float)OpenGLControl.Size.Height;
float Width = (float)(OpenGLControl.Size.Width - panel2.Width);
if(Height <= 0)
Height = 1;
float AspectRatio = Width / Height;
Vector relative = new Vector(
(float)MousePoint.X / Width,
(float)MousePoint.Y / Height);
relative -= new Vector(.5f, .5f);
relative *= 2;
relative.Y *= -1;
relative.X *= AspectRatio;
return relative;
}
private void OpenGLControl_MouseMove(object sender, MouseEventArgs e)
{
CurrentTest.MouseMove(RelativeCoordinates(e.Location));
}
private void OpenGLControl_MouseUp(object sender, MouseEventArgs e)
{
CurrentTest.MouseUp(RelativeCoordinates(e.Location));
}
private void OpenGLControl_MouseDown(object sender, MouseEventArgs e)
{
CurrentTest.MouseDown(RelativeCoordinates(e.Location));
}
private void RedrawTimer_Tick(object sender, EventArgs e)
{
CurrentTest.Step(Settings);
Renderer.OpenGLDraw(CurrentTest);
OpenGLControl.Draw();
int errorCode = 0;
if ((errorCode = Gl.glGetError()) > 0)
{
RedrawTimer.Stop();
//Handled by the OpenGLControl
//MessageBox.Show(Glu.gluErrorString(errorCode));
}
}
private void OpenGLControl_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if(e.KeyCode == Keys.R)
CurrentTest = (Test)System.Activator.CreateInstance(CurrentTest.GetType());
else
CurrentTest.KeyPress(e.KeyCode);
}
}
}

View File

@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="RedrawTimer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
</root>

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Text;
using Box2D.Net;
using System.Windows.Forms;
namespace TestBed.Net
{
class Program
{
static void Main(string[] args)
{
Console.Title = "Box2D.Net Test Bed";
Application.EnableVisualStyles();
MainWindow win = new MainWindow();
Console.Write("Loading the OpenGL display window. Please be patient... ");
//Show above the console
win.Show();
win.BringToFront(); //Doesn't bring above the console?
win.TopMost = true; //Hacky fix instead:
win.TopMost = false;
Console.WriteLine("DONE");
Application.Run(win);
}
}
}

View File

@@ -0,0 +1,33 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("TestBed.Net")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("TestBed.Net")]
[assembly: AssemblyCopyright("Copyright © 2008")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("c08bf3be-733a-49ed-820b-ad4f13ff3a00")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,176 @@
using System;
using System.Collections.Generic;
using System.Text;
using Tao.OpenGl;
using Box2D.Net;
using System.Windows.Forms;
namespace TestBed.Net
{
static class Renderer
{
public static void InitOpenGL(System.Drawing.Size WidthHeight, float viewZoom, Vector ViewOffset)
{
int Width = WidthHeight.Width;
int Height = WidthHeight.Height;
Height = Height > 0 ? Height : 1;
Gl.glDisable(Gl.GL_CULL_FACE);
Gl.glClearColor(.5f, .5f, .5f, 1);
float AspectRatio = (float)Width / (float)Height;
Gl.glViewport(0, 0, Width, Height);
Gl.glMatrixMode(Gl.GL_PROJECTION);
Gl.glLoadIdentity();
Glu.gluOrtho2D(-AspectRatio, AspectRatio, -1, 1);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
}
public static void OpenGLDraw(Test test)
{
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
Gl.glMatrixMode(Gl.GL_MODELVIEW);
Gl.glLoadIdentity();
Gl.glScalef(1.0f / test.Zoom, 1.0f / test.Zoom, 1.0f / test.Zoom);
Gl.glTranslatef(-test.ViewOffset.X, -test.ViewOffset.Y, 0);
Gl.glPushMatrix();
DrawBodies(test.world.Bodies);
DrawJoints(test.world.Joints);
Gl.glPopMatrix();
}
public static void DrawJoints(IList<Joint> joints)
{
foreach (Joint joint in joints)
{
DrawJoint(joint, System.Drawing.Color.LawnGreen);
}
}
public static void DrawBodies(IList<Body> bodies)
{
foreach(Body body in bodies)
foreach (Shape shape in body.Shapes)
{
System.Drawing.Color color = System.Drawing.Color.White;
if (body.Static)
color = System.Drawing.Color.LightGreen; //Color(0.5f, 0.9f, 0.5f)
else if (body.Sleeping)
color = System.Drawing.Color.LightBlue;
//else if(body == bomb)
DrawShape(body.GetXForm(), shape, System.Drawing.Color.White);
}
}
public static void DrawShape(XForm xform, Shape shape, System.Drawing.Color c)
{
switch (shape.ShapeType)
{
case ShapeType.e_circleShape:
{
Vector x = xform.Position;
float r = (new CircleShape(shape)).Radius;
float segments = 16;
double increment = 2 * Math.PI / segments;
Gl.glColor3ub(c.R, c.G, c.B);
Gl.glBegin(Gl.GL_LINE_LOOP);
for (double i = 0, theta = 0; i < segments; ++i, theta += increment)
{
Vector d = new Vector(r * (float)Math.Cos(theta), r * (float)Math.Sin(theta));
Vector v = x + d;
Gl.glVertex2f(v.X, v.Y);
}
Gl.glEnd();
//Draw a line from the circle's center to it's right side
//so we can visually inspect rotations.
Gl.glBegin(Gl.GL_LINES);
Gl.glVertex2f(x.X, x.Y);
Vector ax = xform.Rotation.col1;
Gl.glVertex2f(x.X + r * ax.X, x.Y + r * ax.Y);
Gl.glEnd();
}
break;
case ShapeType.e_polygonShape:
{
Gl.glColor3ub(c.R, c.G, c.B);
Gl.glBegin(Gl.GL_LINE_LOOP);
foreach (Vector vertex in (new PolyShape(shape)).Vertices)
{
Vector vertprime = xform.Rotation * vertex + xform.Position;
Gl.glVertex2f(vertprime.X, vertprime.Y);
}
Gl.glEnd();
}
break;
}
}
public static void DrawAABB(AABB aabb, System.Drawing.Color c)
{
Gl.glColor3b(c.R, c.G, c.B);
Gl.glBegin(Gl.GL_LINE_LOOP);
Gl.glVertex2f(aabb.lowerBound.X, aabb.lowerBound.Y);
Gl.glVertex2f(aabb.upperBound.X, aabb.lowerBound.Y);
Gl.glVertex2f(aabb.upperBound.X, aabb.upperBound.Y);
Gl.glVertex2f(aabb.lowerBound.X, aabb.upperBound.Y);
Gl.glEnd();
}
public static void DrawJoint(Joint joint, System.Drawing.Color color)
{
Body b1 = joint.Body1;
Body b2 = joint.Body2;
Vector x1 = b1.GetXForm().Position;
Vector x2 = b2.GetXForm().Position;
Vector p1 = joint.Anchor2;
Vector p2 = joint.Anchor1;
Gl.glColor3ub(color.R, color.G, color.B);
Gl.glBegin(Gl.GL_LINES);
switch (joint.JointType)
{
case JointType.e_mouseJoint:
case JointType.e_distanceJoint:
Gl.glVertex2f(p1.X, p1.Y);
Gl.glVertex2f(p2.X, p2.Y);
break;
/*
* case JointType.e_pulleyJoint:
{
b2PulleyJoint* pulley = (b2PulleyJoint*)joint;
b2Vec2 s1 = pulley->GetGroundPoint1();
b2Vec2 s2 = pulley->GetGroundPoint2();
glVertex2f(s1.x, s1.y);
glVertex2f(p1.x, p1.y);
glVertex2f(s2.x, s2.y);
glVertex2f(p2.x, p2.y);
}
break;
*/
default:
Gl.glVertex2f(x1.X, x1.Y);
Gl.glVertex2f(p1.X, p1.Y);
Gl.glVertex2f(x2.X, x2.Y);
Gl.glVertex2f(p2.X, p2.Y);
break;
}
Gl.glEnd();
}
}
}

View File

@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TestBed.Net
{
public class Settings
{
public float Hz = 60;
public int IterationCount = 10;
public bool DrawStats = false;
public bool DrawContacts = false;
public bool DrawImpulses = false;
public bool DrawAABBs = false;
public bool DrawPairs = false;
public bool WarmStarting = true;
public bool PositionCorrection = true;
public bool Pause = false;
}
}

View File

@@ -0,0 +1,179 @@
using System;
using System.Collections.Generic;
using System.Text;
using Box2D.Net;
using Tao.OpenGl;
namespace TestBed.Net
{
public class Test
{
public World world;
public MouseJoint mouseJoint;
public Body bomb;
public float Zoom = 20;
public Vector ViewOffset = new Vector();
public Vector Mouse = null;
public Test()
{
AABB worldAABB = new AABB(new Vector(-100.0f, -100.0f), new Vector(100.0f, 200.0f));
Vector gravity = new Vector(0, -10);
bool doSleep = true;
world = new World(worldAABB, gravity, doSleep);
//m_textLine = 30;
//
//m_listener.test = this;
//m_world->SetListener(&m_listener);
}
public void Step(Settings settings)
{
if(settings.Pause)
return;
float timeStep = settings.Hz > 0 ? 1.0f / settings.Hz : 0;
World.WarmStarting = settings.WarmStarting;
World.PositionCorrection = settings.PositionCorrection;
world.Step(timeStep, settings.IterationCount);
//m_world->m_broadPhase->Validate();
}
Vector RelativeToWorld(Vector Relative)
{
Vector working = Relative;
working *= Zoom;
working -= ViewOffset;
return working;
}
/// <summary>
/// Handles what happens when a user clicks the mouse
/// </summary>
/// <param name="p">
/// The position of the mouse click in relative coordinates.
/// </param>
public void MouseDown(Vector point)
{
Vector p = RelativeToWorld(point);
if (mouseJoint != null)
throw new Exception("ASSERT: mouseJoint should be null");
// Make a small box.
Vector d = new Vector(.001f, .001f);
AABB aabb = new AABB(p - d, p + d);
// Query the world for overlapping shapes.
IList<Shape> shapes = world.Query(aabb);
Body body = null;
foreach (Shape shape in shapes)
{
if (shape.Body.Static == false &&
shape.TestPoint(shape.Body.GetXForm(), p))
{
body = shape.Body;
break;
}
}
if (body != null)
{
MouseJointDef md = new MouseJointDef();
md.Body1 = world.GetGroundBody();
md.Body2 = body;
md.Target = p;
md.MaxForce = 1000 * body.Mass;
mouseJoint = new MouseJoint(world.CreateJoint(md));
body.WakeUp();
}
}
public void MouseUp(Vector point)
{
Vector p = RelativeToWorld(point);
if (mouseJoint != null)
{
world.DestroyJoint(mouseJoint);
mouseJoint = null;
}
}
public void MouseMove(Vector point)
{
Vector p = RelativeToWorld(point);
if (mouseJoint != null)
mouseJoint.SetTarget(p);
}
public void KeyPress(System.Windows.Forms.Keys key)
{
switch (key)
{
case System.Windows.Forms.Keys.Space:
LaunchBomb();
break;
case System.Windows.Forms.Keys.Left:
ViewOffset.X -= 1;
break;
case System.Windows.Forms.Keys.Right:
ViewOffset.X += 1;
break;
case System.Windows.Forms.Keys.Up:
ViewOffset.Y += 1;
break;
case System.Windows.Forms.Keys.Down:
ViewOffset.Y -= 1;
break;
case System.Windows.Forms.Keys.X:
Zoom -= 1;
break;
case System.Windows.Forms.Keys.Z:
Zoom += 1;
break;
}
}
void LaunchBomb()
{
if (bomb != null)
{
world.DestroyBody(bomb);
bomb = null;
}
BodyDef bd = new BodyDef();
bd.BodyType = BodyType.e_dynamicBody;
bd.AllowSleep = true;
Random rand = new Random();
bd.Position = new Vector((float)rand.NextDouble() * 30 - 15, 30.0f);
bd.IsBullet = true;
bomb = world.CreateBody(bd);
bomb.LinearVelocity = bd.Position * -5;
CircleDef sd = new CircleDef();
sd.Radius = 0.3f;
sd.Density = 20.0f;
sd.Restitution = 0.1f;
bomb.CreateShape(sd);
bomb.SetMassFromShapes();
}
public override string ToString()
{
return GetType().Name;
}
};
}

View File

@@ -0,0 +1,74 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{B2F62680-048C-46AA-B1B2-7731252E1304}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TestBed.Net</RootNamespace>
<AssemblyName>TestBed.Net</AssemblyName>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Tao.FreeGlut, Version=2.4.0.1, Culture=neutral, PublicKeyToken=6e602a6ad6c0d06d, processorArchitecture=MSIL" />
<Reference Include="Tao.OpenGl, Version=2.1.0.4, Culture=neutral, PublicKeyToken=1ca010269a4501ef, processorArchitecture=MSIL" />
<Reference Include="Tao.Platform.Windows, Version=1.0.0.4, Culture=neutral, PublicKeyToken=701104b2da67a104, processorArchitecture=MSIL" />
</ItemGroup>
<ItemGroup>
<Compile Include="MainWindow.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="MainWindow.Designer.cs">
<DependentUpon>MainWindow.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Renderer.cs" />
<Compile Include="Settings.cs" />
<Compile Include="Test.cs" />
<Compile Include="Tests\Bridge.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Box2D.Net.vcproj">
<Project>{0E95DBB9-EA97-407B-811C-810B225E79D2}</Project>
<Name>Box2D.Net</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="MainWindow.resx">
<SubType>Designer</SubType>
<DependentUpon>MainWindow.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Text;
using Box2D.Net;
namespace TestBed.Net.Tests
{
public class Bridge : TestBed.Net.Test
{
public Bridge()
{
Body ground;
{
PolygonDef sd = new PolygonDef();
sd.ShapeType = ShapeType.e_polygonShape;
sd.SetAsBox(50.0f, 10.0f);
BodyDef bd = new BodyDef();
bd.Position = new Vector(0, -10);
ground = world.CreateBody(bd);
ground.CreateShape(sd);
}
{
PolygonDef sd = new PolygonDef();
sd.SetAsBox(0.5f, 0.125f);
sd.Density = 20.0f;
sd.Friction = 0.2f;
BodyDef bd = new BodyDef();
bd.BodyType = BodyType.e_dynamicBody;
RevoluteJointDef jd = new RevoluteJointDef();
const float numPlanks = 30;
Body prevBody = ground;
for (float i = 0; i < numPlanks; ++i)
{
bd.Position = new Vector(-14.5f + i, 5);
Body body = world.CreateBody(bd);
body.CreateShape(sd);
body.SetMassFromShapes();
Vector anchor = new Vector(-15 + i, 5);
jd.Initialize(prevBody, body, anchor);
world.CreateJoint(jd);
prevBody = body;
}
Vector anchor2 = new Vector(-15 + numPlanks, 5);
jd.Initialize(prevBody, ground, anchor2);
world.CreateJoint(jd);
}
}
}
}