当前位置: 首页>>代码示例>>C#>>正文


C# Polygon.Project方法代码示例

本文整理汇总了C#中Polygon.Project方法的典型用法代码示例。如果您正苦于以下问题:C# Polygon.Project方法的具体用法?C# Polygon.Project怎么用?C# Polygon.Project使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Polygon的用法示例。


在下文中一共展示了Polygon.Project方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CirclePolygon

        public static CollisionData CirclePolygon(Circle a, Polygon b)
        {
            Matrix rotation = Matrix.CreateRotationZ(b.Entity.Orientation);
            Matrix inverseRotation = Matrix.CreateTranslation(new Vector3(-b.Entity.Position, 0)) * Matrix.CreateRotationZ(-b.Entity.Orientation) * Matrix.CreateTranslation(new Vector3(b.Entity.Position, 0));
            Vector2 circlePosition = Vector2.Transform(a.Entity.Position, inverseRotation);

            Vector2 delta = b.Entity.Position - circlePosition;

            Polygon.Projection proj;
            CollisionData result = new CollisionData(float.MaxValue);

            float inter1 = 0, inter2 = 0;
            for (int i = 0; i < b.normals.Length; i++)
            {
                proj = a.Project(b.normals[i], -delta);
                inter1 = b.projections[i].Max - proj.Min;
                inter2 = -(b.projections[i].Min - proj.Max);

            #if DEBUG && SATDEBUG
                Vector2 p = b.Entity.Position;
                Integrator.line(p + Vector2.TransformNormal(b.normals[i], rotation) * proj.Min, p + Vector2.TransformNormal(b.normals[i], rotation) * proj.Max, Color.LimeGreen);
            #endif

                if (inter1 < 0 || inter2 < 0)
                    return CollisionData.Empty;

                if (inter1 < inter2 && inter1 < result.Interpenetration)
                {
                    result.Interpenetration = inter1;
                    result.Normal = -Vector2.TransformNormal(b.normals[i], rotation);
                }
                else if (inter2 < result.Interpenetration)
                {
                    result.Interpenetration = inter2;
                    result.Normal = Vector2.TransformNormal(b.normals[i], rotation);
                }
            }

            Vector2 closest = Vector2.UnitX;
            float maxDist = float.MaxValue;
            for (int i = 0; i < b.Vertices.Length; i++)
            {
                Vector2 v = b.Vertices[i] + b.Entity.Position;
                Vector2 d = v - circlePosition;

                if (d.LengthSquared() < maxDist)
                {
                    closest = d;
                    maxDist = d.LengthSquared();
                }
            }
            Vector2 normal = closest.Normalized();

            proj = b.Project(normal, delta);
            Polygon.Projection self = a.Project(normal, Vector2.Zero);
            inter1 = self.Max - proj.Min;
            inter2 = -(self.Min - proj.Max);

            #if DEBUG && SATDEBUG
            Vector2 p2 = a.Entity.Position;
            Integrator.line(p2 + Vector2.TransformNormal(normal, rotation) * proj.Min, p2 + Vector2.TransformNormal(normal, rotation) * proj.Max, Color.LimeGreen);
            #endif

            if (inter1 < 0 || inter2 < 0)
                return CollisionData.Empty;

            if (inter1 < inter2 && inter1 < result.Interpenetration)
            {
                result.Interpenetration = inter1;
                result.Normal = Vector2.TransformNormal(normal, rotation);
            }
            else if (inter2 < result.Interpenetration)
            {
                result.Interpenetration = inter2;
                result.Normal = -Vector2.TransformNormal(normal, rotation);
            }

            #if DEBUG && SATDEBUG
            Vector2 pos = a.Entity.Position;
            Integrator.line(pos, pos + result.Normal * result.Interpenetration, Color.White);
            #endif

            return result;
        }
开发者ID:HuntiXz,项目名称:phantom,代码行数:84,代码来源:CollisionChecks.cs

示例2: TestPointInPoly

        public static bool TestPointInPoly(Polygon a, Vector2 pt)
        {
            var norms = a.GetNormals();

            foreach (Vector2 ax in norms)
            {
                var p = a.Project(ax);
                var pt_p = Vector2.Dot(pt, ax);

                if (pt_p > p.Max || pt_p < p.Min) //no collision if even 1 separating axis found
                {
                    return false;
                }
            }

            return true; //must check all SAs before confirming a collision.
        }
开发者ID:tylermenezes,项目名称:Hunt-the-Wumpus-2010,代码行数:17,代码来源:Collision.cs

示例3: PolygonPolygon

        public static CollisionData PolygonPolygon(Polygon a, Polygon b)
        {
            Vector2 delta = b.Entity.Position - a.Entity.Position;
            float inter1 = 0, inter2 = 0;

            CollisionData result = new CollisionData(float.MaxValue);

            Polygon lookingAt = a;

            delta = Vector2.Transform(delta, Matrix.CreateRotationZ(-b.Entity.Orientation));
            Matrix rot = Matrix.CreateRotationZ(a.Entity.Orientation - b.Entity.Orientation);

            for (int i = 0; i < a.normals.Length; i++)
            {
                Polygon.Projection proj = b.Project(Vector2.TransformNormal(a.normals[i], rot), delta);
                inter1 = a.projections[i].Max - proj.Min;
                inter2 = -(a.projections[i].Min - proj.Max);

            #if DEBUG && SATDEBUG
                Vector2 p = a.Entity.Position;
                Matrix r = Matrix.CreateRotationZ(a.Entity.Orientation);
                Integrator.line(p + Vector2.TransformNormal(a.normals[i], r) * proj.Min, p + Vector2.TransformNormal(a.normals[i], r) * proj.Max, Color.HotPink);
            #endif

                if (inter1 < 0 || inter2 < 0)
                    return CollisionData.Empty;

                if (inter1 < inter2 && inter1 < result.Interpenetration)
                {
                    result.Interpenetration = inter1;
                    result.Normal = Vector2.TransformNormal(a.normals[i], Matrix.CreateRotationZ(a.Entity.Orientation));
                    lookingAt = b;
                }
                else if( inter2 < result.Interpenetration)
                {
                    result.Interpenetration = inter2;
                    result.Normal = -Vector2.TransformNormal(a.normals[i], Matrix.CreateRotationZ(a.Entity.Orientation));
                    lookingAt = b;
                }

            }

            delta = b.Entity.Position - a.Entity.Position;
            delta = Vector2.Transform(delta, Matrix.CreateRotationZ(-a.Entity.Orientation));
            rot = Matrix.CreateRotationZ(b.Entity.Orientation - a.Entity.Orientation);

            for (int i = 0; i < b.normals.Length; i++)
            {
                Polygon.Projection proj = a.Project(Vector2.TransformNormal(b.normals[i], rot), -delta);
                inter1 = b.projections[i].Max - proj.Min;
                inter2 = -(b.projections[i].Min - proj.Max);

            #if DEBUG && SATDEBUG
                Vector2 p = b.Entity.Position;
                Matrix r = Matrix.CreateRotationZ(b.Entity.Orientation);
                Integrator.line(p + Vector2.TransformNormal(b.normals[i], r) * proj.Min, p + Vector2.TransformNormal(b.normals[i], r) * proj.Max, Color.LimeGreen);
            #endif

                if (inter1 < 0 || inter2 < 0)
                    return CollisionData.Empty;

                if (inter1 < inter2 && inter1 < result.Interpenetration)
                {
                    result.Interpenetration = inter1;
                    result.Normal = -Vector2.TransformNormal(b.normals[i], Matrix.CreateRotationZ(b.Entity.Orientation));
                    lookingAt = a;
                }
                else if (inter2 < result.Interpenetration)
                {
                    result.Interpenetration = inter2;
                    result.Normal = Vector2.TransformNormal(b.normals[i], Matrix.CreateRotationZ(b.Entity.Orientation));
                    lookingAt = a;
                }
            }

            int dir = lookingAt == a ? -1 : 1;
            float bestDot = 0;
            Vector2 guess = Vector2.Zero;
            Vector2[] verts = lookingAt.RotatedVertices(lookingAt.Entity.Orientation);
            for (int i = 0; i < verts.Length; i++)
            {
                Vector2 v = verts[i];
                float d = Vector2.Dot(v, result.Normal) * dir;
                if (d < bestDot - SATPositioningAngle)
                {
                    bestDot = d;
                    guess = v;
                }
                else if (d < bestDot + SATPositioningAngle)
                {
                    bestDot = d;
                    guess += v;
                    guess *= .5f;
                }
            }
            result.Position = lookingAt.Entity.Position + guess;

            #if DEBUG && SATDEBUG
            Vector2 pos = result.Position;
            Integrator.line(pos, pos + result.Normal * result.Interpenetration * 10, Color.White);
//.........这里部分代码省略.........
开发者ID:HuntiXz,项目名称:phantom,代码行数:101,代码来源:CollisionChecks.cs

示例4: TestCollisionPolyFull

        //axis is the normal of collision, FROM a TO b!. DISP IS ALWYAS NEGATIVE! (for collisions)
        public static bool TestCollisionPolyFull(Polygon a, Polygon b, out Vector2 axis, out float disp)
        {
            axis = new Vector2();
            disp = float.MinValue;

            //
            //Collision test
            //

            //var norms = a.GetNormals().ToList(); //used to be a union.
            //norms.AddRange(b.GetNormals().ToList());
            var norms = a.GetNormals().Union(b.GetNormals());

            foreach (Vector2 ax in norms)
            {
                float d = Projection.IntervalDist(a.Project(ax), b.Project(ax));

                if (d > 0) //no collision if even 1 separating axis found
                {
                    disp = 0;
                    return false;
                }
                else if (d > disp) //return disp w/ lowset overlap (ie: highest separation)
                {
                    disp = d;
                    axis = ax;
                }

            }

            if (Vector2.Dot(b.Position - a.Position, axis) < 0) //make sure the normal always points from a->b. (should this use .GetCentroid() instead of position?)
                axis = -axis;

            return true; //must check all SAs before confirming a collision. return SA w/ lowest overlap.
        }
开发者ID:tylermenezes,项目名称:Hunt-the-Wumpus-2010,代码行数:36,代码来源:Collision.cs


注:本文中的Polygon.Project方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。