Exemplo n.º 1
0
        public void TestVerticesIsConvex1()
        {
            KVertices rect = new KVertices
            {
                new KVector2(0, 1),
                new KVector2(1, 1),
                new KVector2(1, 0),
                new KVector2(0, 0)
            };

            Assert.AreEqual(true, rect.IsConvex());
        }
Exemplo n.º 2
0
        public void TestVerticesIsConvex2()
        {
            KVertices poly = new KVertices
            {
                new KVector2(0, 1),
                new KVector2(0.5f, 0.5f),
                new KVector2(1, 1),
                new KVector2(1, 0),
                new KVector2(0, 0)
            };

            Assert.AreEqual(false, poly.IsConvex());
        }
Exemplo n.º 3
0
        public void TestVerticesIsConvex3()
        {
            KVertices poly = new KVertices
            {
                new KVector2(0, 0),
                new KVector2(0f, 1f),
                new KVector2(1, 1),
                new KVector2(1.5f, 1.5f),
                new KVector2(1, 1),
                new KVector2(1, 0)
            };

            Assert.AreEqual(false, poly.IsConvex());
            Assert.AreEqual(4, poly.Count);
        }
Exemplo n.º 4
0
        public void TestOverlapSquareAndTrangle2()
        {
            KTransform t1 = new KTransform(KVector2.Zero, 0, KVector2.One);
            KTransform t2 = new KTransform(KVector2.Zero, 0f, KVector2.One);

            KVertices vA = new KVertices()
            {
                new KVector2(0f, 1f),
                new KVector2(0.5f, 0f),
                new KVector2(-0.5f, 0f),
            };
            KVertices vB = new KVertices()
            {
                new KVector2(0.9f, 0.5f),
                new KVector2(0.9f, -0.5f),
                new KVector2(-0.2f, -0.5f),
                new KVector2(-0.2f, 0.5f),
            };

            vA.Reverse();
            vB.Reverse();

            Rigidbody tri = BodyFactory.CreatePolygon(t1, new MassData(1, 1), vA);
            Rigidbody sqr = BodyFactory.CreatePolygon(t2, new MassData(1, 1), vB);

            Fixture fA = new Fixture(sqr, sqr.Colliders[0]);
            Fixture fB = new Fixture(tri, tri.Colliders[0]);

            Contact c = new Contact(fA, fB);

            bool result = true;

            while (sqr.State.Transform.Rotation < 2 * Math.PI)
            {
                sqr.State.Transform.Rotation += 0.01f;
                if (!CollisionDetection.Collision(ref c))
                {
                    result = false;
                    break;
                }
            }

            Assert.AreEqual(true, result);
        }
Exemplo n.º 5
0
        public void TestOverlapSquareAnd8Poly()
        {
            KTransform t1 = new KTransform(KVector2.Zero, 0, KVector2.One);
            KTransform t2 = new KTransform(KVector2.Zero, 0, KVector2.One);

            KVertices vA = new KVertices()
            {
                new KVector2(-1f, -1f),
                new KVector2(-2.62f, 0.18f),
                new KVector2(-2.62f, 3.98f),
                new KVector2(-1f, 5.16f),
                new KVector2(1f, 5.16f),
                new KVector2(2.62f, 3.98f),
                new KVector2(2.62f, 0.18f),
                new KVector2(1f, -1f),
            };

            KVertices vB = new KVertices()
            {
                new KVector2(0.5f, -0.5f),
                new KVector2(-0.5f, -0.5f),
                new KVector2(-0.5f, -1.5f),
                new KVector2(0.5f, -1.5f),
            };

            vA.Reverse();

            Rigidbody sqr  = BodyFactory.CreatePolygon(t1, new MassData(1, 1), vB);
            Rigidbody poly = BodyFactory.CreatePolygon(t2, new MassData(1, 1), vA);

            Fixture fA = new Fixture(sqr, sqr.Colliders[0]);
            Fixture fB = new Fixture(poly, poly.Colliders[0]);

            Contact c = new Contact(fA, fB);

            Assert.AreEqual(CollisionDetection.Collision(ref c), true);
        }
Exemplo n.º 6
0
        public void TestSeparationSAT1()
        {
            KTransform t1 = new KTransform(KVector2.Zero, 0, KVector2.One);
            KTransform t2 = new KTransform(KVector2.Zero, 0f, KVector2.One);

            KVertices vA = new KVertices()
            {
                new KVector2(0f, .5f),
                new KVector2(1f, 2f),
                new KVector2(-1f, 2f),
            };
            KVertices vB = new KVertices()
            {
                new KVector2(-1, -1),
                new KVector2(1, -1),
                new KVector2(1, 1),
                new KVector2(-1, 1),
            };

            Polygon tri = new Polygon(vA);

            tri.Transform = t1;
            Polygon sqr = new Polygon(vB);

            sqr.Transform = t2;

            CollisionDetection.SeparationSAT(tri, sqr, out int indexA, out double separationA);

            CollisionDetection.SeparationSAT(sqr, tri, out int indexB, out double separationB);

            Assert.IsFalse(separationA > Config.EpsilonsDouble);
            Assert.IsFalse(separationB > Config.EpsilonsDouble);

            Assert.AreEqual(new KVector2(0, .5f), tri.Vertices[indexA]);
            Assert.AreEqual(new KVector2(1, 1), sqr.Vertices[indexB]);
        }
Exemplo n.º 7
0
        public Scene()
        {
            KTransform      t1 = new KTransform(new KVector2(0f, 2f), 1f, new KVector2(3, 1));
            KTransform      t2 = new KTransform(new KVector2(0, 0f), 0f, new KVector2(1, 1));
            KTransform      t3 = new KTransform(new KVector2(0f, 9f), 0.5, new KVector2(2, 1));
            List <Collider> ss = new List <Collider>
            {
                new Circle(new KVector2(0, 0)),
                new Circle(new KVector2(10, 4))
            };
            //Vertices v1 = new Vertices()
            //{
            // new KVector2(-1f, -1f),
            // new KVector2(-2.62f, 0.18f),
            // new KVector2(-2.62f, 3.98f),
            // new KVector2(-1f, 5.16f),
            // new KVector2(1f, 5.16f),
            // new KVector2(2.62f, 3.98f),
            // new KVector2(2.62f, 0.18f),
            // new KVector2(1f, -1f),
            //};
            //v1.Reverse();
            KVertices v1 = new KVertices()
            {
                new KVector2(0.5f, 0.5f),
                new KVector2(-0.5f, 0.5f),
                new KVector2(-0.5f, -0.5f),
                new KVector2(0.5f, -0.5f),
            };
            //Vertices v1 = new Vertices()
            //{
            //    new KVector2(-0.4330127018922f,-0.25f),
            //    new KVector2(0.4330127018922f,-0.25f),
            //    new KVector2(0f,0.5f),
            //};
            //v1.Reverse();
            //Vertices v = new Vertices()
            //{
            //    new KVector2(0.5f,0.5f),
            //    new KVector2(-0.5f,0.5f),
            //    new KVector2(-0.5f,-0.5f),
            //    new KVector2(0.5f,-0.5f),
            //};
            KVertices v = new KVertices()
            {
                new KVector2(5f, 5f),
                new KVector2(-5f, 5f),
                new KVector2(-5f, -5f),
                new KVector2(5f, -5f),
            };

            Rigidbody s  = BodyFactory.CreatePolygon(t1, new MassData(3, 3), v1);
            Rigidbody s2 = BodyFactory.CreatePolygon(t3, new MassData(1, 3), v1);

            c = BodyFactory.CreateCircleBody(new MassData(1, 1), t3);
            Rigidbody seg = BodyFactory.CreateSegmentBody(new KVector2(-1, 0), new KVector2(1, 0), new MassData(1, 1), t2);

            rect = BodyFactory.CreatePolygon(t2, new MassData(1, 0), v);
            //Bodies.Add(BodyFactory.CreateCircleBody(new MassData(1, 1), t1));
            //Bodies.Add(seg);
            Bodies.Add(rect);
            Bodies.Add(c);
            s.AddImpulseAtWPos(new KVector2(3f, 0), s.State.Transform.Position);

            s.State.ForceGenerators.Add("G", new GravityHMR(new KVector2(0, -2)));
            c.State.ForceGenerators.Add("G", new GravityHMR(new KVector2(0, -2)));

            Integrator = new RK4();
        }
Exemplo n.º 8
0
 public static Rigidbody CreatePolygon(KTransform tx, MassData massData, KVertices vertices)
 {
     return(new Rigidbody(new Polygon(vertices), massData, tx));
 }
Exemplo n.º 9
0
 public Polygon(KVertices vertices)
 {
     this.Vertices = vertices;
     Type          = ColliderType.POLYGON;
     ComputeProperties();
 }