A debug view shows you what happens inside the physics engine. You can view bodies, joints, fixtures and more.
Inheritance: RenderableComponent, IDisposable
Beispiel #1
0
        static void DrawJoint(FSDebugView instance, Joint joint)
        {
            if (!joint.Enabled)
            {
                return;
            }

            var b1 = joint.BodyA;
            var b2 = joint.BodyB;

            FarseerPhysics.Common.Transform xf1;
            b1.GetTransform(out xf1);

            var x2 = System.Numerics.Vector2.Zero;

            if (b2 != null || !joint.IsFixedType())
            {
                FarseerPhysics.Common.Transform xf2;
                b2.GetTransform(out xf2);
                x2 = xf2.P;
            }

            var p1 = joint.WorldAnchorA;
            var p2 = joint.WorldAnchorB;
            var x1 = xf1.P;

            var color = new Color(0.5f, 0.8f, 0.8f);

            switch (joint.JointType)
            {
            case JointType.Distance:
            {
                instance.DrawSegment(p1, p2, color);
                break;
            }

            case JointType.Pulley:
            {
                var pulley = (PulleyJoint)joint;
                var s1     = b1.GetWorldPoint(pulley.LocalAnchorA);
                var s2     = b2.GetWorldPoint(pulley.LocalAnchorB);
                instance.DrawSegment(p1, p2, color);
                instance.DrawSegment(p1, s1, color);
                instance.DrawSegment(p2, s2, color);
                break;
            }

            case JointType.FixedMouse:
            {
                instance.DrawPoint(p1.ToXna(), 0.2f, new Color(0.0f, 1.0f, 0.0f));
                instance.DrawSegment(p1, p2, new Color(0.8f, 0.8f, 0.8f));
                break;
            }

            case JointType.Revolute:
            {
                instance.DrawSegment(x1, p1, color);
                instance.DrawSegment(p1, p2, color);
                instance.DrawSegment(x2, p2, color);

                instance.DrawSolidCircle(p2, 0.1f, System.Numerics.Vector2.Zero, Color.Red);
                instance.DrawSolidCircle(p1, 0.1f, System.Numerics.Vector2.Zero, Color.Blue);
                break;
            }

            case JointType.Gear:
            {
                instance.DrawSegment(x1, x2, color);
                break;
            }

            default:
            {
                instance.DrawSegment(x1, p1, color);
                instance.DrawSegment(p1, p2, color);
                instance.DrawSegment(x2, p2, color);
                break;
            }
            }
        }
        /// <summary>
        /// Call this to draw shapes and other debug draw data.
        /// </summary>
        void drawDebugData()
        {
            if ((flags & DebugViewFlags.Shape) == DebugViewFlags.Shape)
            {
                foreach (Body b in world.bodyList)
                {
                    FarseerPhysics.Common.Transform xf;
                    b.getTransform(out xf);
                    foreach (Fixture f in b.fixtureList)
                    {
                        if (b.enabled == false)
                        {
                            drawShape(f, xf, inactiveShapeColor);
                        }
                        else if (b.bodyType == BodyType.Static)
                        {
                            drawShape(f, xf, staticShapeColor);
                        }
                        else if (b.bodyType == BodyType.Kinematic)
                        {
                            drawShape(f, xf, kinematicShapeColor);
                        }
                        else if (b.isAwake == false)
                        {
                            drawShape(f, xf, sleepingShapeColor);
                        }
                        else
                        {
                            drawShape(f, xf, defaultShapeColor);
                        }
                    }
                }
            }

            if ((flags & DebugViewFlags.ContactPoints) == DebugViewFlags.ContactPoints)
            {
                const float axisScale = 0.3f;

                for (int i = 0; i < _pointCount; ++i)
                {
                    ContactPoint point = _points[i];

                    if (point.state == PointState.Add)
                    {
                        drawPoint(point.position, 0.1f, new Color(0.3f, 0.95f, 0.3f));
                    }
                    else if (point.state == PointState.Persist)
                    {
                        drawPoint(point.position, 0.1f, new Color(0.3f, 0.3f, 0.95f));
                    }

                    if ((flags & DebugViewFlags.ContactNormals) == DebugViewFlags.ContactNormals)
                    {
                        Vector2 p1 = point.position;
                        Vector2 p2 = p1 + axisScale * point.normal;
                        drawSegment(p1, p2, new Color(0.4f, 0.9f, 0.4f));
                    }
                }

                _pointCount = 0;
            }

            if ((flags & DebugViewFlags.PolygonPoints) == DebugViewFlags.PolygonPoints)
            {
                foreach (Body body in world.bodyList)
                {
                    foreach (Fixture f in body.fixtureList)
                    {
                        var polygon = f.shape as PolygonShape;
                        if (polygon != null)
                        {
                            FarseerPhysics.Common.Transform xf;
                            body.getTransform(out xf);

                            for (int i = 0; i < polygon.vertices.Count; i++)
                            {
                                Vector2 tmp = MathUtils.mul(ref xf, polygon.vertices[i]);
                                drawPoint(tmp, 0.1f, Color.Red);
                            }
                        }
                    }
                }
            }

            if ((flags & DebugViewFlags.Joint) == DebugViewFlags.Joint)
            {
                foreach (var j in world.jointList)
                {
                    FSDebugView.drawJoint(this, j);
                }
            }

            if ((flags & DebugViewFlags.AABB) == DebugViewFlags.AABB)
            {
                var color = new Color(0.9f, 0.3f, 0.9f);
                var bp    = world.contactManager.broadPhase;

                foreach (var body in world.bodyList)
                {
                    if (body.enabled == false)
                    {
                        continue;
                    }

                    foreach (var f in body.fixtureList)
                    {
                        for (var t = 0; t < f.proxyCount; ++t)
                        {
                            var  proxy = f.proxies[t];
                            AABB aabb;
                            bp.getFatAABB(proxy.proxyId, out aabb);

                            drawAABB(ref aabb, color);
                        }
                    }
                }
            }

            if ((flags & DebugViewFlags.CenterOfMass) == DebugViewFlags.CenterOfMass)
            {
                foreach (Body b in world.bodyList)
                {
                    FarseerPhysics.Common.Transform xf;
                    b.getTransform(out xf);
                    xf.p = b.worldCenter;
                    drawTransform(ref xf);
                }
            }

            if ((flags & DebugViewFlags.Controllers) == DebugViewFlags.Controllers)
            {
                for (int i = 0; i < world.controllerList.Count; i++)
                {
                    Controller controller = world.controllerList[i];

                    var buoyancy = controller as BuoyancyController;
                    if (buoyancy != null)
                    {
                        AABB container = buoyancy.container;
                        drawAABB(ref container, Color.LightBlue);
                    }
                }
            }

            if ((flags & DebugViewFlags.DebugPanel) == DebugViewFlags.DebugPanel)
            {
                drawDebugPanel();
            }
        }
Beispiel #3
0
        /// <summary>
        /// Call this to draw shapes and other debug draw data.
        /// </summary>
        void DrawDebugData()
        {
            if ((Flags & DebugViewFlags.Shape) == DebugViewFlags.Shape)
            {
                foreach (Body b in world.BodyList)
                {
                    FarseerPhysics.Common.Transform xf;
                    b.GetTransform(out xf);
                    foreach (Fixture f in b.FixtureList)
                    {
                        if (b.Enabled == false)
                        {
                            DrawShape(f, xf, InactiveShapeColor);
                        }
                        else if (b.BodyType == BodyType.Static)
                        {
                            DrawShape(f, xf, StaticShapeColor);
                        }
                        else if (b.BodyType == BodyType.Kinematic)
                        {
                            DrawShape(f, xf, KinematicShapeColor);
                        }
                        else if (b.IsAwake == false)
                        {
                            DrawShape(f, xf, SleepingShapeColor);
                        }
                        else
                        {
                            DrawShape(f, xf, DefaultShapeColor);
                        }
                    }
                }
            }

            if ((Flags & DebugViewFlags.ContactPoints) == DebugViewFlags.ContactPoints)
            {
                const float axisScale = 0.3f;

                for (int i = 0; i < _pointCount; ++i)
                {
                    ContactPoint point = _points[i];

                    if (point.State == PointState.Add)
                    {
                        DrawPoint(point.Position.ToXna(), 0.1f, new Color(0.3f, 0.95f, 0.3f));
                    }
                    else if (point.State == PointState.Persist)
                    {
                        DrawPoint(point.Position.ToXna(), 0.1f, new Color(0.3f, 0.3f, 0.95f));
                    }

                    if ((Flags & DebugViewFlags.ContactNormals) == DebugViewFlags.ContactNormals)
                    {
                        System.Numerics.Vector2 p1 = point.Position;
                        System.Numerics.Vector2 p2 = p1 + axisScale * point.Normal;
                        DrawSegment(p1, p2, new Color(0.4f, 0.9f, 0.4f));
                    }
                }

                _pointCount = 0;
            }

            if ((Flags & DebugViewFlags.PolygonPoints) == DebugViewFlags.PolygonPoints)
            {
                foreach (Body body in world.BodyList)
                {
                    foreach (Fixture f in body.FixtureList)
                    {
                        var polygon = f.Shape as PolygonShape;
                        if (polygon != null)
                        {
                            FarseerPhysics.Common.Transform xf;
                            body.GetTransform(out xf);

                            for (int i = 0; i < polygon.Vertices.Count; i++)
                            {
                                System.Numerics.Vector2 tmp = MathUtils.Mul(ref xf, polygon.Vertices[i]);
                                DrawPoint(tmp.ToXna(), 0.1f, Color.Red);
                            }
                        }
                    }
                }
            }

            if ((Flags & DebugViewFlags.Joint) == DebugViewFlags.Joint)
            {
                foreach (var j in world.JointList)
                {
                    FSDebugView.DrawJoint(this, j);
                }
            }

            if ((Flags & DebugViewFlags.AABB) == DebugViewFlags.AABB)
            {
                var color = new Color(0.9f, 0.3f, 0.9f);
                var bp    = world.ContactManager.BroadPhase;

                foreach (var body in world.BodyList)
                {
                    if (body.Enabled == false)
                    {
                        continue;
                    }

                    foreach (var f in body.FixtureList)
                    {
                        for (var t = 0; t < f.ProxyCount; ++t)
                        {
                            var  proxy = f.Proxies[t];
                            AABB aabb;
                            bp.GetFatAABB(proxy.ProxyId, out aabb);

                            DrawAABB(ref aabb, color);
                        }
                    }
                }
            }

            if ((Flags & DebugViewFlags.CenterOfMass) == DebugViewFlags.CenterOfMass)
            {
                foreach (Body b in world.BodyList)
                {
                    FarseerPhysics.Common.Transform xf;
                    b.GetTransform(out xf);
                    xf.P = b.WorldCenter;
                    DrawTransform(ref xf);
                }
            }

            if ((Flags & DebugViewFlags.Controllers) == DebugViewFlags.Controllers)
            {
                for (int i = 0; i < world.ControllerList.Count; i++)
                {
                    Controller controller = world.ControllerList[i];

                    var buoyancy = controller as BuoyancyController;
                    if (buoyancy != null)
                    {
                        AABB container = buoyancy.Container;
                        DrawAABB(ref container, Color.LightBlue);
                    }
                }
            }

            if ((Flags & DebugViewFlags.DebugPanel) == DebugViewFlags.DebugPanel)
            {
                DrawDebugPanel();
            }
        }
Beispiel #4
0
		static void drawJoint( FSDebugView instance, Joint joint )
		{
			if( !joint.enabled )
				return;

			var b1 = joint.bodyA;
			var b2 = joint.bodyB;
			FarseerPhysics.Common.Transform xf1;
			b1.getTransform( out xf1 );

			var x2 = Vector2.Zero;

			if( b2 != null || !joint.isFixedType() )
			{
				FarseerPhysics.Common.Transform xf2;
				b2.getTransform( out xf2 );
				x2 = xf2.p;
			}

			var p1 = joint.worldAnchorA;
			var p2 = joint.worldAnchorB;
			var x1 = xf1.p;

			var color = new Color( 0.5f, 0.8f, 0.8f );

			switch( joint.jointType )
			{
				case JointType.Distance:
				{
					instance.drawSegment( p1, p2, color );
					break;
				}
				case JointType.Pulley:
				{
					var pulley = (PulleyJoint)joint;
					var s1 = b1.getWorldPoint( pulley.localAnchorA );
					var s2 = b2.getWorldPoint( pulley.localAnchorB );
					instance.drawSegment( p1, p2, color );
					instance.drawSegment( p1, s1, color );
					instance.drawSegment( p2, s2, color );
					break;
				}
				case JointType.FixedMouse:
				{
					instance.drawPoint( p1, 0.2f, new Color( 0.0f, 1.0f, 0.0f ) );
					instance.drawSegment( p1, p2, new Color( 0.8f, 0.8f, 0.8f ) );
					break;
				}
				case JointType.Revolute:
				{
					instance.drawSegment( x1, p1, color );
					instance.drawSegment( p1, p2, color );
					instance.drawSegment( x2, p2, color );

					instance.drawSolidCircle( p2, 0.1f, Vector2.Zero, Color.Red );
					instance.drawSolidCircle( p1, 0.1f, Vector2.Zero, Color.Blue );
					break;
				}
				case JointType.Gear:
				{
					instance.drawSegment( x1, x2, color );
					break;
				}
				default:
				{
					instance.drawSegment( x1, p1, color );
					instance.drawSegment( p1, p2, color );
					instance.drawSegment( x2, p2, color );
					break;
				}
			}
		}