예제 #1
0
        ///<summary>
        ///Transforms a point from the agent's local space into world space
        ///</summary>
        ///<param name="point"></param>
        ///<param name="agentHeading"></param>
        ///<param name="agentSide"></param>
        ///<param name="agentPosition"></param>
        ///<returns>the transformed point</returns>
        public static Vector2 PointToWorldSpace(
            Vector2 point,
            Vector2 agentHeading,
            Vector2 agentSide,
            Vector2 agentPosition)
        {
            //make a copy of the point
            Vector2 transformedPoint = point; //TODO: why copy?

            //create a transformation matrix
            Matrix2 transformationMatrix = new Matrix2();

            //rotate
            transformationMatrix.Rotate(agentHeading, agentSide);

            //and translate
            transformationMatrix.Translate(agentPosition.X, agentPosition.Y);

            //now transform the point
            transformationMatrix.TransformVector2(ref transformedPoint);

            return transformedPoint;
        }
예제 #2
0
        ///<summary>
        ///given a target position, this method rotates the bot's facing vector
        ///by an amount not greater than <see cref="MovingEntity.MaxTurnRate"/>
        ///until it directly faces the target.
        ///</summary>
        ///<param name="target">the target position</param>
        ///<returns>
        ///true when the heading is facing in the desired direction
        /// </returns>
        public bool RotateFacingTowardPosition(Vector2 target)
        {
            Vector2 toTarget = Vector2.Normalize(target - Position);
            float dot = Vector2.Dot(Facing, toTarget);

            //clamp to rectify any rounding errors
            dot = MathHelper.Clamp(dot, -1, 1);

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

            //return true if the bot's facing is within WeaponAimTolerance
            //radians of facing the target
            //TODO: should be a parameter (different than BotAimAccuracy??)
            const float weaponAimTolerance = 0.01f; //2 degs approx

            if (angle < weaponAimTolerance)
            {
                Facing = toTarget;
                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
            //facing vector accordingly
            Matrix2 rotationMatrix = new Matrix2();

            //notice how the direction of rotation has to be determined when
            //creating the rotation matrix
            rotationMatrix.Rotate(angle*Vector2Util.Sign(Facing, toTarget));
            Facing = rotationMatrix.TransformVector2(Facing);

            return false;
        }
예제 #3
0
        ///<summary>
        ///rotates a vector <paramref name="angle"/> radians around the origin
        ///</summary>
        ///<param name="v">vector to rotate around the origin</param>
        ///<param name="angle">rotation angle in radians</param>
        public static void RotateVectorAroundOrigin(ref Vector2 v, float angle)
        {
            //create a transformation matrix
            Matrix2 transformationMatrix = new Matrix2();

            //rotate
            transformationMatrix.Rotate(angle);

            //now transform the object's vertices
            transformationMatrix.TransformVector2(ref v);
        }
예제 #4
0
        ///<summary>
        ///given a list of 2D vectors, a position and  orientation, this
        ///function transforms the 2D vectors into the object's world space
        ///</summary>
        ///<param name="points"></param>
        ///<param name="position"></param>
        ///<param name="forward"></param>
        ///<param name="side"></param>
        ///<returns>list of transformed points</returns>
        public static List<Vector2> WorldTransform(
            List<Vector2> points,
            Vector2 position,
            Vector2 forward,
            Vector2 side)
        {
            //copy the original points into the list about to be transformed
            List<Vector2> transformedPoints = new List<Vector2>(points);

            //create a transformation matrix
            Matrix2 transformationMatrix = new Matrix2();

            //rotate
            transformationMatrix.Rotate(forward, side);

            //and translate
            transformationMatrix.Translate(position.X, position.Y);

            //now transform the points
            transformationMatrix.TransformVector2(transformedPoints);

            return transformedPoints;
        }
예제 #5
0
 ///<summary>
 ///rotates a vector <paramref name="angle"/> radians around the origin
 ///</summary>
 ///<param name="v">vector to rotate around the origin</param>
 ///<param name="p"></param>
 ///<param name="angle">rotation angle in radians</param>
 ///<returns>the rotated vector</returns>
 public static void RotateVectorAroundPoint(ref Vector2 v, Vector2 p, float angle)
 {
     //create a transformation matrix
     Matrix2 transformationMatrix = new Matrix2();
     transformationMatrix.Translate(-p.X, -p.Y);
     transformationMatrix.Rotate(angle);
     transformationMatrix.Translate(p.X, p.Y);
     //now transform the object's vertices
     transformationMatrix.TransformVector2(ref v);
 }