using System.Collections; using System.Collections.Generic; using UnityEngine; using Box2DX; using Box2DX.Dynamics; using Box2DX.Collision; using Box2DX.Common; public class ApplicationMain : MonoBehaviour { public Material material; private World world; private Body body; private Body groundBody; private Vec2 translate(Vec2 p) { //return new Vec2(p.X * 3 + 100, p.Y * 3 + 100); return new Vec2(p.X, p.Y); } 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() { //绘制正四边形,提供的坐标必须是顺时针或者逆时针 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(Vec2 p1, Vec2 p2, Vec2 p3, Vec2 p4) { GL.PushMatrix(); material.SetPass(0);//设置该材质通道,0为默认值 GL.LoadOrtho();//设置绘制2d图像 GL.Begin(GL.QUADS);//绘制类型为四边形 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(); } }