Пример #1
0
        public List <Segment> getSegments(Segment seg)
        {
            List <Segment> segments = new List <Segment>();
            List <Vector2> squares  = Collisions.getSquares(seg, cellSize);


            foreach (RectCollisionHull col in Hulls)
            {
                if (!col.Static)
                {
                    col.genBounds();
                }
                foreach (Vector2 square in squares)
                {
                    if (col.boundMin.X <(square.X *cellSize) + cellSize& col.boundMax.X> square.X * cellSize & col.boundMax.Y > (square.Y * cellSize) & col.boundMin.Y < (square.Y * cellSize) + cellSize)
                    {
                        segments.AddRange(col.getSegments());
                        break;
                    }
                }
            }



            return(segments);
        }
Пример #2
0
        public List <Segment> getSegmentsHash(Segment seg)
        {
            List <Segment> segments = new List <Segment>();
            List <Vector2> squares  = Collisions.getSquares(seg, cellSize);

            foreach (Vector2 square in squares)
            {
                List <CollisionHull> hulls = table.getHulls(square);
                if (hulls != null)
                {
                    foreach (CollisionHull h in hulls)
                    {
                        segments.AddRange(h.getSegments());
                    }
                }
            }

            return(segments);
        }
Пример #3
0
        public List <Vector2> generateMesh(PhysicsWorld pWorld)
        {
            List <Vector2> vertices = new List <Vector2>();
            List <Vector2> corners  = new List <Vector2>();

            corners.Add(new Vector2(position.X - Size.X / 2, position.Y + Size.Y / 2));
            corners.Add(new Vector2(position.X - Size.X / 2, position.Y - Size.Y / 2));
            corners.Add(new Vector2(position.X + Size.X / 2, position.Y - Size.Y / 2));
            corners.Add(new Vector2(position.X + Size.X / 2, position.Y + Size.Y / 2));

            List <Segment> segments = new List <Segment>();


            for (int i = 0; i < pWorld.Hulls.Count; i++)
            {
                List <Vector2> verts = pWorld.Hulls[i].getVertices();

                for (int j = 0; j < verts.Count; j++)
                {
                    segments.Clear();
                    segments.Add(new Segment(new Vector2(position.X - Size.X / 2, position.Y + Size.Y / 2), new Vector2(position.X - Size.X / 2, position.Y - Size.Y / 2)));
                    segments.Add(new Segment(new Vector2(position.X - Size.X / 2, position.Y - Size.Y / 2), new Vector2(position.X + Size.X / 2, position.Y - Size.Y / 2)));
                    segments.Add(new Segment(new Vector2(position.X + Size.X / 2, position.Y - Size.Y / 2), new Vector2(position.X + Size.X / 2, position.Y + Size.Y / 2)));
                    segments.Add(new Segment(new Vector2(position.X + Size.X / 2, position.Y + Size.Y / 2), new Vector2(position.X - Size.X / 2, position.Y + Size.Y / 2)));
                    //Vector2 dir = verts[j] - position;
                    //dir.Normalize();
                    Segment ray = new Segment(position, verts[j]);
                    segments.AddRange(pWorld.getSegmentsHash(ray));
                    //segments.AddRange(pWorld.getSegmentsHash(ray));

                    bool intersect = false;

                    Vector2 vertex       = verts[j];
                    Vector2 intersection = verts[j];
                    Vector2 tempIntersection;
                    float   dist = float.MaxValue;
                    float   temp;

                    for (int k = 0; k < segments.Count; k++)
                    {
                        if (Collisions.LineSegementsIntersect(ray.start, ray.end, segments[k].start, segments[k].end, out tempIntersection, out temp))
                        {
                            intersect = true;
                            if (temp < dist)
                            {
                                dist         = temp;
                                intersection = tempIntersection;
                            }
                        }
                    }

                    if (Math.Abs(vertex.X - intersection.X) < 1 & Math.Abs(vertex.Y - intersection.Y) < 1)
                    {
                        Vector2 rotIntersection = new Vector2();
                        Vector2 rotated         = (intersection - position).rotate(-0.001f) + position;
                        Vector2 dir             = rotated - position;
                        dir.Normalize();
                        dir    *= 600;
                        rotated = position + dir;
                        ray.end = rotated;

                        segments.Clear();
                        segments.Add(new Segment(new Vector2(position.X - Size.X / 2, position.Y + Size.Y / 2), new Vector2(position.X - Size.X / 2, position.Y - Size.Y / 2)));
                        segments.Add(new Segment(new Vector2(position.X - Size.X / 2, position.Y - Size.Y / 2), new Vector2(position.X + Size.X / 2, position.Y - Size.Y / 2)));
                        segments.Add(new Segment(new Vector2(position.X + Size.X / 2, position.Y - Size.Y / 2), new Vector2(position.X + Size.X / 2, position.Y + Size.Y / 2)));
                        segments.Add(new Segment(new Vector2(position.X + Size.X / 2, position.Y + Size.Y / 2), new Vector2(position.X - Size.X / 2, position.Y + Size.Y / 2)));

                        segments.AddRange(pWorld.getSegmentsHash(ray));

                        for (int k = 0; k < segments.Count; k++)
                        {
                            if (Collisions.LineSegementsIntersect(ray.start, ray.end, segments[k].start, segments[k].end, out tempIntersection, out temp))
                            {
                                intersect = true;
                                if (temp < dist)
                                {
                                    dist            = temp;
                                    rotIntersection = tempIntersection;
                                }
                            }
                        }

                        vertices.Add(rotIntersection);

                        Vector2 rotIntersection1 = new Vector2();
                        Vector2 tempIntersection1;
                        float   dist1 = float.MaxValue;
                        float   temp1;

                        rotated = (intersection - position).rotate(0.001f) + position;
                        dir     = rotated - position;
                        dir.Normalize();
                        dir    *= 600;
                        rotated = position + dir;
                        ray.end = rotated;

                        segments.Clear();
                        segments.Add(new Segment(new Vector2(position.X - Size.X / 2, position.Y + Size.Y / 2), new Vector2(position.X - Size.X / 2, position.Y - Size.Y / 2)));
                        segments.Add(new Segment(new Vector2(position.X - Size.X / 2, position.Y - Size.Y / 2), new Vector2(position.X + Size.X / 2, position.Y - Size.Y / 2)));
                        segments.Add(new Segment(new Vector2(position.X + Size.X / 2, position.Y - Size.Y / 2), new Vector2(position.X + Size.X / 2, position.Y + Size.Y / 2)));
                        segments.Add(new Segment(new Vector2(position.X + Size.X / 2, position.Y + Size.Y / 2), new Vector2(position.X - Size.X / 2, position.Y + Size.Y / 2)));



                        segments.AddRange(pWorld.getSegmentsHash(ray));

                        for (int k = 0; k < segments.Count; k++)
                        {
                            if (Collisions.LineSegementsIntersect(ray.start, ray.end, segments[k].start, segments[k].end, out tempIntersection1, out temp1))
                            {
                                intersect = true;
                                if (temp1 < dist1)
                                {
                                    dist1            = temp1;
                                    rotIntersection1 = tempIntersection1;
                                }
                            }
                        }

                        vertices.Add(rotIntersection1);
                    }
                    vertices.Add(intersection);

                    for (int k = 0; k < corners.Count; k++)
                    {
                        segments.Clear();
                        segments.Add(new Segment(new Vector2(position.X - Size.X / 2, position.Y + Size.Y / 2), new Vector2(position.X - Size.X / 2, position.Y - Size.Y / 2)));
                        segments.Add(new Segment(new Vector2(position.X - Size.X / 2, position.Y - Size.Y / 2), new Vector2(position.X + Size.X / 2, position.Y - Size.Y / 2)));
                        segments.Add(new Segment(new Vector2(position.X + Size.X / 2, position.Y - Size.Y / 2), new Vector2(position.X + Size.X / 2, position.Y + Size.Y / 2)));
                        segments.Add(new Segment(new Vector2(position.X + Size.X / 2, position.Y + Size.Y / 2), new Vector2(position.X - Size.X / 2, position.Y + Size.Y / 2)));


                        ray = new Segment(position, corners[j]);
                        segments.AddRange(pWorld.getSegmentsHash(ray));
                        intersect    = false;
                        vertex       = corners[j];
                        intersection = corners[j];

                        dist = float.MaxValue;


                        for (int l = 0; l < segments.Count; l++)
                        {
                            if (Collisions.LineSegementsIntersect(ray.start, ray.end, segments[l].start, segments[l].end, out tempIntersection, out temp))
                            {
                                intersect = true;
                                if (temp < dist)
                                {
                                    dist         = temp;
                                    intersection = tempIntersection;
                                }
                            }
                        }
                    }
                    vertices.Add(intersection);
                }
            }
            //Sort for triangle draw
            Collisions.sortClockwise(vertices, position);

            //Remove vertices outside of size
            for (int j = 0; j < vertices.Count; j++)
            {
                if (vertices[j].X <position.X - (Size.X / 2) - 10 || vertices[j].X> position.X + Size.X / 2 + 10 || vertices[j].Y <position.Y - ((Size.Y / 2)) - 10 || vertices[j].Y> position.Y + Size.X / 2 + 10)
                {
                    vertices.RemoveAt(j);
                    j--;
                    continue;
                }
            }

            return(vertices);
        }