示例#1
0
        public void FillPolygon(GraphicsDevice graphicsDevice, ITextureUV uvPoints, Vector2 translation, Vector2 scale)
        {
            PolygonFillEffect fillEffect = new PolygonFillEffect(graphicsDevice, uvPoints.GetTexture());

            //Split into triangles
            Triangle[] triangles = GetTriangles();

            //Scale and translate
            for (int i = 0; i < triangles.Length; i++)
            {
                triangles[i].Translate(translation);
                triangles[i].Scale(scale);
            }

            //Get UV map points and ensure validity
            Vector2[] uvMapPoints = uvPoints.GetUVPoints();
            if (uvMapPoints.Length != triangles.Length * 3)
            {
                throw new Exception("Invalid UV points. They are not of the proper length.");
            }

            //Apply triangles
            VertexPositionTexture[] vertexTexturePositions = new VertexPositionTexture[triangles.Length * 3];
            int[] indexData = new int[vertexTexturePositions.Length];
            for (int i = 0; i < vertexTexturePositions.Length; i++)
            {
                vertexTexturePositions[i].Position          = new Vector3(triangles[i / 3].Vertices[i % 3], 0);
                vertexTexturePositions[i].TextureCoordinate = uvMapPoints[i];
                indexData[i] = i;
            }

            foreach (EffectPass pass in fillEffect.CurrentTechnique.Passes)
            {
                pass.Apply();
                graphicsDevice.DrawUserIndexedPrimitives <VertexPositionTexture>(PrimitiveType.TriangleList, vertexTexturePositions, 0, vertexTexturePositions.Length, indexData, 0, triangles.Length);
            }

            fillEffect.Dispose();
        }
示例#2
0
        //public static bool AreColliding(RigidBody rigidBody1, RigidBody rigidBody2, out IntersectionEvent intersectionEvent)
        //{
        //    IntersectionData intersectionData;
        //    if (rigidBody1.CollisionPolygon.IntersectingPolygon(rigidBody2.CollisionPolygon, out intersectionData) && intersectionData.ValidOverlap)
        //    {
        //        RigidBody sender, reciever;
        //        if (intersectionData.RecipientIsFunctionCaller)
        //        {
        //            sender = rigidBody2;
        //            reciever = rigidBody1;
        //        }
        //        else
        //        {
        //            sender = rigidBody1;
        //            reciever = rigidBody2;
        //        }

        //        intersectionEvent = new IntersectionEvent(sender, reciever, intersectionData);
        //        return true;
        //    }
        //    else
        //    {
        //        intersectionEvent = null;
        //        return false;
        //    }
        //}

        public static RigidBody[] ApplySplit(RigidBody originalBody, Polygon[] newSplits, ITextureUV parentUV, out StandardUVMap[] childrenUV)
        {
            Material originalMaterial = originalBody.material;

            RigidBody[] newBodies = new RigidBody[newSplits.Length];

            if (parentUV != null)
            {
                childrenUV = new StandardUVMap[newBodies.Length];
                for (int i = 0; i < childrenUV.Length; i++)
                {
                    childrenUV[i] = new StandardUVMap(parentUV.GetTexture(), newSplits[i], originalBody.CollisionPolygon, parentUV);
                }
            }
            else
            {
                childrenUV = null;
            }

            for (int i = 0; i < newBodies.Length; i++)
            {
                newBodies[i] = new RigidBody(newSplits[i], originalMaterial);

                //Apply proportional velocity
                newBodies[i].AddTranslationalVelocity(originalBody.TranslationalVelocity, isMomentum: false);

                //Apply tangential velocity
                newBodies[i].AddTranslationalVelocity(originalBody.GetLocalVelocity(newBodies[i].CollisionPolygon.CenterPoint) - originalBody.TranslationalVelocity, isMomentum: false);
            }

            return(newBodies);
        }
示例#3
0
 public DrawBodyWrapper(ITextureUV textureUV, Polygon collisionPolygon, float Bounce, float Mass, float StaticFriction, float DynamicFriction, int collisionLevel = 0) : base(collisionPolygon, Bounce, Mass, StaticFriction, DynamicFriction, collisionLevel)
 {
     this.TextureUV = textureUV;
 }
示例#4
0
        public StandardUVMap(Texture2D texture, Polygon polygon, Polygon parentPolygon, ITextureUV parentUVMap)
        {
            this.texture = texture;

            //Rotate relative to parent
            Polygon childCopy = new Polygon(polygon.Vertices);

            childCopy.Rotate(-parentPolygon.TotalRotation % (float)(2 * Math.PI), parentPolygon.CenterPoint);

            //Get triangle vertices
            Triangle[] triangles       = childCopy.GetTriangles();
            Vector2[]  polygonVertices = new Vector2[triangles.Length * 3];
            for (int i = 0; i < polygonVertices.Length; i++)
            {
                polygonVertices[i] = triangles[i / 3].Vertices[i % 3];
            }

            RectangleF parentBoundary = parentPolygon.BoundaryRectangle;
            float      polygonMajor   = Math.Max(parentBoundary.Width, parentBoundary.Height);

            //Make vertices relative to parent
            for (int i = 0; i < polygonVertices.Length; i++)
            {
                polygonVertices[i] -= parentBoundary.Location;
                polygonVertices[i] /= polygonMajor;
            }

            //Map to minor axis of source
            RectangleF uvBounds        = parentUVMap.GetUVBounds();
            RectangleF sourceRectangle = new RectangleF(uvBounds.X * texture.Width, uvBounds.Y * texture.Height, uvBounds.Width * texture.Width, uvBounds.Height * texture.Height);
            float      sourceMinor     = Math.Min(sourceRectangle.Width, sourceRectangle.Height);

            for (int i = 0; i < polygonVertices.Length; i++)
            {
                polygonVertices[i] *= sourceMinor;
                polygonVertices[i] += new Vector2(sourceRectangle.X, sourceRectangle.Y);
            }

            //Make relative to axes
            for (int i = 0; i < polygonVertices.Length; i++)
            {
                polygonVertices[i].X /= texture.Width;
                polygonVertices[i].Y /= texture.Height;
            }

            uvPoints = polygonVertices;

            throw new NotImplementedException();
        }
示例#5
0
 public DrawBodyWrapper(ITextureUV textureUV, Polygon collisionPolygon, Material material, int collisionLevel = 0) : base(collisionPolygon, material, collisionLevel)
 {
     this.TextureUV = textureUV;
 }