Exemplo n.º 1
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));
        }
Exemplo n.º 2
0
 public void Reflect(float COR, Vec2 other)
 {
     this = this - ((1 + COR) * (Dot(other.Normal()) * other.Normal()));
 }