Пример #1
0
 public WeldJointDef()
 {
     JointType = JointType.Weld;
     _localAnchorA = Vec2.Empty;
     _localAnchorB = Vec2.Empty;
     _referenceAngle = 0.0f;
 }
Пример #2
0
        public LineJoint()
        {
            Body ground = null;
            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));

                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);
                ground.CreateFixture(shape, 0.0f);
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(0.5f, 2.0f);

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(0.0f, 7.0f);
                Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 1.0f);

                LineJointDef jd = new LineJointDef();
                Vec2 axis = new Vec2(2.0f, 1.0f);
                axis.Normalize();
                jd.Initialize(ground, body, new Vec2(0.0f, 8.5f), axis);
                jd.MotorSpeed = 0.0f;
                jd.MaxMotorForce = 100.0f;
                jd.EnableMotor = true;
                jd.LowerTranslation = -4.0f;
                jd.UpperTranslation = 4.0f;
                jd.EnableLimit = true;
                m_world.CreateJoint(jd);
            }
        }
Пример #3
0
        public override void DrawSolidCircle(Vec2 center, float radius, Vec2 axis, ColorF color)
        {
            float k_segments = (int)(1024.0f * (radius / 50));
            float k_increment = (float)(2.0f * Math.PI / k_segments);
            float theta = 0.0f;
            Gl.glEnable(Gl.GL_BLEND);
            Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA);
            Gl.glColor4f(0.5f * color.R, 0.5f * color.G, 0.5f * color.B, 0.5f);
            Gl.glBegin(Gl.GL_TRIANGLE_FAN);
            for (int i = 0; i < k_segments; ++i)
            {
                Vec2 v = center + radius * new Vec2((float)Math.Cos(theta), (float)Math.Sin(theta));
                Gl.glVertex2f(v.X, v.Y);
                theta += k_increment;
            }
            Gl.glEnd();
            Gl.glDisable(Gl.GL_BLEND);

            theta = 0.0f;
            Gl.glColor4f(color.R, color.G, color.B, 1.0f);
            Gl.glBegin(Gl.GL_LINE_LOOP);
            for (int i = 0; i < k_segments; ++i)
            {
                Vec2 v = center + radius * new Vec2((float)Math.Cos(theta), (float)Math.Sin(theta));
                Gl.glVertex2f(v.X, v.Y);
                theta += k_increment;
            }
            Gl.glEnd();

            Vec2 p = center + radius * axis;
            Gl.glBegin(Gl.GL_LINES);
            Gl.glVertex2f(center.X, center.Y);
            Gl.glVertex2f(p.X, p.Y);
            Gl.glEnd();
        }
Пример #4
0
        void Report(Vec2 point, Vec2 normal)
        {
            _hit++;
            Vec2 sub = _start - point;
            float len = sub.Length();

            if (_hit > 100 || _start == point || len == 0)
                return;

            starts.Add(_start);
            ends.Add(point);

            var reflect = Reflect(_start, point, normal);

            _length -= len;
            _start = point;
            _normal = normal;

            if (_length > 0)
            {
                _world.RayCast(_callback, _start, _start - (reflect * _length));

                if (!_callback.m_hit || (_start == _callback.m_point))
                {
                    starts.Add(_start);
                    ends.Add(_start - (reflect * _length));
                }

                Report(_callback.m_point, _callback.m_normal);
            }
        }
Пример #5
0
 public FrictionJointDef()
 {
     JointType = JointType.Friction;
     _localAnchorA = Vec2.Empty;
     _localAnchorB = Vec2.Empty;
     _maxForce = 0.0f;
     _maxTorque = 0.0f;
 }
Пример #6
0
 public override void DrawSegment(Vec2 p1, Vec2 p2, ColorF color)
 {
     Gl.glColor3f(color.R, color.G, color.B);
     Gl.glBegin(Gl.GL_LINES);
     Gl.glVertex2f(p1.X, p1.Y);
     Gl.glVertex2f(p2.X, p2.Y);
     Gl.glEnd();
 }
Пример #7
0
 public override void MouseMove(Vec2 p)
 {
     if (_moving)
     {
         newPos = p;
     }
     base.MouseMove(p);
 }
Пример #8
0
 /// Initialize the bodies, anchors, and reference angle using a world
 /// anchor point.
 public void Initialize(Body body1, Body body2, Vec2 anchor)
 {
     BodyA = body1;
     BodyB = body2;
     _localAnchorA = BodyA.GetLocalPoint(anchor);
     _localAnchorB = BodyB.GetLocalPoint(anchor);
     _referenceAngle = BodyB.Angle - BodyA.Angle;
 }
Пример #9
0
 public void DrawPoint(Vec2 p, float size, ColorF color)
 {
     Gl.glPointSize(size);
     Gl.glBegin(Gl.GL_POINTS);
     Gl.glColor3f(color.R, color.G, color.B);
     Gl.glVertex2f(p.X, p.Y);
     Gl.glEnd();
     Gl.glPointSize(1.0f);
 }
Пример #10
0
 public DistanceJointDef()
 {
     JointType = JointType.Distance;
     _localAnchorA = Vec2.Empty;
     _localAnchorB = Vec2.Empty;
     _length = 1.0f;
     _frequencyHz = 0.0f;
     _dampingRatio = 0.0f;
 }
Пример #11
0
 /// Does this aabb contain the provided AABB.
 public bool Contains(Vec2 pt)
 {
     bool result = true;
     result = result && LowerBound.X <= pt.X;
     result = result && LowerBound.Y <= pt.Y;
     result = result && pt.X <= UpperBound.X;
     result = result && pt.Y <= UpperBound.Y;
     return result;
 }
Пример #12
0
 public override void DrawPolygon(Vec2[] vertices, int vertexCount, ColorF color)
 {
     Gl.glColor3f(color.R, color.G, color.B);
     Gl.glBegin(Gl.GL_LINE_LOOP);
     for (int i = 0; i < vertexCount; ++i)
     {
         Gl.glVertex2f(vertices[i].X, vertices[i].Y);
     }
     Gl.glEnd();
 }
Пример #13
0
 public void DrawArc(Vec2 pos, float radius, float startAngle, float endAngle)
 {
     Gl.glPushMatrix();
     Gl.glTranslatef(pos.X, pos.Y, 0);
     var xx = Glu.gluNewQuadric();
     Glu.gluQuadricDrawStyle(xx, Glu.GLU_SILHOUETTE);
     Glu.gluPartialDisk(xx, radius, radius, 24, 1, b2Math.Rad2Deg(startAngle), b2Math.Rad2Deg(endAngle - startAngle));
     Glu.gluDeleteQuadric(xx);
     Gl.glPopMatrix();
 }
Пример #14
0
 public PulleyJointDef()
 {
     JointType = JointType.Pulley;
     _groundAnchorA = new Vec2(-1.0f, 1.0f);
     _groundAnchorB = new Vec2(1.0f, 1.0f);
     _localAnchorA = new Vec2(-1.0f, 0.0f);
     _localAnchorB = new Vec2(1.0f, 0.0f);
     _lengthA = 0.0f;
     _maxLengthA = 0.0f;
     _lengthB = 0.0f;
     _maxLengthB = 0.0f;
     _ratio = 1.0f;
     CollideConnected = true;
 }
Пример #15
0
 public override void DrawCircle(Vec2 center, float radius, ColorF color)
 {
     float k_segments = (int)(1024.0f * (radius / 50));
     float k_increment = (float)(2.0f * Math.PI / k_segments);
     float theta = 0.0f;
     Gl.glColor3f(color.R, color.G, color.B);
     Gl.glBegin(Gl.GL_LINE_LOOP);
     for (int i = 0; i < k_segments; ++i)
     {
         Vec2 v = center + radius * new Vec2((float)Math.Cos(theta), (float)Math.Sin(theta));
         Gl.glVertex2f(v.X, v.Y);
         theta += k_increment;
     }
     Gl.glEnd();
 }
Пример #16
0
        public Prismatic()
        {
            Body ground = null;
            {
                BodyDef bd = new BodyDef();
                ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                PolygonShape shape = new PolygonShape();
                shape.SetAsBox(2.0f, 0.5f);

                BodyDef bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Position = new Vec2(-10.0f, 10.0f);
                bd.Angle = 0.5f * (float)Math.PI;
                bd.AllowSleep = false;
                Body body = m_world.CreateBody(bd);
                body.CreateFixture(shape, 5.0f);

                PrismaticJointDef pjd = new PrismaticJointDef();

                // Bouncy limit
                Vec2 axis = new Vec2(2.0f, 1.0f);
                axis.Normalize();
                pjd.Initialize(ground, body, new Vec2(0.0f, 0.0f), axis);

                // Non-bouncy limit
                //pjd.Initialize(ground, body, new Vec2(-10.0f, 10.0f), new Vec2(1.0f, 0.0f));

                pjd.MotorSpeed = 10.0f;
                pjd.MaxMotorForce = 10000.0f;
                pjd.EnableMotor = true;
                pjd.LowerTranslation = 0.0f;
                pjd.UpperTranslation = 20.0f;
                pjd.EnableLimit = false;

                m_joint = (PrismaticJoint)m_world.CreateJoint(pjd);
            }
        }
Пример #17
0
        /// Main...
        public ElasticBody()
        {
            Program.MainForm.ViewZoom = 0.25f;

            /// Bottom static body
            {
                PolygonShape sd = new PolygonShape();
                sd.SetAsBox(50.0f, 2.0f);
                BodyDef bd = new BodyDef();
                bd.Position = new Vec2(-1.0f, -7.5f);

                m_ground = m_world.CreateBody(bd);
                m_ground.CreateFixture(new FixtureDef(sd, 0.0f, 0.1f, 0.1f));
            }

            /// "Elastic body" 64 bodies - something like a lin. elastic compound
            /// connected via dynamic forces (springs)
            {
                PolygonShape sd = new PolygonShape();
                sd.SetAsBox(width, height);

                FixtureDef sdf = new FixtureDef();
                sdf.Density    = 1.5f;
                sdf.Friction   = 0.01f;
                sdf.Filter.GroupIndex = -1;
                sdf.Shape = sd;
                Vec2 startpoint = new Vec2(0, 0);
                BodyDef    bd = new BodyDef();
                bd.BodyType = BodyType.Dynamic;
                bd.Bullet = false;
              	 			//bd.AllowSleep = false;
                for (int i = 0; i < BodyCountY; ++i)
                {
                    for (int j = 0; j < BodyCountX; ++j)
                    {
                        bd.Position = new Vec2(j*(width*2), 2.51f + (height*2) * i);
                        bd.Position  += startpoint;
                        Body body  = m_world.CreateBody(bd);
                        bodies[BodyCountX*i+j] = body;
                        body.CreateFixture(sdf);
                    }
                }
            }
        }
Пример #18
0
        public void Apply()
        {
            if (!string.IsNullOrEmpty(toolStripStatusLabel1.Text))
            {
                MessageBox.Show("You have errors in your polygon. Please fix these before applying:\n\n"+toolStripStatusLabel1.ToolTipText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Vec2[] vertices = new Vec2[PolyData.Vertices.Count];

            for (int i = 0; i < PolyData.Vertices.Count; ++i)
            {
                vertices[i] = (new Vec2((PolyData.Vertices[i].X - (pictureBox1.Width / 2)) / pictureBox1.Width, (PolyData.Vertices[i].Y - (pictureBox1.Height / 2)) / pictureBox1.Height)) * (float)PolyData.Scale;
            }

            SelectedShape.Vertices = vertices;

            if ((Program.MainForm.SelectedNode.Node.Parent as FixtureNode).OwnedBody != null &&
                (Program.MainForm.SelectedNode.Node.Parent as FixtureNode).OwnedBody.AutoMassRecalculate)
                (Program.MainForm.SelectedNode.Node.Parent as FixtureNode).OwnedBody.RecalculateMass();
        }
Пример #19
0
        public Body[] GenerateBodies(World world, Vec2 basePosition, FixtureDef def)
        {
            if (_vecs.Count <= 1)
                throw new ArgumentOutOfRangeException("Vecs");

            Body[] bodies = new Body[_vecs.Count - 1];

            for (int i = 0; i < _vecs.Count - 1; ++i)
            {
                PolygonShape edge = new PolygonShape(_vecs[i], _vecs[i + 1]);

                BodyDef bd = new BodyDef();
                bd.Position = basePosition;

                bodies[i] = world.CreateBody(bd);
                bodies[i].CreateFixture(new FixtureDef(edge, 0, def.Restitution, def.Friction, false, def.UserData));
            }

            return bodies;
        }
Пример #20
0
        void CreateCircle()
        {
            float radius = 2.0f;
            CircleShape shape = new CircleShape();
            shape.Position = Vec2.Empty;
            shape.Radius = radius;

            FixtureDef fd = new FixtureDef();
            fd.Shape = shape;
            fd.Density = 1.0f;
            fd.Friction = 0.0f;

            Vec2 p = new Vec2(Rand.RandomFloat(), 3.0f + Rand.RandomFloat());
            BodyDef bd = new BodyDef();
            bd.BodyType = BodyType.Dynamic;
            bd.Position = p;
            //bd.allowSleep = false;
            Body body = m_world.CreateBody(bd);

            body.CreateFixture(fd);
        }
Пример #21
0
        public void Start(World world, float totalLength, Vec2 start, Vec2 normal)
        {
            Clear();

            _hit = 0;
            _totalLength = totalLength;
            _length = totalLength;
            _start = start;
            _normal = normal;
            _world = world;

            // Send a beam out
            _callback.m_hit = false;
            _world.RayCast(_callback, start, start + (normal * _length));

            if (_callback.m_hit)
                Report(_callback.m_point, _callback.m_normal);
            else
            {
                starts.Add(_start);
                ends.Add(_start  + (_normal * _length));
            }
        }
Пример #22
0
 /// Initialize the bodies, anchors, and length using the world
 /// anchors.
 public void Initialize(Body bodyA, Body bodyB,
     Vec2 anchorA, Vec2 anchorB)
 {
     BodyA = bodyA;
     BodyB = bodyB;
     _localAnchorA = bodyA.GetLocalPoint(anchorA);
     _localAnchorB = bodyB.GetLocalPoint(anchorB);
     Vec2 d = anchorB - anchorA;
     _length = d.Length();
 }
Пример #23
0
        public override float ReportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction)
        {
            Body body = fixture.Body;
            object userData = body.UserData;
            if (userData != null && userData is int)
            {
                int index = (int)userData;
                if (index == 0)
                {
                    // filter
                    return -1.0f;
                }
            }

            //Assert(m_count < e_maxCount);

            m_points.Add(point);
            m_normals.Add(normal);
            ++m_count;

            // (m_count == e_maxCount)
            //{
            //	return 0.0f;
            //}

            return 1.0f;
        }
Пример #24
0
        public override float ReportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction)
        {
            Body body = fixture.Body;
            object userData = body.UserData;

            if (userData != null && userData is int)
            {
                int index = (int)userData;

                if (index == 0)
                {
                    // filter
                    return -1.0f;
                }
            }

            m_hit = true;
            m_point = point;
            m_normal = normal;
            return fraction;
        }
Пример #25
0
        public RayCast()
        {
            // Ground body
            {
                BodyDef bd = new BodyDef();
                Body ground = m_world.CreateBody(bd);

                PolygonShape shape = new PolygonShape();
                shape.SetAsEdge(new Vec2(-40.0f, 0.0f), new Vec2(40.0f, 0.0f));
                ground.CreateFixture(shape, 0.0f);
            }

            {
                Vec2[] vertices = new Vec2[3];
                vertices[0] = new Vec2(-0.5f, 0.0f);
                vertices[1] = new Vec2(0.5f, 0.0f);
                vertices[2] = new Vec2(0.0f, 1.5f);
                m_polygons[0] = new PolygonShape();
                m_polygons[0].Vertices = vertices;
            }

            {
                Vec2[] vertices = new Vec2[3];
                vertices[0] = new Vec2(-0.1f, 0.0f);
                vertices[1] = new Vec2(0.1f, 0.0f);
                vertices[2] = new Vec2(0.0f, 1.5f);
                m_polygons[1] = new PolygonShape();
                m_polygons[1].Vertices = vertices;
            }

            {
                float w = 1.0f;
                float b = w / (2.0f + (float)Math.Sqrt(2.0f));
                float s = (float)Math.Sqrt(2.0f) * b;

                Vec2[] vertices = new Vec2[8];
                vertices[0] = new Vec2(0.5f * s, 0.0f);
                vertices[1] = new Vec2(0.5f * w, b);
                vertices[2] = new Vec2(0.5f * w, b + s);
                vertices[3] = new Vec2(0.5f * s, w);
                vertices[4] = new Vec2(-0.5f * s, w);
                vertices[5] = new Vec2(-0.5f * w, b + s);
                vertices[6] = new Vec2(-0.5f * w, b);
                vertices[7] = new Vec2(-0.5f * s, 0.0f);

                m_polygons[2] = new PolygonShape();
                m_polygons[2].Vertices = vertices;
            }

            {
                m_polygons[3] = new PolygonShape();
                m_polygons[3].SetAsBox(0.5f, 0.5f);
            }

            {
                m_circle = new CircleShape();
                m_circle.Radius = 0.5f;
            }

            m_bodyIndex = 0;
            m_angle = 0.0f;

            m_mode = Mode.e_closest;
        }
Пример #26
0
        public override void Step()
        {
            bool advanceRay = TestSettings.pause == false || TestSettings.singleStep;

            base.Step();

            float L = 11.0f;
            Vec2 point1 = new Vec2(0.0f, 10.0f);
            Vec2 d = new Vec2(L * (float)Math.Cos(m_angle), L * (float)Math.Sin(m_angle));
            Vec2 point2 = point1 + d;

            if (m_mode == Mode.e_closest)
            {
                _callback = new RayCastClosestCallback();
                m_world.RayCast(_callback, point1, point2);
            }
            else if (m_mode == Mode.e_any)
            {
                _callback = new RayCastAnyCallback();
                m_world.RayCast(_callback, point1, point2);
            }
            else if (m_mode == Mode.e_multiple)
            {
                _callback = new RayCastMultipleCallback();
                m_world.RayCast(_callback, point1, point2);
            }

            if (advanceRay)
            {
                m_angle += (float)(0.25 * Math.PI / 180.0);
            }
        }
Пример #27
0
        public override void Draw()
        {
            base.Draw();

            m_debugDraw.DrawString(5, m_textLine, "Press 1-5 to place stuff, m to change the mode");
            m_textLine += 15;
            m_debugDraw.DrawString(5, m_textLine, "Mode = "+m_mode.ToString());
            m_textLine += 15;

            if (_callback == null)
                return;

            float L = 11.0f;
            Vec2 point1 = new Vec2(0.0f, 10.0f);
            Vec2 d = new Vec2(L * (float)Math.Cos(m_angle), L * (float)Math.Sin(m_angle));
            Vec2 point2 = point1 + d;

            if (_callback is RayCastClosestCallback)
            {
                RayCastClosestCallback callback = (RayCastClosestCallback)_callback;
                m_world.RayCast(_callback, point1, point2);

                if (callback.m_hit)
                {
                    m_debugDraw.DrawPoint(callback.m_point, 5.0f, new ColorF(0.4f, 0.9f, 0.4f));
                    m_debugDraw.DrawSegment(point1, callback.m_point, new ColorF(0.8f, 0.8f, 0.8f));
                    Vec2 head = callback.m_point + 0.5f * callback.m_normal;
                    m_debugDraw.DrawSegment(callback.m_point, head, new ColorF(0.9f, 0.9f, 0.4f));
                }
                else
                {
                    m_debugDraw.DrawSegment(point1, point2, new ColorF(0.8f, 0.8f, 0.8f));
                }
            }
            else if (_callback is RayCastAnyCallback)
            {
                RayCastAnyCallback callback = (RayCastAnyCallback)_callback;
                _callback = new RayCastAnyCallback();

                if (callback.m_hit)
                {
                    m_debugDraw.DrawPoint(callback.m_point, 5.0f, new ColorF(0.4f, 0.9f, 0.4f));
                    m_debugDraw.DrawSegment(point1, callback.m_point, new ColorF(0.8f, 0.8f, 0.8f));
                    Vec2 head = callback.m_point + 0.5f * callback.m_normal;
                    m_debugDraw.DrawSegment(callback.m_point, head, new ColorF(0.9f, 0.9f, 0.4f));
                }
                else
                {
                    m_debugDraw.DrawSegment(point1, point2, new ColorF(0.8f, 0.8f, 0.8f));
                }
            }
            else if (_callback is RayCastMultipleCallback)
            {
                RayCastMultipleCallback callback = (RayCastMultipleCallback)_callback;
                m_debugDraw.DrawSegment(point1, point2, new ColorF(0.8f, 0.8f, 0.8f));

                for (int i = 0; i < callback.m_count; ++i)
                {
                    Vec2 p = callback.m_points[i];
                    Vec2 n = callback.m_normals[i];
                    m_debugDraw.DrawPoint(p, 5.0f, new ColorF(0.4f, 0.9f, 0.4f));
                    m_debugDraw.DrawSegment(point1, p, new ColorF(0.8f, 0.8f, 0.8f));
                    Vec2 head = p + 0.5f * n;
                    m_debugDraw.DrawSegment(p, head, new ColorF(0.9f, 0.9f, 0.4f));
                }
            }
        }
Пример #28
0
 /// Initialize the bodies, anchors, axis, and reference angle using the world
 /// anchor and world axis.
 public void Initialize(Body bodyA, Body bodyB, Vec2 anchor, Vec2 axis)
 {
     BodyA = bodyA;
     BodyB = bodyB;
     _localAnchorA = bodyA.GetLocalPoint(anchor);
     _localAnchorB = bodyB.GetLocalPoint(anchor);
     _localAxisA = bodyA.GetLocalVector(axis);
 }
Пример #29
0
 public LineJointDef()
 {
     JointType = JointType.Line;
     _localAnchorA = Vec2.Empty;
     _localAnchorB = Vec2.Empty;
     _localAxisA = new Vec2(1.0f, 0.0f);
     _enableLimit = false;
     _lowerTranslation = 0.0f;
     _upperTranslation = 0.0f;
     _enableMotor = false;
     _maxMotorForce = 0.0f;
     _motorSpeed = 0.0f;
 }
Пример #30
0
 /// Combine two AABBs into this one.
 public void Combine(AABB aabb1, AABB aabb2)
 {
     LowerBound = Vec2.Min(aabb1.LowerBound, aabb2.LowerBound);
     UpperBound = Vec2.Min(aabb1.UpperBound, aabb2.UpperBound);
 }