Sign() public method

public Sign ( Vector2D v2 ) : int
v2 Vector2D
return int
コード例 #1
0
        public bool RotateHeadingToFacePosition(Vector2D target)
        {
            Vector2D delta    = target - Position;
            Vector2D toTarget = Vector2D.Vec2DNormalize(delta);

            double dot = _heading.GetDotProduct(toTarget);

            //some compilers lose acurracy so the value is clamped to ensure it
            //remains valid for the acos
            if (dot < -1)
            {
                dot = -1;
            }
            else if (dot > 1)
            {
                dot = 1;
            }

            //first determine the angle between the heading vector and the target
            double angle = Math.Acos(dot);

            //return true if the player is facing the target
            if (angle < .00001)
            {
                return(true);
            }

            //clamp the amount to turn to the max turn rate
            if (angle > _maxTurnRate)
            {
                angle = _maxTurnRate;
            }

            //The next few lines use a rotation matrix to rotate the player's heading
            //vector accordingly
            Matrix2D rotationMatrix = new Matrix2D();

            //notice how the direction of rotation has to be determined when creating
            //the rotation matrix
            rotationMatrix.Rotate(angle * _heading.Sign(toTarget));
            rotationMatrix.TransformVector2Ds(_heading);
            rotationMatrix.TransformVector2Ds(_velocity);

            //finally recreate m_vSide
            _side = _heading.Perp;

            return(false);
        }