Example #1
0
 public static IEnumerable <Vector2f> GetPoints(this ConvexShape shape)
 {
     for (uint i = 0; i < shape.GetPointCount(); i++)
     {
         yield return(shape.GetPoint(i));
     }
 }
Example #2
0
 public Vector2[] GetPoints()
 {
     Vector2[] points = new Vector2[shape.GetPointCount()];
     for (uint i = 0; i < points.Length; i++)
     {
         points[i] = (Vector2)shape.GetPoint(i);
     }
     return(points);
 }
Example #3
0
        public static bool Contain(this ConvexShape convexShape, Vector2f point)
        {
            var shapePointCount = convexShape.GetPointCount();
            var count           = 0;

            for (uint i = 0, j = shapePointCount - 1; i < shapePointCount; j = i++)
            {
                var current = convexShape.GetPoint(i);
                var last    = convexShape.GetPoint(j);

                if (IsBetween(current, last, point) &&
                    (point.X > (last.X - current.X) * (point.Y - current.Y) / (last.Y - current.Y) + current.X))
                {
                    count++;
                }
            }

            return(count % 2 == 1);
        }
Example #4
0
        public KeyControl(float size)
        {
            array = new ConvexShape(8);
            array.SetPoint(0, new Vector2f(0, -size));
            array.SetPoint(1, new Vector2f(.3f * size, -.3f * size));
            array.SetPoint(2, new Vector2f(size, 0));
            array.SetPoint(3, new Vector2f(.3f * size, .3f * size));
            array.SetPoint(4, new Vector2f(0, size));
            array.SetPoint(5, new Vector2f(-.3f * size, .3f * size));
            array.SetPoint(6, new Vector2f(-size, 0));
            array.SetPoint(7, new Vector2f(-.3f * size, -.3f * size));
            array.OutlineColor     = Color.Black;
            array.OutlineThickness = -1f;

            var tmp = new CustomHitbox();

            for (uint i = 0; i < 8; i++)
            {
                tmp.CustomShape.Add(array.GetPoint(i));
            }
            HitBox = tmp;
        }
Example #5
0
        public void Draw(Box box)
        {
            var scale = new Vector2f(box.Width, box.Height);
            var color = box.Intersected ? Color.Red : Color.Green;

            _shape.Position         = box.Position;
            _shape.Rotation         = box.Rotation;
            _shape.Scale            = scale;
            _shape.OutlineColor     = color;
            _shape.FillColor        = Color.Transparent;
            _shape.OutlineThickness = 1f / scale.X;
            _renderTarget.Draw(_shape);

            var point = _shape.GetPoint(0);

            point = RotateVector(point, new Vector2f(0f, 0f), (float)MathUtils.DegreeToRadian(box.Rotation));
            var first = point.Multiply(scale) + box.Position;

            _renderTarget.Draw(new []
            {
                new Vertex(box.Position, color),
                new Vertex(first, color)
            }, 0, 2, PrimitiveType.Lines);
        }
Example #6
0
        public static void ProjectPolygon(Vector2f axis, ConvexShape polygon,
                                          ref float min, ref float max)
        {
            // To project a point on an axis use the dot product
            float dotProduct = Trig.DotProduct(axis, polygon.Transform.TransformPoint(polygon.GetPoint(0)));

            min = dotProduct;
            max = dotProduct;
            for (uint i = 0; i < polygon.GetPointCount(); i++)
            {
                dotProduct = Trig.DotProduct(polygon.Transform.TransformPoint(polygon.GetPoint(i)), axis);
                if (dotProduct < min)
                {
                    min = dotProduct;
                }
                else
                {
                    if (dotProduct > max)
                    {
                        max = dotProduct;
                    }
                }
            }
        }
Example #7
0
        public static List <ConvexShape> LoadFromPoly(out CorrectPath path, string title)
        {
            const int rectsize = 60;
            const int wallSize = 45;

            ConvexShape        pathShape = LoadObjects(title)[0];
            List <ConvexShape> pathList  = new List <ConvexShape>();
            List <ConvexShape> mapList   = new List <ConvexShape>();

            for (uint i = 0; i < pathShape.GetPointCount(); i++)
            {
                Vector2f        point1    = pathShape.GetPoint(i) + pathShape.Position;
                Vector2f        point2    = pathShape.GetPoint((i + 1) % pathShape.GetPointCount()) + pathShape.Position;
                float           angle     = Trig.FindAngleToTarget(point1, point2);
                ConvexShape     myShape   = new ConvexShape(4);
                List <Vector2f> pathPoint = new List <Vector2f>();
                pathPoint.Add(point1 + Trig.MoveDist(angle + 90, rectsize));
                pathPoint.Add(point1 + Trig.MoveDist(angle - 90, rectsize));
                pathPoint.Add(point2 + Trig.MoveDist(angle - 90, rectsize));
                pathPoint.Add(point2 + Trig.MoveDist(angle + 90, rectsize));
                Vector2f min = pathPoint[0];
                foreach (Vector2f point in pathPoint)
                {
                    if (point.X < min.X)
                    {
                        min.X = point.X;
                    }
                    if (point.Y < min.Y)
                    {
                        min.Y = point.Y;
                    }
                }
                for (int j = 0; j < pathPoint.Count; j++)
                {
                    pathPoint[j] -= min;
                }
                myShape.Position = min;
                for (int j = 0; j < pathPoint.Count; j++)
                {
                    myShape.SetPoint((uint)j, pathPoint[j]);
                }
                pathList.Add(myShape);
            }

            pathList = new Sempoolfi(pathList).Run();


            for (int i = 0; i < pathList.Count; i++)
            {
                int      index  = (i + 1) % pathList.Count;
                Vector2f point1 = pathList[i].GetPoint(3) + pathList[i].Position;
                Vector2f point2 = pathList[i].GetPoint(0) + pathList[i].Position;

                Vector2f    point3 = point1;
                ConvexShape shape1 = new ConvexShape(2);
                Vector2f    min    = point1;
                if (point2.X < min.X)
                {
                    min.X = point2.X;
                }
                if (point2.Y < min.Y)
                {
                    min.Y = point2.Y;
                }
                if (point3.X < min.X)
                {
                    min.X = point3.X;
                }
                if (point3.Y < min.Y)
                {
                    min.Y = point3.Y;
                }
                point1         -= min;
                point2         -= min;
                point3         -= min;
                shape1.Position = min;
                shape1.SetPoint(0, point1);
                shape1.SetPoint(1, point2);
                //shape1.SetPoint(2, point3);
                shape1.FillColor = Color.Black;
                mapList.Add(shape1);

                Vector2f    point1_2 = pathList[i].GetPoint(2) + pathList[i].Position;
                Vector2f    point2_2 = pathList[i].GetPoint(1) + pathList[i].Position;
                Vector2f    point3_2 = point1_2;
                ConvexShape shape2   = new ConvexShape(2);
                Vector2f    min2     = point1_2;
                if (point2_2.X < min2.X)
                {
                    min2.X = point2_2.X;
                }
                if (point2_2.Y < min2.Y)
                {
                    min2.Y = point2_2.Y;
                }
                if (point3_2.X < min2.X)
                {
                    min2.X = point3_2.X;
                }
                if (point3_2.Y < min2.Y)
                {
                    min2.Y = point3_2.Y;
                }
                point1_2       -= min2;
                point2_2       -= min2;
                point3_2       -= min2;
                shape2.Position = min2;
                shape2.SetPoint(0, point1_2);
                shape2.SetPoint(1, point2_2);
                //shape2.SetPoint(2, point3_2);
                shape2.FillColor = Color.Black;
                mapList.Add(shape2);
            }
            for (int i = 0; i < pathList.Count; i++)
            {
                int      index  = (i + 1) % pathList.Count;
                Vector2f point1 = pathList[i].GetPoint(3) + pathList[i].Position;
                Vector2f point2 = pathList[index].GetPoint(0) + pathList[index].Position;

                Vector2f    point3 = point1;
                ConvexShape shape1 = new ConvexShape(2);
                Vector2f    min    = point1;
                if (point2.X < min.X)
                {
                    min.X = point2.X;
                }
                if (point2.Y < min.Y)
                {
                    min.Y = point2.Y;
                }
                if (point3.X < min.X)
                {
                    min.X = point3.X;
                }
                if (point3.Y < min.Y)
                {
                    min.Y = point3.Y;
                }
                point1         -= min;
                point2         -= min;
                point3         -= min;
                shape1.Position = min;
                shape1.SetPoint(0, point1);
                shape1.SetPoint(1, point2);
                //shape1.SetPoint(2, point3);
                shape1.FillColor = Color.Black;
                mapList.Add(shape1);

                Vector2f    point1_2 = pathList[i].GetPoint(2) + pathList[i].Position;
                Vector2f    point2_2 = pathList[index].GetPoint(1) + pathList[index].Position;
                Vector2f    point3_2 = point1_2;
                ConvexShape shape2   = new ConvexShape(2);
                Vector2f    min2     = point1_2;
                if (point2_2.X < min2.X)
                {
                    min2.X = point2_2.X;
                }
                if (point2_2.Y < min2.Y)
                {
                    min2.Y = point2_2.Y;
                }
                if (point3_2.X < min2.X)
                {
                    min2.X = point3_2.X;
                }
                if (point3_2.Y < min2.Y)
                {
                    min2.Y = point3_2.Y;
                }
                point1_2       -= min2;
                point2_2       -= min2;
                point3_2       -= min2;
                shape2.Position = min2;
                shape2.SetPoint(0, point1_2);
                shape2.SetPoint(1, point2_2);
                //shape2.SetPoint(2, point3_2);
                shape2.FillColor = Color.Black;
                mapList.Add(shape2);
            }
            path = new CorrectPath(new GroupShape(pathList));
            return(mapList);
        }
Example #8
0
 /// <summary>
 /// Créer la forme selon les paramètres entrés
 /// </summary>
 /// <param name="index">Le numéro du sommet de la forme</param>
 /// <returns></returns>
 public Vector2f this[uint index]
 {
     get { return(shape.GetPoint(index)); }
     set { shape.SetPoint(index, value); }
 }
Example #9
0
        public static CollisionInfo CheckCol(ConvexShape shape1, ConvexShape shape2)
        {
            Vector2f max = new Vector2f(0, 0);

            for (uint i = 0; i < shape1.GetPointCount(); i++)
            {
                float x = shape1.GetPoint(i).X;
                if (x > max.X)
                {
                    max.X = x;
                }
                float y = shape1.GetPoint(i).Y;
                if (y > max.Y)
                {
                    max.Y = y;
                }
            }
            Vector2f center1 = shape1.Position + .5F * max;

            max = new Vector2f(0, 0);
            for (uint i = 0; i < shape2.GetPointCount(); i++)
            {
                float x = shape2.GetPoint(i).X;
                if (x > max.X)
                {
                    max.X = x;
                }
                float y = shape2.GetPoint(i).Y;
                if (y > max.Y)
                {
                    max.Y = y;
                }
            }
            Vector2f center2 = shape2.Position + .5F * max;


            CollisionInfo myInfo = new CollisionInfo();

            myInfo.isCollide = true;
            float    intervalDist;
            float    minIntervalDist   = float.PositiveInfinity;
            Vector2f translationNormal = new Vector2f(0, 0);

            foreach (var polygon in new ConvexShape[] { shape1, shape2 })
            {
                for (int i1 = 0; i1 < polygon.GetPointCount(); i1++)
                {
                    int      i2 = (i1 + 1) % (int)polygon.GetPointCount();
                    Vector2f p1 = polygon.Transform.TransformPoint(polygon.GetPoint((uint)i1));
                    Vector2f p2 = polygon.Transform.TransformPoint(polygon.GetPoint((uint)i2));

                    Vector2f normal = new Vector2f(p2.Y - p1.Y, p1.X - p2.X);
                    normal = Trig.Normalize(normal);

                    float minA = 0; float minB = 0; float maxA = 0; float maxB = 0;
                    ProjectPolygon(normal, shape1, ref minA, ref maxA);
                    ProjectPolygon(normal, shape2, ref minB, ref maxB);
                    intervalDist = IntervalDistance(minA, maxA, minB, maxB);
                    if (minB > maxA || minA > maxB)
                    {
                        myInfo.isCollide = false;
                        break;
                    }

                    intervalDist = Math.Abs(intervalDist);
                    if (intervalDist < minIntervalDist)
                    {
                        minIntervalDist   = intervalDist;
                        translationNormal = normal;

                        float d = (minA + maxA) / 2 - (minB + maxB) / 2;
                        if (d < 0)
                        {
                            translationNormal = -translationNormal;
                        }
                    }
                }
                if (myInfo.isCollide == false)
                {
                    break;
                }
            }
            if (myInfo.isCollide)
            {
                myInfo.minIntervalDist = minIntervalDist * translationNormal;
                myInfo.shapes          = new List <ConvexShape>()
                {
                    shape2
                };
            }
            return(myInfo);
        }
Example #10
0
        /// <summary>
        /// Creates the wheels for the car.  Must be called after CreateBody.
        /// </summary>
        private void CreateWheels()
        {
            Debug.Assert(m_bodyShape != null);
            Debug.Assert(m_bodyBody != null);

            m_wheelShapes = new CircleShape[Definition.NumWheels];
            m_wheelLines  = new RectangleShape[Definition.NumWheels];
            m_wheelBodies = new Body[Definition.NumWheels];
            m_wheelJoints = new RevoluteJoint[Definition.NumWheels];
            m_torqueStep  = new float[Definition.NumWheels];

            for (int i = 0; i < m_wheelShapes.Length; i++)
            {
                // the offset of the attachment point from the center of the main body
                var attachOffset =
                    m_bodyShape.GetPoint((uint)m_definition.WheelAttachment[i]);
                // the world position of the attachment point
                var attachPos       = attachOffset + m_bodyShape.Position;
                var radius          = m_definition.CalcWheelRadius(i);
                var density         = m_definition.CalcWheelDensity(i);
                var densityFraction = density / Definition.MaxWheelDensity;
                // greater density = darker color
                byte color = (byte)(255 - (210 * densityFraction));

                var shape = new CircleShape
                {
                    FillColor        = new Color(color, color, color),
                    OutlineColor     = OutlineColor,
                    OutlineThickness = OutlineThickness,
                    Origin           = new Vector2f(radius, radius),
                    Position         = attachPos,
                    Radius           = radius
                };
                var line = new RectangleShape
                {
                    FillColor = Color.Black,
                    Origin    = new Vector2f(0, WheelAxisLineThickness / 2f),
                    Size      = new Vector2f(WheelAxisLineThickness, radius - WheelAxisLineThickness),
                    Position  = shape.Position
                };

                var body = BodyFactory.CreateCircle(
                    m_physicsManager.World, radius, density,
                    attachPos.ToVector2().InvertY()
                    );
                body.BodyType            = BodyType.Dynamic;
                body.Friction            = 1;
                body.CollidesWith        = Track.CollisionCategory;
                body.CollisionCategories = CollisionCategory;

                // need to catch the first collision of this body
                body.OnCollision += WheelInitialCollision;

                var joint = new RevoluteJoint(
                    m_bodyBody, attachOffset.ToVector2().InvertY(), body,
                    new Vector2(0, 0))
                {
                    MotorEnabled   = false,
                    MaxMotorTorque = 0,
                    // speed must be negative for clockwise rotation
                    MotorSpeed =
                        -(float)MathExtensions.DegToRad(m_definition.CalcWheelSpeed(i))
                };
                m_physicsManager.World.AddJoint(joint);

                m_wheelShapes[i] = shape;
                m_wheelLines[i]  = line;
                m_wheelBodies[i] = body;
                m_wheelJoints[i] = joint;
            }
        }