summaryrefslogtreecommitdiff
path: root/Box2d/Assets/Program/Test/ApplicationMain.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Box2d/Assets/Program/Test/ApplicationMain.cs')
-rw-r--r--Box2d/Assets/Program/Test/ApplicationMain.cs112
1 files changed, 101 insertions, 11 deletions
diff --git a/Box2d/Assets/Program/Test/ApplicationMain.cs b/Box2d/Assets/Program/Test/ApplicationMain.cs
index bec4540..15aca5d 100644
--- a/Box2d/Assets/Program/Test/ApplicationMain.cs
+++ b/Box2d/Assets/Program/Test/ApplicationMain.cs
@@ -8,30 +8,120 @@ using Box2DX.Common;
public class ApplicationMain : MonoBehaviour
{
+ public Material material;
+ private World world;
+ private Body body;
+ private Body groundBody;
- public Material material;
+ private Vec2 translate(Vec2 p)
+ {
+ //return new Vec2(p.X * 3 + 100, p.Y * 3 + 100);
+ return new Vec2(p.X, p.Y);
+ }
- void OnPostRender()
+ private void Awake()
+ {
+ material.SetPass(0);//设置该材质通道,0为默认值
+
+ // if bodies reach the end of the world, but it will be slower.
+ AABB worldAABB = new AABB();
+ worldAABB.LowerBound.Set(-100.0f, -100.0f);
+ worldAABB.UpperBound.Set(100.0f, 100.0f);
+
+ // Define the gravity vector.
+ Vec2 gravity = new Vec2(0.0f, -10.0f);
+
+ // Do we want to let bodies sleep?
+ bool doSleep = true;
+
+ // Construct a world object, which will hold and simulate the rigid bodies.
+ world = new World(worldAABB, gravity, doSleep);
+
+ // Define the ground body.
+ BodyDef groundBodyDef = new BodyDef();
+ groundBodyDef.Position.Set(0.0f, -10.0f);
+
+ // Call the body factory which creates the ground box shape.
+ // The body is also added to the world.
+ groundBody = world.CreateBody(groundBodyDef);
+
+ // Define the ground box shape.
+ PolygonDef groundShapeDef = new PolygonDef();
+
+ // The extents are the half-widths of the box.
+ groundShapeDef.SetAsBox(50.0f, 10.0f);
+
+ // Add the ground shape to the ground body.
+ groundBody.CreateShape(groundShapeDef);
+
+ // Define the dynamic body. We set its position and call the body factory.
+ BodyDef bodyDef = new BodyDef();
+ bodyDef.Position.Set(0.0f, 80.0f);
+ body = world.CreateBody(bodyDef);
+
+ // Define another box shape for our dynamic body.
+ PolygonDef shapeDef = new PolygonDef();
+ shapeDef.SetAsBox(1.0f, 1.0f);
+
+ // Set the box density to be non-zero, so it will be dynamic.
+ shapeDef.Density = 1.0f;
+
+ // Override the default friction.
+ shapeDef.Friction = 0.3f;
+
+ // Add the shape to the body.
+ body.CreateShape(shapeDef);
+
+ // Now tell the dynamic body to compute it's mass properties base
+ // on its shape.
+ body.SetMassFromShapes();
+
+ // Prepare for simulation. Typically we use a time step of 1/60 of a
+ // second (60Hz) and 10 iterations. This provides a high quality simulation
+ // in most game scenarios.
+ }
+
+ private void Update()
+ {
+ float timeStep = 1.0f / 60.0f;
+ int velocityIterations = 8;
+ int positionIterations = 1;
+ world.Step(timeStep, velocityIterations, positionIterations);
+ }
+
+ private void OnRenderObject()
{
//绘制正四边形,提供的坐标必须是顺时针或者逆时针
- Draw(100, 100, 100, 200, 200, 200, 200, 100);
- //绘制无规则四边形
- Draw(15, 5, 10, 115, 95, 110, 90, 10);
+ Vec2 position = body.GetPosition();
+ Draw(
+ translate(new Vec2(position.X + 1, position.Y + 1))
+ , translate(new Vec2(position.X + 1, position.Y - 1))
+ , translate(new Vec2(position.X - 1, position.Y - 1))
+ , translate(new Vec2(position.X - 1, position.Y + 1))
+ );
+ position = groundBody.GetPosition();
+ Draw(
+ translate(new Vec2(position.X + 50, position.Y + 10))
+ , translate(new Vec2(position.X + 50, position.Y - 10))
+ , translate(new Vec2(position.X - 50, position.Y - 10))
+ , translate(new Vec2(position.X - 50, position.Y + 10))
+ );
}
//绘制四边形,四个点坐标
- void Draw(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
+ void Draw(Vec2 p1, Vec2 p2, Vec2 p3, Vec2 p4)
{
GL.PushMatrix();
- //material.SetPass(0);//设置该材质通道,0为默认值
+ material.SetPass(0);//设置该材质通道,0为默认值
+
GL.LoadOrtho();//设置绘制2d图像
GL.Begin(GL.QUADS);//绘制类型为四边形
- GL.Vertex3(x1 / Screen.width, y1 / Screen.height, 0);
- GL.Vertex3(x2 / Screen.width, y2 / Screen.height, 0);
- GL.Vertex3(x3 / Screen.width, y3 / Screen.height, 0);
- GL.Vertex3(x4 / Screen.width, y4 / Screen.height, 0);
+ GL.Vertex3(p1.X / Screen.width, p1.Y / Screen.height, 0);
+ GL.Vertex3(p2.X / Screen.width, p2.Y / Screen.height, 0);
+ GL.Vertex3(p3.X / Screen.width, p3.Y / Screen.height, 0);
+ GL.Vertex3(p4.X / Screen.width, p4.Y / Screen.height, 0);
GL.End();
GL.PopMatrix();