1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 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;
}
};
}
|