Test.cs
上传用户:gb3593
上传日期:2022-01-07
资源大小:3028k
文件大小:5k
源码类别:

游戏引擎

开发平台:

Visual C++

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Box2D.Net;
  5. using Tao.OpenGl;
  6. namespace TestBed.Net
  7. {
  8.     public class Test
  9.     {
  10.         public World world;
  11.         public MouseJoint mouseJoint;
  12.         public Body bomb;
  13.         public float Zoom = 20;
  14.         public Vector ViewOffset = new Vector();
  15.         public Vector Mouse = null;
  16.         public Test()
  17.         {
  18.             AABB worldAABB = new AABB(new Vector(-100.0f, -100.0f), new Vector(100.0f, 200.0f));
  19.             Vector gravity = new Vector(0, -10);
  20.             bool doSleep = true;
  21.             world = new World(worldAABB, gravity, doSleep);
  22.             
  23.             //m_textLine = 30;
  24.             //
  25.             //m_listener.test = this;
  26.             //m_world->SetListener(&m_listener);
  27.         }
  28.         public void Step(Settings settings)
  29.         {
  30.             if(settings.Pause)
  31.                 return;
  32.             float timeStep = settings.Hz > 0 ? 1.0f / settings.Hz : 0;
  33.             World.WarmStarting = settings.WarmStarting;
  34.             World.PositionCorrection = settings.PositionCorrection;
  35.         
  36.             world.Step(timeStep, settings.IterationCount);
  37.             //m_world->m_broadPhase->Validate();            
  38.         }
  39.         Vector RelativeToWorld(Vector Relative)
  40.         {
  41.             Vector working = Relative;
  42.             working *= Zoom;
  43.             working -= ViewOffset;
  44.             return working;
  45.         }
  46.         /// <summary>
  47.         /// Handles what happens when a user clicks the mouse
  48.         /// </summary>
  49.         /// <param name="p">
  50.         /// The position of the mouse click in relative coordinates.
  51.         /// </param>
  52.         public void MouseDown(Vector point)
  53.         {
  54.             Vector p = RelativeToWorld(point);
  55.             if (mouseJoint != null)
  56.                 throw new Exception("ASSERT: mouseJoint should be null");
  57.             // Make a small box.
  58.             Vector d = new Vector(.001f, .001f);
  59.             AABB aabb = new AABB(p - d, p + d);
  60.             // Query the world for overlapping shapes.
  61.             IList<Shape> shapes = world.Query(aabb);
  62.             Body body = null;
  63.             foreach (Shape shape in shapes)
  64.             {
  65.                 if (shape.Body.Static == false &&
  66.                     shape.TestPoint(shape.Body.GetXForm(), p))
  67.                 {
  68.                     body = shape.Body;
  69.                     break;
  70.                 }
  71.             }
  72.             if (body != null)
  73.             {
  74.                 MouseJointDef md = new MouseJointDef();
  75.                 md.Body1 = world.GetGroundBody();
  76.                 md.Body2 = body;
  77.                 md.Target = p;
  78.                 md.MaxForce = 1000 * body.Mass;
  79.                 mouseJoint = new MouseJoint(world.CreateJoint(md));
  80.                 body.WakeUp();
  81.             }
  82.         }
  83.         public void MouseUp(Vector point)
  84.         {
  85.             Vector p = RelativeToWorld(point);
  86.             if (mouseJoint != null)
  87.             {
  88.                 world.DestroyJoint(mouseJoint);
  89.                 mouseJoint = null;
  90.             }
  91.         }
  92.         public void MouseMove(Vector point)
  93.         {
  94.             Vector p = RelativeToWorld(point);
  95.             if (mouseJoint != null)
  96.                 mouseJoint.SetTarget(p);
  97.         }
  98.         public void KeyPress(System.Windows.Forms.Keys key)
  99.         {
  100.             switch (key)
  101.             {
  102.                 case System.Windows.Forms.Keys.Space:
  103.                     LaunchBomb();
  104.                     break;
  105.                 
  106.                 case System.Windows.Forms.Keys.Left:
  107.                     ViewOffset.X -= 1;
  108.                     break;
  109.                 case System.Windows.Forms.Keys.Right:
  110.                     ViewOffset.X += 1;
  111.                     break;
  112.                 case System.Windows.Forms.Keys.Up:
  113.                     ViewOffset.Y += 1;
  114.                     break;
  115.                 case System.Windows.Forms.Keys.Down:
  116.                     ViewOffset.Y -= 1;
  117.                     break;
  118.                 case System.Windows.Forms.Keys.X:
  119.                     Zoom -= 1;
  120.                     break;
  121.                 case System.Windows.Forms.Keys.Z:
  122.                     Zoom += 1;
  123.                     break;                    
  124.             }
  125.         }
  126.         void LaunchBomb()
  127.         {
  128.         if (bomb != null)
  129.         {
  130.                 world.DestroyBody(bomb);
  131.                 bomb = null;
  132.         }
  133.             BodyDef bd = new BodyDef();
  134.         bd.BodyType = BodyType.e_dynamicBody;
  135.         bd.AllowSleep = true;
  136.             Random rand = new Random();
  137.             bd.Position = new Vector((float)rand.NextDouble() * 30 - 15, 30.0f);
  138.         bd.IsBullet = true;
  139.             bomb = world.CreateBody(bd);
  140.         bomb.LinearVelocity = bd.Position * -5;
  141.             CircleDef sd = new CircleDef();
  142.         sd.Radius = 0.3f;
  143.         sd.Density = 20.0f;
  144.         sd.Restitution = 0.1f;
  145.             bomb.CreateShape(sd);
  146.             bomb.SetMassFromShapes();
  147.         }
  148.         public override string ToString()
  149.         {
  150.             return GetType().Name;
  151.         }
  152.     };
  153. }