Ejemplo n.º 1
0
        public Vec2 Normal()
        {
            Vec2 unitNormal = new Vec2(-y, x);

            unitNormal.Normalize();
            return(unitNormal);
        }
Ejemplo n.º 2
0
        private void Control()
        {
            if (Input.GetKey(Key.W))
            {
                force.y -= 1;
                SetState("^");
            }

            if (Input.GetKey(Key.S))
            {
                force.y += 1;
                SetState("v");
            }

            if (Input.GetKey(Key.A))
            {
                force.x -= 1;
                SetState("<");
            }

            if (Input.GetKey(Key.D))
            {
                force.x += 1;
                SetState(">");
            }

            force.Normalize();
            force *= _speed;

            AnimationHandeler();
        }
Ejemplo n.º 3
0
 private void EulerIntegration()
 {
     _oldPosition = Position;
     _velocity   += _acceleration;
     if (_velocity.Length() > _maxSpeed)
     {
         _velocity.Normalize();
         _velocity *= _maxSpeed;
     }
     Position += _velocity;
     SetXY(Position.x, Position.y);
 }
Ejemplo n.º 4
0
        void calcDistToPoint()
        {
            if (currentPoint.x != 0 && currentPoint.y != 0)
            {
                velocity.SetXY(0, 0);
                if (isFishHungry <= 3000)
                {
                    if (isFoodPresent())
                    {
                        calcNearestFood();
                    }
                }
                Vec2 deltaVector = currentPoint - _position;

                if (deltaVector.Magnitude() <= 0.5f)
                {
                    currentPoint.SetXY(0, 0);
                    if (isFoodPresent())
                    {
                        if (currentFood != null && isFishHungry <= 3000)
                        {
                            RemoveFood(currentFood);
                            currentFood.LateDestroy();
                            isFishHungry += 4000;
                        }
                    }
                }
                else
                {
                    deltaVector.Normalize();
                    //deltaVector *= 0.2f;
                    velocity += deltaVector;
                }
            }
            else
            {
                currentPoint.SetXY(Utils.Random(50, game.width - 50), Utils.Random(_position.y - 100, _position.y + 100));
                if (isFoodPresent())
                {
                    if (isFishHungry <= 3000)
                    {
                        calcNearestFood();
                    }
                }
            }
        }
Ejemplo n.º 5
0
        void UpdatePattern()
        {
            if (patternIndex > destination.Count - 1)
            {
                patternIndex = 0;
            }
            //Console.WriteLine($"Heading towards {destination[patternIndex]}, Curently at {_position}");
            //Console.WriteLine($"But should be OK if in between {destination[patternIndex] - new Vec2(20, 20)} and {destination[patternIndex] + new Vec2(20, 20)}");

            _force.x = destination[patternIndex].x - _position.x;
            _force.y = destination[patternIndex].y - _position.y;

            _force.Normalize();
            _force *= speed;

            if (_position > destination[patternIndex] - new Vec2(speed, speed))
            {
                if (_position < destination[patternIndex] + new Vec2(speed, speed))
                {
                    patternIndex++;
                }
            }
        }
Ejemplo n.º 6
0
        public TestVectorFunction()
        {
            //-------------------------------------------------------------------------
            //						Addition
            //-------------------------------------------------------------------------
            Vec2 testVectorAddition1 = new Vec2(2, 3);
            Vec2 testVectorAddition2 = new Vec2(3, 4);

            Console.WriteLine("Addition ok ?:" + (testVectorAddition1.x + testVectorAddition2.x == 5 && testVectorAddition1.y + testVectorAddition2.y == 7));

            //-------------------------------------------------------------------------
            //						Subtraction
            //-------------------------------------------------------------------------
            Vec2 testVectorSubtraction1 = new Vec2(2, 3);
            Vec2 testVectorSubtraction2 = new Vec2(3, 4);

            Console.WriteLine("Subtraction ok ?:" + (testVectorSubtraction1.x - testVectorSubtraction2.x == -1 && testVectorSubtraction1.y - testVectorSubtraction2.y == -1));

            //-------------------------------------------------------------------------
            //						Multiplication Right
            //-------------------------------------------------------------------------
            Vec2 testMultiplicationRight   = new Vec2(2, 3);
            Vec2 resultMultiplicationRight = testMultiplicationRight * 3;

            Console.WriteLine("Scalar multiplication right ok ?:" + (resultMultiplicationRight.x == 6 && resultMultiplicationRight.y == 9 && testMultiplicationRight.x == 2 && testMultiplicationRight.y == 3));

            //-------------------------------------------------------------------------
            //						Multiplication Left
            //-------------------------------------------------------------------------

            Vec2 testMultiplicationLeft   = new Vec2(2, 3);
            Vec2 resultMultiplicationLeft = testMultiplicationRight * 3;

            Console.WriteLine("Scalar multiplication right ok ?:" + (resultMultiplicationLeft.x == 6 && resultMultiplicationLeft.y == 9 && testMultiplicationLeft.x == 2 && testMultiplicationLeft.y == 3));

            //-------------------------------------------------------------------------
            //							Magnitude
            //-------------------------------------------------------------------------
            Vec2 testVectorMagnitude = new Vec2(3, 4);

            Console.WriteLine("Magnitude ok ?:" + (testVectorMagnitude.Magnitude() == 5f));

            //-------------------------------------------------------------------------
            //							Normalize Current Vector
            //-------------------------------------------------------------------------
            Vec2 testVectorNormalizeCurrent = new Vec2(3, 4);

            testVectorNormalizeCurrent.Normalize();
            Console.WriteLine("Normalized Curent Vector ok ?:" + (testVectorNormalizeCurrent.x == 0.6f && testVectorNormalizeCurrent.y == 0.8f));

            //-------------------------------------------------------------------------
            //							Normalize New Vector
            //-------------------------------------------------------------------------
            Vec2 testVectorNormalizeOther = new Vec2(3, 4);
            Vec2 normalizedVector         = testVectorNormalizeOther.Normalized();

            Console.WriteLine("Normalized New Vector ok ?:" + (normalizedVector.x == 0.6f && normalizedVector.y == 0.8f));

            //-------------------------------------------------------------------------
            //							SetScale Vector
            //-------------------------------------------------------------------------
            Vec2 testVectorSetScale = new Vec2(3, 4);

            testVectorSetScale.SetXY(5, 6);
            Console.WriteLine("SetScale Vector ok ?:" + (testVectorSetScale.x == 5 && testVectorSetScale.y == 6));
        }
Ejemplo n.º 7
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));
        }
Ejemplo n.º 8
0
 public Vec2 Normal()
 {
     Vec2 unitNormal = new Vec2(-y, x);
     unitNormal.Normalize();
     return unitNormal;
 }