Calculator extension

Topics: User Forum
Developer
Jan 26, 2008 at 7:09 PM
Edited Jan 26, 2008 at 7:11 PM
Hi there,

I've extended the Engine a bit. In this post I'm going to show you my little addition to the calculator. Maybe you can use this, maybe you can add this (or something like this) to the Engine or maybe you'll detect errors or some slow parts and tell me what I could do better.

Here is a list of the functions:
  • public static float VectorToRadians(Vector2 vector)
  • public static Vector2 RadiansToVector(float radians)
  • public static void RadiansToVector(float radians, ref Vector2 vector)
  • public static void RotateVector(ref Vector2 vector, float radians)
  • public static Vector2 LinearBezierCurve(Vector2 start, Vector2 end, float t)
  • public static Vector2 QuadraticBezierCurve(Vector2 start, Vector2 curve, Vector2 end, float t)
  • public static Vector2 QuadraticBezierCurve(Vector2 start, Vector2 curve, Vector2 end, float t, ref float radians)
  • public static Vector2 CubicBezierCurve2(Vector2 start, Vector2 startPoitsTo, Vector2 end, Vector2 endPointsTo, float t)
  • public static Vector2 CubicBezierCurve2(Vector2 start, Vector2 startPoitsTo, Vector2 end, Vector2 endPointsTo, float t, ref float radians)
  • public static Vector2 CubicBezierCurve2(Vector2 start, float startPointDirection, float startPointLength, Vector2 end, float endPointDirection, float endPointLength, float t, ref float radians)
  • public static Vector2 CubicBezierCurve(Vector2 start, Vector2 curve1, Vector2 curve2, Vector2 end, float t)
  • public static Vector2 CubicBezierCurve(Vector2 start, Vector2 curve1, Vector2 curve2, Vector2 end, float t, ref float radians)
  • public static Vector2 InterpolateNormal(Vector2 vector1, Vector2 vector2, float t)
  • public static void InterpolateNormal(Vector2 vector1, Vector2 vector2, float t, out Vector2 vector)
  • public static void InterpolateNormal(ref Vector2 vector1, Vector2 vector2, float t)
  • public static float InterpolateRotation(float radians1, float radians2, float t)

The last function seems strange huh? Interpolate two floats? Well yes ... But I transform them into vectors, interpolate and transform into floats again. I do this because I have to. In my game the camera is rotate able. Now, where is the problem you might think... It's easy explained. If you pass the TotalRotation of the Body to rotate the camera with the Body you pass a high value and the cam spins around a lot till it gets the right direction. If you use the clamped rotation of the body it causes wrong behavior again. Imagine rotating into the positive direction and the rotation gets clamped into zero or a negative value. It suddenly spins into the other direction a lot. So, that's why I created this function.

Uhm yeah. Have fun with this or give me some feedback? Neither? Do whatever you want :).

        /// Temp variables to speed up the code.
        private static float _tPow2;
        private static float _wayToGo;
        private static float _wayToGoPow2;
 
        private static Vector2 _startCurve;
        private static Vector2 _curveEnd;
        private static Vector2 _temp;
 
        public static float VectorToRadians(Vector2 vector)
        {
            return (float)Math.Atan2((double)vector.X, -(double)vector.Y);
        }
 
        public static Vector2 RadiansToVector(float radians)
        {
            return new Vector2((float)Math.Sin((double)radians), -(float)Math.Cos((double)radians));
        }
 
        public static void RadiansToVector(float radians, ref Vector2 vector)
        {
            vector.X = (float)Math.Sin((double)radians);
            vector.Y = -(float)Math.Cos((double)radians);
        }
 
        public static void RotateVector(ref Vector2 vector, float radians)
        {
            float length = vector.Length();
            float newRadians = (float)Math.Atan2((double)vector.X, -(double)vector.Y) + radians;
 
            vector.X = (float)Math.Sin((double)newRadians) * length;
            vector.Y = -(float)Math.Cos((double)newRadians) * length;
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="t">Value between 0.0f and 1.0f.</param>
        /// <returns></returns>
        public static Vector2 LinearBezierCurve(Vector2 start, Vector2 end, float t)
        {
            return start + (end - start) * t;
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="start"></param>
        /// <param name="curve"></param>
        /// <param name="end"></param>
        /// <param name="t">Value between 0.0f and 1.0f.</param>
        /// <returns></returns>
        public static Vector2 QuadraticBezierCurve(Vector2 start, Vector2 curve, Vector2 end, float t)
        {
            _wayToGo = 1.0f - t;
 
            return _wayToGo * _wayToGo * start
                   + 2.0f * t * _wayToGo * curve
                   + t * t * end;
        }
 
        public static Vector2 QuadraticBezierCurve(Vector2 start, Vector2 curve, Vector2 end, float t, ref float radians)
        {
            _startCurve = start + (curve - start) * t;
            _curveEnd = curve + (end - curve) * t;
            _temp = _curveEnd - _startCurve;
 
            radians = (float)Math.Atan2((double)_temp.X, -(double)_temp.Y);
            return _startCurve + _temp * t;
        }
 
        public static Vector2 CubicBezierCurve2(Vector2 start, Vector2 startPoitsTo, Vector2 end, Vector2 endPointsTo, float t)
        {
            return CubicBezierCurve(start, start + startPoitsTo, end + endPointsTo, end, t);
        }
 
        public static Vector2 CubicBezierCurve2(Vector2 start, Vector2 startPoitsTo, Vector2 end, Vector2 endPointsTo, float t, ref float radians)
        {
            return CubicBezierCurve(start, start + startPoitsTo, end + endPointsTo, end, t, ref radians);
        }
 
        public static Vector2 CubicBezierCurve2(Vector2 start, float startPointDirection, float startPointLength,
                                                Vector2 end, float endPointDirection, float endPointLength,
                                                float t, ref float radians)
        {
            return CubicBezierCurve(start,
                                    Calculator.RadiansToVector(startPointDirection) * startPointLength,
                                    Calculator.RadiansToVector(endPointDirection) * endPointLength,
                                    end,
                                    t,
                                    ref radians);
        }
 
        public static Vector2 CubicBezierCurve(Vector2 start, Vector2 curve1, Vector2 curve2, Vector2 end, float t)
        {
            _tPow2 = t * t;
            _wayToGo = 1.0f - t;
            _wayToGoPow2 = _wayToGo * _wayToGo;
 
            return _wayToGo * _wayToGoPow2 * start
                   + 3.0f * t * _wayToGoPow2 * curve1
                   + 3.0f * _tPow2 * _wayToGo * curve2
                   + t * _tPow2 * end;
        }
 
        public static Vector2 CubicBezierCurve(Vector2 start, Vector2 curve1, Vector2 curve2, Vector2 end, float t, ref float radians)
        {
            return QuadraticBezierCurve(start + (curve1 - start) * t,
                                        curve1 + (curve2 - curve1) * t,
                                        curve2 + (end - curve2) * t,
                                        t,
                                        ref radians);
        }
 
        //Interpolate normal vectors ...
        public static Vector2 InterpolateNormal(Vector2 vector1, Vector2 vector2, float t)
        {
            vector1 += (vector2 - vector1) * t;
            vector1.Normalize();
 
            return vector1;
        }
 
        public static void InterpolateNormal(Vector2 vector1, Vector2 vector2, float t, out Vector2 vector)
        {
            vector = vector1 + (vector2 - vector1) * t;
            vector.Normalize();
        }
 
        public static void InterpolateNormal(ref Vector2 vector1, Vector2 vector2, float t)
        {
            vector1 += (vector2 - vector1) * t;
            vector1.Normalize();
        }
 
        public static float InterpolateRotation(float radians1, float radians2, float t)
        {
            Vector2 vector1 = new Vector2((float)Math.Sin((double)radians1), -(float)Math.Cos((double)radians1));
            Vector2 vector2 = new Vector2((float)Math.Sin((double)radians2), -(float)Math.Cos((double)radians2));
 
            vector1 += (vector2 - vector1) * t;
            vector1.Normalize();
 
            return (float)Math.Atan2((double)vector1.X, -(double)vector1.Y);
        }
Coordinator
Jan 28, 2008 at 5:29 PM
Thank, looks useful. I'll add it for next release.
Developer
Jan 28, 2008 at 6:46 PM
Awesome :).
Thanks ^^ ...
Coordinator
Feb 14, 2008 at 11:05 AM
I forgot to add these in the latest release. I've just added them and they will be in the next release.