Example #1
0
        public COLLISIONWORKEngine(string Title, Vector2d ScreenDimensions, ShapeHandler shapeHandler)
        {
            this.ScreenDimensions = ScreenDimensions;
            this.Title            = Title;
            this.shapeHandler     = shapeHandler;
            this.levelHandler     = new LevelHandler(this.shapeHandler);
            this.Sound            = new Sound();

            Shape2D playerShape = new Shape2D(new Vector2d(200, 200), new Vector2d(50, 50), Color.Purple, TypeSpec.Player);

            player = new Player(playerShape, 200, Sound);

            GameWindow                 = new Window();
            GameWindow.Size            = new Size((int)ScreenDimensions.X, (int)ScreenDimensions.Y);
            GameWindow.FormBorderStyle = FormBorderStyle.FixedToolWindow;

            GameWindow.Text   = this.Title;
            GameWindow.Paint += Renderer;


            GameLoopThread = new Thread(GameLoop);
            GameLoopThread.Start();

            Application.Run(GameWindow);
        }
Example #2
0
 public FlatShape3D(Shape2D flatShape, bool isOrientable = true) : base(ShapeTypes3D.Flat, isOrientable)
 {
     if (flatShape != null)
     {
         FlatShape = flatShape;
     }
 }
Example #3
0
 public ExclusiveOrShape(Shape2D first, Shape2D second)
 {
     First   = first;
     Second  = second;
     Union   = new UnionShape(first, second);
     Overlap = new IntersectionShape(first, second);
 }
    private Vector3[] CreateVertices(Shape2D shape)
    {
        // Creates the vertices
        Vector3[] meshVertices = new Vector3[(resolution * (_controlPoints.Length - 1) + 1) * shape.points.Length];

        // For each control point...
        for (int controlPointIndex = 0; controlPointIndex < _controlPoints.Length; controlPointIndex++)
        {
            // For each resolution pass...
            for (int resolutionPass = 0; resolutionPass < resolution; resolutionPass++)
            {
                // Calculates the interpolated values
                float      lerpFactor           = controlPointIndex + (float)resolutionPass / resolution;
                Vector3    interpolatedPosition = InterpolatePosition(lerpFactor);
                Quaternion interpolatedRotation = InterpolateRotation(lerpFactor);
                Vector3    interpolatedScale    = InterpolateScale(lerpFactor);

                // Caches some values
                int meshVertexBaseIndex = (controlPointIndex * resolution + resolutionPass) * shape.points.Length;

                // Creates a vertex for each point using the interpolated information
                Vector3[] newVertices = VerticesFromShape(shape, interpolatedPosition, interpolatedRotation, interpolatedScale);
                Array.Copy(newVertices, 0, meshVertices, meshVertexBaseIndex, newVertices.Length);

                // The last control point only has one resolution pass!
                if (controlPointIndex == _controlPoints.Length - 1)
                {
                    break;
                }
            }
        }

        return(meshVertices);
    }
    void OnEnable()
    {
        if (editorTarget.type != 0)
        {
            type = Type.SHAPE;
        }
        else
        {
            type = Type.PLANE;
        }

        if (string.IsNullOrEmpty(editorTarget.shapeData) || editorTarget.shapeData == "null")             // No data was found so we create a new empty shape object
        {
            shapeOutline = Shape2D.triangle;
        }
        else
        {
            JSONObject shapeObj = new JSONObject(editorTarget.shapeData);

            shapeOutline = new Shape2D();
            foreach (JSONObject vecObj in shapeObj.list)
            {
                Vector2 point = new Vector2((float)vecObj.GetField("x").n, (float)vecObj.GetField("y").n);
                shapeOutline.AddPoint(point);
            }
        }
    }
Example #6
0
 [AOP_Test("chen")]         //AOP属性  用于AOP处理
 public void CallHello(string message, Shape2D shape)
 {
     using (new AOP(this, message, shape))             //对AOP的属性处理的调用,xxx,yyy,kkk为该函数的参数
     {
         LogCat.LogError(message);
     }
 }
        private void Spawn(Color color)
        {
            int X = r.Next(200, 1050);
            int Y = r.Next(-250, -50);

            Shape2D falling = new Shape2D(new Vector2d(X, Y), new Vector2d(20, 20), color, TypeSpec.Falling);

            shapeHandler.addShape(falling);
        }
Example #8
0
 private static void MakeShapesFromSectors(VectorImage image, Dictionary <int, List <Line2D> > sectors)
 {
     foreach (KeyValuePair <int, List <Line2D> > lines in sectors)
     {
         Shape2D shape = Shape2DFunctions.CreateShapesFromLines(lines.Value);
         PrimitiveRenderData.Get(shape).Text = "Sector: " + lines.Key;
         image.Add(shape);
     }
 }
 private Vector3[] VerticesFromShape(Shape2D shape, Vector3 position, Quaternion rotation, Vector3 scale)
 {
     Vector3[] vertices = new Vector3[shape.points.Length];
     for (int shapePointIndex = 0; shapePointIndex < shape.points.Length; shapePointIndex++)
     {
         vertices[shapePointIndex] = Shape2DExtrudeControlPoint.TransformPoint(shape.points[shapePointIndex], position, rotation, scale);
     }
     return(vertices);
 }
Example #10
0
        public void IntersectingShapeWithLineSegment_NoIntersection_ExpectedEmptyShape()
        {
            LineSegment2D lineSegment1 = new LineSegment2D(new Point2D(0, 0), new Point2D(10, 10));
            LineSegment2D lineSegment2 = new LineSegment2D(new Point2D(40, 40), new Point2D(30, 50));
            Shape2D       expectedIntersectingShape = new EmptyShape2D();

            Shape2D actualIntersectingShape = lineSegment1.IntersectingShape(lineSegment2);

            Assert.That(expectedIntersectingShape.Equals(actualIntersectingShape));
        }
Example #11
0
        public void IntersectingShapeWithLineSegment_IdenticalSegments_ExpectedLineSegment()
        {
            LineSegment2D lineSegment1 = new LineSegment2D(new Point2D(0, 0), new Point2D(10, 10));
            LineSegment2D lineSegment2 = new LineSegment2D(new Point2D(0, 0), new Point2D(10, 10));
            Shape2D       expectedIntersectingShape = new LineSegment2D(new Point2D(0, 0), new Point2D(10, 10));

            Shape2D actualIntersectingShape = lineSegment1.IntersectingShape(lineSegment2);

            Assert.That(expectedIntersectingShape.Equals(actualIntersectingShape));
        }
Example #12
0
        public void IntersectingShapeWithRay_TouchingPoint_ExpectedPoint()
        {
            LineSegment2D lineSegment = new LineSegment2D(new Point2D(5, 0), new Point2D(5, 10));
            Ray2D         ray         = new Ray2D(new Point2D(-5, 0), new Point2D(10, 0));
            Shape2D       expectedIntersectingShape = new Point2D(5, 0);

            Shape2D actualIntersectingShape = lineSegment.IntersectingShape(ray);

            Assert.That(expectedIntersectingShape.Equals(actualIntersectingShape));
        }
Example #13
0
        public void IntersectingShapeWithRay_LineSegmentPartiallyInsideRay_ExpectedShortLineSegment(double p1x, double p1y, double p2x, double p2y)
        {
            LineSegment2D lineSegment = new LineSegment2D(new Point2D(p1x, p1y), new Point2D(p2x, p2y));
            Ray2D         ray         = new Ray2D(new Point2D(0, 0), new Point2D(10, 10));
            Shape2D       expectedIntersectingShape = new LineSegment2D(new Point2D(0, 0), new Point2D(5, 5));

            Shape2D actualIntersectingShape = lineSegment.IntersectingShape(ray);

            Assert.That(expectedIntersectingShape.Equals(actualIntersectingShape));
        }
Example #14
0
        public void IntersectingShapeWithRay_LineSegmentFullyInsideRay_ExpectedLineSegment()
        {
            LineSegment2D lineSegment = new LineSegment2D(new Point2D(15, 15), new Point2D(20, 20));
            Ray2D         ray         = new Ray2D(new Point2D(0, 0), new Point2D(10, 10));
            Shape2D       expectedIntersectingShape = new LineSegment2D(new Point2D(15, 15), new Point2D(20, 20));

            Shape2D actualIntersectingShape = lineSegment.IntersectingShape(ray);

            Assert.That(expectedIntersectingShape.Equals(actualIntersectingShape));
        }
Example #15
0
        public void IntersectingShapeWithRay_NoIntersection_ExpectedEmptyShape()
        {
            LineSegment2D lineSegment = new LineSegment2D(new Point2D(0, 0), new Point2D(10, 0));
            Ray2D         ray         = new Ray2D(new Point2D(0, 10), new Point2D(10, 0));
            Shape2D       expectedIntersectingShape = new EmptyShape2D();

            Shape2D actualIntersectingShape = lineSegment.IntersectingShape(ray);

            Assert.That(expectedIntersectingShape.Equals(actualIntersectingShape));
        }
Example #16
0
    public static Mesh MeshFromShape(Shape2D shape, float length)
    {
        // Creates the vertices
        Vector3[] meshVertices = new Vector3[2 * shape.points.Length];
        for (int i = 0; i < shape.points.Length; i++)
        {
            meshVertices[i] = new Vector3(shape.points[i].x, shape.points[i].y, -length / 2f);
            meshVertices[i + shape.points.Length] = new Vector3(shape.points[i].x, shape.points[i].y, length / 2f);
        }

        // Creates the normals
        Vector3[] meshNormals = new Vector3[2 * shape.normals.Length];
        for (int i = 0; i < shape.normals.Length; i++)
        {
            meshNormals[i] = new Vector3(shape.normals[i].x, shape.normals[i].y, 0);
            meshNormals[i + shape.normals.Length] = new Vector3(shape.normals[i].x, shape.normals[i].y, 0);
        }

        // Creates the UVs
        Vector2[] meshUVs = new Vector2[2 * shape.us.Length];
        for (int i = 0; i < shape.us.Length; i++)
        {
            meshUVs[i] = new Vector3(shape.us[i], 0);
            meshUVs[i + shape.us.Length] = new Vector3(shape.us[i], 1);
        }

        // Creates the triangles
        int[] meshTriangles = new int[3 * shape.lines.Length];
        for (int i = 0; i < shape.lines.Length; i += 2)
        {
            // Each line generates 2 triangles, then 6 vertices
            int currentTriangle = 3 * i;

            // Creates the first triangle
            meshTriangles[currentTriangle]     = shape.lines[i];
            meshTriangles[currentTriangle + 1] = shape.lines[i + 1];
            meshTriangles[currentTriangle + 2] = shape.lines[i] + shape.points.Length;

            // Creates the first triangle
            meshTriangles[currentTriangle + 3] = shape.lines[i] + shape.points.Length;
            meshTriangles[currentTriangle + 4] = shape.lines[i + 1];
            meshTriangles[currentTriangle + 5] = shape.lines[i + 1] + shape.points.Length;
        }

        // Populates the mesh
        Mesh mesh = new Mesh();

        mesh.vertices  = meshVertices;
        mesh.normals   = meshNormals;
        mesh.uv        = meshUVs;
        mesh.triangles = meshTriangles;
        mesh.RecalculateBounds();

        return(mesh);
    }
Example #17
0
    public void Extrude(Mesh mesh, Shape2D shape, OrientedPoint[] path)
    {
        int vertsInShape  = shape.verts.Length;
        int segments      = path.Length - 1;
        int edgeLoops     = path.Length;
        int vertCount     = vertsInShape * edgeLoops;
        int triCount      = (shape.lines.Length - 1) * segments * 2;
        int triIndexCount = triCount * 3;


        int[]     trianglesIndices = new int[triIndexCount];
        Vector3[] vertices         = new Vector3[vertCount];
        Vector3[] normals          = new Vector3[vertCount];
        Vector2[] uvs = new Vector2[vertCount];


        for (int i = 0; i < path.Length; i++)           //for each oriented point
        {
            int offset = i * vertsInShape;
            for (int j = 0; j < vertsInShape; j++)
            {
                int id = offset + j;
                vertices [id] = path[i].LocalToWorld(shape.verts [j]);
                normals [id]  = path [i].LocalToWorldDirection(shape.normals [j]);
                uvs [id]      = new Vector2(shape.us [j], path [i].cumulDistance);            //i/((float)edgeLoops));
            }
        }

        int ti = 0;

        for (int i = 0; i < segments; i++)
        {
            int offset = i * vertsInShape;
            for (int l = 0; l < shape.lines.Length - 1; l++)
            {
                int a = offset + shape.lines [l] + vertsInShape;
                int b = offset + shape.lines [l];
                int c = offset + shape.lines [l + 1];
                int d = offset + shape.lines [l + 1] + vertsInShape;
                trianglesIndices [ti] = a; ti++;
                trianglesIndices [ti] = c; ti++;
                trianglesIndices [ti] = b; ti++;
                trianglesIndices [ti] = c; ti++;
                trianglesIndices [ti] = a; ti++;
                trianglesIndices [ti] = d; ti++;
            }
        }


        mesh.Clear();
        mesh.vertices  = vertices;
        mesh.normals   = normals;
        mesh.uv        = uvs;
        mesh.triangles = trianglesIndices;
    }
Example #18
0
        public void IntersectingShapeWithLineSegment_PartialOverlap_ExpectedLineSegment(int exampleID)
        {
            (LineSegment2D, LineSegment2D, LineSegment2D)values = InitValues_IntersectingShapeWithLineSegment_PartialOverlap(exampleID);
            LineSegment2D lineSegment1 = values.Item1;
            LineSegment2D lineSegment2 = values.Item2;
            Shape2D       expectedIntersectingShape = values.Item3;

            Shape2D actualIntersectingShape = lineSegment1.IntersectingShape(lineSegment2);

            Assert.That(expectedIntersectingShape.Equals(actualIntersectingShape));
        }
 public bool IsColliding(Shape2D a, Shape2D b)
 {
     if (a.position.x < b.position.x + b.scale.x &&
         a.position.x + a.scale.x > b.position.x &&
         a.position.y < b.position.y + b.scale.y &&
         a.position.y + a.scale.y > b.position.y)
     {
         return(true);
     }
     return(false);
 }
Example #20
0
    public static Mesh GenerateShapeMesh(Vector2 tileSize, Shape2D shapeOutline)
    {
        List <CombineInstance> meshInstances = new List <CombineInstance>();

        Rect meshBounds = shapeOutline.GetBounds();

        int columns = Mathf.CeilToInt(meshBounds.width / tileSize.x);           // We ceil these two guys so the amount of tiles will be just enough to cover the entire shape
        int rows    = Mathf.CeilToInt(meshBounds.height / tileSize.y);

        for (int iCol = 0; iCol < columns; iCol++)
        {
            for (int iRow = 0; iRow < rows; iRow++)
            {
                Vector2 topLeft     = new Vector3(meshBounds.x + iCol * tileSize.x, meshBounds.y + (iRow + 1) * tileSize.y);
                Vector2 topRight    = new Vector3(meshBounds.x + (iCol + 1) * tileSize.x, meshBounds.y + (iRow + 1) * tileSize.y);
                Vector2 bottomLeft  = new Vector3(meshBounds.x + iCol * tileSize.x, meshBounds.y + iRow * tileSize.y);
                Vector2 bottomRight = new Vector3(meshBounds.x + (iCol + 1) * tileSize.x, meshBounds.y + iRow * tileSize.y);

                Shape2D tileShape = new Shape2D();
                tileShape.AddRange(new Vector2[] { topLeft, topRight, bottomRight, bottomLeft });

                List <Mesh> meshes = new List <Mesh>();

                if (shapeOutline.Contains(tileShape))                       // The tile is square, so no need for clipping
                {
                    meshes.AddRange(GenerateQuad(topLeft, topRight, bottomLeft, bottomRight));
                }
                else                     // The tile is somehow clipped, so we need to involve the Clipper and Poly2Tri libs
                {
                    meshes.AddRange(GenerateClippedQuad(tileShape, shapeOutline, ClipType.ctIntersection));
                }

                foreach (Mesh mesh in meshes)
                {
                    CombineInstance meshInstance = new CombineInstance();
                    meshInstance.mesh         = mesh;
                    meshInstance.subMeshIndex = 0;
                    meshInstance.transform    = Matrix4x4.identity;

                    meshInstances.Add(meshInstance);
                }
            }
        }


        Mesh shapeMesh = new Mesh();

        shapeMesh.name = "shapeMesh";
        shapeMesh.CombineMeshes(meshInstances.ToArray(), true);
        shapeMesh.Optimize();
        shapeMesh.RecalculateNormals();

        return(shapeMesh);
    }
Example #21
0
        public void SetProxy(IAopProxy target)
        {
            Shape2D shape = target as Shape2D;

            if (shape == null)
            {
                throw new ArgumentException("target is not an IShape2D");
            }

            this.shape = shape;
        }
Example #22
0
 public void removeShape(Shape2D shape)
 {
     for (int i = 0; i < shapes_.Count; ++i)
     {
         if (shapes_[i] == shape)
         {
             shapes_.Remove(shape);
             return;
         }
     }
 }
 public override void OnUpdate()
 {
     if (time > 400)
     {
         if (player != null)
         {
             player.DestroySelf();
             player = null;
         }
     }
     time++;
 }
Example #24
0
        private static string Polygon2DToText(Shape2D s)
        {
            string          result = "";
            List <Vector2D> points = new List <Vector2D>(s.GetPoints());

            for (int i = 0; i < points.Count; i++)
            {
                bool isLastPoint = i == points.Count - 1;
                result += Vector2DToText(points[i]) + (isLastPoint ? "" : ", ");
            }
            return(result);
        }
Example #25
0
 private void DrawShape2D(RenderContext context, Shape2D s)
 {
     if (context.ShowShapes)
     {
         List <Line2D> lines = s.GetLines();
         for (int i = 0; i < lines.Count; i++)
         {
             Vector2D p1 = context.PointToScreen(lines[i].Start);
             Vector2D p2 = context.PointToScreen(lines[i].End);
             context.Graphics.DrawLine(p1, p2, GetColor(s, Colors.Shape2D), 2);
         }
     }
 }
Example #26
0
    public override bool HasPreviewGUI()
    {
        _shape2D = (Shape2D)target;
        _mesh    = MeshFromShape(_shape2D, 1);

        if (_preview == null && _material != null)
        {
            _preview = new ScenePreview();
            _preview.AddModel(_mesh, Matrix4x4.identity, _material);
        }

        return(true);
    }
    private Mesh ExtrudeShape(Shape2D visualShape, Shape2D coverShape = null)
    {
        // At least two control points are needed to extrude the shape
        if (_controlPoints.Length < 2)
        {
            Debug.LogWarning("WARNING: At least 2 control points needed to extrude!");
            return(null);
        }

        // Creates and populates the mesh
        Mesh mesh = new Mesh();

        mesh.vertices = CreateVertices(visualShape);
        mesh.uv       = CreateUVs(visualShape);

        // Creates the mesh triangles
        mesh.triangles = CreateTriangles(visualShape);

        // Calculates the mesh's normals
        if (recalculateNormals)
        {
            mesh.RecalculateNormals();
        }
        else
        {
            mesh.normals = CreateNormals(visualShape);
        }

        // If the shape is closed, adds the covers
        if (closeShape)
        {
            try {
                Mesh closedMesh = new Mesh();
                if (coverShape == null)
                {
                    closedMesh.CombineMeshes(CloseShape(mesh, visualShape), false, false);
                }
                else
                {
                    closedMesh.CombineMeshes(CloseShape(mesh, coverShape), false, false);
                }
                mesh = closedMesh;
            }
            catch (InvalidOperationException) {
                Debug.LogError("ERROR: The selected shape is not closed and cannot be used as a cover!");
            }
        }

        return(mesh);
    }
Example #28
0
        public Input(Shape2D playerShape, Window window, ShapeHandler shapeHandler, Sound Sound, Player player, LevelHandler levelHandler)
        {
            this.playerShape  = playerShape;
            this.shapeHandler = shapeHandler;
            this.Sound        = Sound;
            this.player       = player;
            this.levelHandler = levelHandler;

            lastPos = new Vector2d();

            window.KeyDown    += Window_KeyDown;
            window.KeyUp      += Window_KeyUp;
            window.MouseClick += Window_MouseClick;
        }
Example #29
0
        private Shape2D getRandomClosedShape()
        {
            var polyPoints = new List <Point>();

            for (var i = -3; i < _rnd.Next(20); ++i)
            {
                polyPoints.Add(getRandomOutputPoint());
            }

            var demoShape = new Shape2D(polyPoints.ToArray());

            demoShape = demoShape.AsClosed();
            return(demoShape);
        }
Example #30
0
        public void Window_MouseClick(object sender, MouseEventArgs e)
        {
            Vector2d mousePos = new Vector2d(e.X, e.Y);

            if (e.Button == MouseButtons.Left)
            {
                Console.WriteLine("CLICKED!");
                if (shapeHandler.IsCollided(mousePos, TypeSpec.Falling) != null)
                {
                    Shape2D clickedObject = shapeHandler.IsCollided(mousePos, TypeSpec.Falling);
                    shapeHandler.removeShape(clickedObject);
                }
            }
        }