Esempio n. 1
0
        public void AddVertex(System.Numerics.Vector2 vertex, Color color, PrimitiveType primitiveType)
        {
            Insist.IsTrue(_hasBegun, "Invalid state. Begin must be called before AddVertex can be called.");
            Insist.IsFalse(primitiveType == PrimitiveType.LineStrip || primitiveType == PrimitiveType.TriangleStrip,
                           "The specified primitiveType is not supported by PrimitiveBatch");

            if (primitiveType == PrimitiveType.TriangleList)
            {
                if (_triangleVertsCount >= _triangleVertices.Length)
                {
                    FlushTriangles();
                }

                _triangleVertices[_triangleVertsCount].Position = new Vector3(vertex.ToXna(), 0);
                _triangleVertices[_triangleVertsCount].Color    = color;
                _triangleVertsCount++;
            }

            if (primitiveType == PrimitiveType.LineList)
            {
                if (_lineVertsCount >= _lineVertices.Length)
                {
                    FlushLines();
                }

                _lineVertices[_lineVertsCount].Position = new Vector3(vertex.ToXna(), 0);
                _lineVertices[_lineVertsCount].Color    = color;
                _lineVertsCount++;
            }
        }
Esempio n. 2
0
 void AddVert(int index, System.Numerics.Vector2 position, System.Numerics.Vector2 texCoord, Color col)
 {
     _vertices.EnsureCapacity();
     _vertices.Buffer[index].Position          = position.ToVector3();
     _vertices.Buffer[index].TextureCoordinate = texCoord.ToXna();
     _vertices.Buffer[index].Color             = col;
     _vertices.Length++;
 }
Esempio n. 3
0
        public static void DrawPixel(this SpriteBatch spriteBatch, System.Numerics.Vector2 position, Color color, int size = 1)
        {
            var sourceRect = Graphics.Instance.PixelTexture.SourceRect;

            if (size != 1)
            {
                position.X        -= size * 0.5f;
                position.Y        -= size * 0.5f;
                sourceRect.Width  *= size;
                sourceRect.Height *= size;
            }

            spriteBatch.Draw(Graphics.Instance.PixelTexture, position.ToXna(), sourceRect, color);
        }
Esempio n. 4
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();
            }
        }
Esempio n. 5
0
 /// <summary>
 /// directly sets the light direction
 /// </summary>
 /// <param name="lightDirection">Light direction.</param>
 public void SetSpotLightDirection(System.Numerics.Vector2 lightDirection)
 {
     _lightDirectionParam.SetValue(lightDirection.ToXna());
 }
Esempio n. 6
0
 public static void DrawLineAngle(this SpriteBatch spriteBatch, System.Numerics.Vector2 start, float angle, float length,
                                  Color color, float thickness)
 {
     spriteBatch.Draw(Graphics.Instance.PixelTexture, start.ToXna(), Graphics.Instance.PixelTexture.SourceRect, color,
                      angle, new Vector2(0f, 0.5f), new Vector2(length, thickness), SpriteEffects.None, 0);
 }
Esempio n. 7
0
        public override void Render(Batcher batcher, Camera camera)
        {
            // TODO: make culling smarter and only render the lines that are actually on the screen rather than all or nothing
            var width  = _points.GetLength(0);
            var height = _points.GetLength(1);

            for (var y = 1; y < height; y++)
            {
                for (var x = 1; x < width; x++)
                {
                    var left = new System.Numerics.Vector2();
                    var up   = new System.Numerics.Vector2();
                    var p    = ProjectToVector2(_points[x, y].Position);

                    if (x > 1)
                    {
                        float thickness;
                        Color gridColor;
                        if (y % GridMajorPeriodY == 1)
                        {
                            thickness = GridMajorThickness;
                            gridColor = GridMajorColor;
                        }
                        else
                        {
                            thickness = GridMinorThickness;
                            gridColor = GridMinorColor;
                        }


                        // use Catmull-Rom interpolation to help smooth bends in the grid
                        left = ProjectToVector2(_points[x - 1, y].Position);
                        var clampedX = Math.Min(x + 1, width - 1);
                        var mid      = Vector2.CatmullRom(ProjectToVector2(_points[x - 2, y].Position).ToXna(), left.ToXna(), p.ToXna(),
                                                          ProjectToVector2(_points[clampedX, y].Position).ToXna(), 0.5f).ToSimd();

                        // If the grid is very straight here, draw a single straight line. Otherwise, draw lines to our new interpolated midpoint
                        if (System.Numerics.Vector2.DistanceSquared(mid, (left + p) / 2) > 1)
                        {
                            DrawLine(batcher, left, mid, gridColor, thickness);
                            DrawLine(batcher, mid, p, gridColor, thickness);
                        }
                        else
                        {
                            DrawLine(batcher, left, p, gridColor, thickness);
                        }
                    }

                    if (y > 1)
                    {
                        float thickness;
                        Color gridColor;
                        if (x % GridMajorPeriodX == 1)
                        {
                            thickness = GridMajorThickness;
                            gridColor = GridMajorColor;
                        }
                        else
                        {
                            thickness = GridMinorThickness;
                            gridColor = GridMinorColor;
                        }

                        up = ProjectToVector2(_points[x, y - 1].Position);
                        var clampedY = Math.Min(y + 1, height - 1);
                        var mid      = Vector2.CatmullRom(ProjectToVector2(_points[x, y - 2].Position).ToXna(), up.ToXna(), p.ToXna(),
                                                          ProjectToVector2(_points[x, clampedY].Position).ToXna(), 0.5f).ToSimd();      // ToDo: SIMD

                        if (System.Numerics.Vector2.DistanceSquared(mid, (up + p) / 2) > 1)
                        {
                            DrawLine(batcher, up, mid, gridColor, thickness);
                            DrawLine(batcher, mid, p, gridColor, thickness);
                        }
                        else
                        {
                            DrawLine(batcher, up, p, gridColor, thickness);
                        }
                    }

                    // Add interpolated lines halfway between our point masses. This makes the grid look
                    // denser without the cost of simulating more springs and point masses.
                    if (x > 1 && y > 1)
                    {
                        var upLeft = ProjectToVector2(_points[x - 1, y - 1].Position);
                        DrawLine(batcher, 0.5f * (upLeft + up), 0.5f * (left + p), GridMinorColor,
                                 GridMinorThickness);                        // vertical line
                        DrawLine(batcher, 0.5f * (upLeft + left), 0.5f * (up + p), GridMinorColor,
                                 GridMinorThickness);                        // horizontal line
                    }
                }
            }
        }