Esempio n. 1
0
        private void BoothCollision()
        {
            foreach (AnimationSprite currentStand in LevelLoader.CollisionObjectList)
            {
                List <Vec2> vectorsToCorners = new List <Vec2>();

                if (currentStand.rotation == 0 || currentStand.rotation == 180)
                {
                    vectorsToCorners.Add(new Vec2(currentStand.x - currentStand.width / 2, currentStand.y - currentStand.height / 2));
                    vectorsToCorners.Add(new Vec2(currentStand.x + currentStand.width / 2, currentStand.y - currentStand.height / 2));
                    vectorsToCorners.Add(new Vec2(currentStand.x + currentStand.width / 2, currentStand.y + currentStand.height / 2));
                    vectorsToCorners.Add(new Vec2(currentStand.x - currentStand.width / 2, currentStand.y + currentStand.height / 2));
                }
                else
                {
                    vectorsToCorners.Add(new Vec2(currentStand.x - currentStand.height / 2, currentStand.y - currentStand.width / 2));
                    vectorsToCorners.Add(new Vec2(currentStand.x + currentStand.height / 2, currentStand.y - currentStand.width / 2));
                    vectorsToCorners.Add(new Vec2(currentStand.x + currentStand.height / 2, currentStand.y + currentStand.width / 2));
                    vectorsToCorners.Add(new Vec2(currentStand.x - currentStand.height / 2, currentStand.y + currentStand.width / 2));
                }

                List <Vec2> sideVectors = new List <Vec2>();
                sideVectors.Add(vectorsToCorners.ElementAt(1) - vectorsToCorners.ElementAt(0));
                sideVectors.Add(vectorsToCorners.ElementAt(2) - vectorsToCorners.ElementAt(1));
                sideVectors.Add(vectorsToCorners.ElementAt(3) - vectorsToCorners.ElementAt(2));
                sideVectors.Add(vectorsToCorners.ElementAt(0) - vectorsToCorners.ElementAt(3));

                byte currentReferenceCornerVector = 0;

                foreach (Vec2 currentSideVector in sideVectors)
                {
                    Vec2 differenceVector = Position - vectorsToCorners.ElementAt(currentReferenceCornerVector);

                    float a = -differenceVector.Dot(currentSideVector.Normal()) - width / 2;
                    float b = _velocity.Dot(currentSideVector.Normal());

                    float currentVectorLength = currentSideVector.Length();

                    float t = a / b;

                    if (a > -width / 2 && a < 0)
                    {
                        t = 0;
                    }

                    if (b >= 0 && t >= 0 && t <= 1)
                    {
                        Vec2 POI = _oldPosition + t * _velocity;
                        differenceVector = POI - vectorsToCorners.ElementAt(currentReferenceCornerVector);
                        float distanceAlongLine = differenceVector.Dot(currentSideVector.Normalized());

                        if (distanceAlongLine >= 0 && distanceAlongLine <= currentVectorLength)
                        {
                            _velocity = new Vec2(0, 0);
                            Position  = POI;
                        }
                    }
                    currentReferenceCornerVector++;
                }
            }
        }
Esempio n. 2
0
        public static void UnitTest()
        {
            Vec2  testVec   = new Vec2(2, 4);
            Vec2  testVec2  = new Vec2(3, 4);
            float tempFloat = 0;

            Vec2 temp = testVec + testVec2;

            Console.WriteLine("Addition correct?: " + (Math.Abs(temp.x - 5) < 0.01f && Math.Abs(temp.y - 8) < 0.01f));

            temp = testVec - testVec2;
            Console.WriteLine("Substraction correct?: " + (Math.Abs(temp.x + 1) < 0.01f && Math.Abs(temp.y) < 0.01f));

            temp = 2 * testVec;
            Console.WriteLine("Multiplication on left correct?: " + (Math.Abs(temp.x - 4) < 0.01f && Math.Abs(temp.y - 8) < 0.01f));

            temp = testVec * 2;
            Console.WriteLine("Multiplication on right correct?: " + (Math.Abs(temp.x - 4) < 0.01f && Math.Abs(temp.y - 8) < 0.01f));

            tempFloat = testVec2.Length();
            Console.WriteLine("Length correct?: " + (Math.Abs(tempFloat - 5) < 0.01f));

            testVec2.Normalize();
            Console.WriteLine("Normalize correct?: " + (Math.Abs(testVec2.Length() - 1) < 0.01f && Math.Abs(testVec2.x - 0.6f) < 0.01f && Math.Abs(testVec2.y - 0.8f) < 0.01f));

            testVec2 = new Vec2(3, 4);

            temp = testVec2.Normalized();
            Console.WriteLine("Normalized correct?: " + (Math.Abs(temp.Length() - 1) < 0.01f && Math.Abs(temp.x - 0.6f) < 0.01f && Math.Abs(temp.y - 0.8f) < 0.01f));

            testVec.SetXY(15, 2);
            Console.WriteLine("SetXY correct?: " + (Math.Abs(testVec.x - 15) < 0.01f && Math.Abs(testVec.y - 2) < 0.01f));

            Console.WriteLine("Deg2Rad correct?: " + (Math.Abs(Deg2Rad(180) - Math.PI) < 0.01f));

            Console.WriteLine("Rad2Deg correct?: " + (Math.Abs(Rad2Deg((float)Math.PI) - 180) < 0.01f));

            testVec = GetUnitVectorDeg(180);
            Console.WriteLine("GetUnitVectorDeg correct?: " + (Math.Abs(testVec.x + 1) < 0.01f && Math.Abs(testVec.y) < 0.01f));

            testVec = GetUnitVectorRad((float)Math.PI);
            Console.WriteLine("GetUnitVectorRad correct?: " + (Math.Abs(testVec.x + 1) < 0.01f && Math.Abs(testVec.y) < 0.01f));

            Console.WriteLine("RandomUnitVector correct?: " + (Math.Abs(RandomUnitVector().Length() - 1) < 0.01f));

            testVec = RandomUnitVector();
            testVec.SetAngleDegrees(270);
            Console.WriteLine("SetAngleDegrees correct?: " + (Math.Abs(testVec.y + 1) < 0.01f && Math.Abs(testVec.x) < 0.01f));

            Console.WriteLine("GetAngleDegrees correct?: " + (Math.Abs(testVec.GetAngleDegrees() - 270) < 0.01f));

            testVec.SetAngleRadians((float)Math.PI);
            Console.WriteLine("SetAngleRadians correct?: " + (Math.Abs(testVec.x + 1) < 0.01f && Math.Abs(testVec.y) < 0.01f));

            Console.WriteLine("GetAngleRadians correct?: " + (Math.Abs(testVec.GetAngleRadians() - Math.PI) < 0.01f));

            testVec.RotateDegrees(90);
            Console.WriteLine("RotateDegrees correct?: " + (Math.Abs(testVec.GetAngleDegrees() - 270) < 0.01f));

            testVec = RandomUnitVector();
            testVec.SetAngleDegrees(180);

            testVec.RotateRadians((float)Math.PI / 2);
            Console.WriteLine("RotateRadians correct?: " + (Math.Abs(testVec.GetAngleRadians() - (3 * Math.PI) / 2) < 0.01f));

            testVec  = new Vec2(2, 4);
            testVec2 = new Vec2(3, 4);

            Console.WriteLine("Dot correct?: " + ((testVec.Dot(testVec2) - 22) < 0.01f));

            Vec2 testVecNormal = testVec2.Normal();

            Console.WriteLine("Normal correct?: " + (Math.Abs(testVecNormal.x + 0.8f) < 0.01f && Math.Abs(testVecNormal.y - 0.6f) < 0.01f && Math.Abs(testVecNormal.Length() - 1) < 0.01f));

            testVec.Reflect(1, testVec2);
            Console.WriteLine("Reflect correct?: " + (Math.Abs(testVec.x - 3.28f) < 0.01f && Math.Abs(testVec.y - 3.04f) < 0.01f));
        }