Ejemplo n.º 1
0
        public bool ClipRay(
            Point2d rayOrigin,
            Vector2d rayDirection,
            out float clipMinT,
            out float clipMaxT)
        {
            bool intersects = false;
            Vector2d tMin = new Vector2d();
            Vector2d tMax = new Vector2d();

            // Compute the ray intersection times along the x-axis
            if (rayDirection.i > MathConstants.EPSILON)
            {
                // ray has positive x component
                tMin.i = (m_pMin.x - rayOrigin.x) / rayDirection.i;
                tMax.i = (m_pMax.x - rayOrigin.x) / rayDirection.i;
            }
            else if (rayDirection.i < -MathConstants.EPSILON)
            {
                // ray has negative x component
                tMin.i = (m_pMax.x - rayOrigin.x) / rayDirection.i;
                tMax.i = (m_pMin.x - rayOrigin.x) / rayDirection.i;
            }
            else
            {
                // Ray has no x component (parallel to x-axis)
                tMin.i = MathConstants.REAL_MIN;
                tMax.i = MathConstants.REAL_MAX;
            }

            // Compute the ray intersection times along the y-axis
            if (rayDirection.j > MathConstants.EPSILON)
            {
                // ray has positive y component
                tMin.j = (m_pMin.y - rayOrigin.y) / rayDirection.j;
                tMax.j = (m_pMax.y - rayOrigin.y) / rayDirection.j;
            }
            else if (rayDirection.j < -MathConstants.EPSILON)
            {
                // ray has negative y component
                tMin.j = (m_pMax.y - rayOrigin.y) / rayDirection.j;
                tMax.j = (m_pMin.y - rayOrigin.y) / rayDirection.j;
            }
            else
            {
                // Ray has no y component (parallel to y-axis)
                tMin.j = MathConstants.REAL_MIN;
                tMax.j = MathConstants.REAL_MAX;
            }

            // Ray only intersects AABB if minT is before maxT and maxT is non-negative
            clipMinT = tMin.MaxComponent();
            clipMaxT = tMax.MinComponent();
            intersects = clipMinT < clipMaxT && clipMaxT >= 0;

            return intersects;
        }
Ejemplo n.º 2
0
        public static eDirection GetDirectionForVector(Vector2d vector)
        {
            eDirection direction = eDirection.none;

            if (vector.MagnitudeSquared() > EPSILON_SQUARED)
            {
                if (vector.i > 0)
                {
                    if (vector.j > vector.i)
                    {
                        direction = eDirection.down;
                    }
                    else if (vector.j < -vector.i)
                    {
                        direction = eDirection.up;
                    }
                    else
                    {
                        direction = eDirection.right;
                    }
                }
                else
                {
                    if (vector.j > -vector.i)
                    {
                        direction = eDirection.down;
                    }
                    else if (vector.j < vector.i)
                    {
                        direction = eDirection.up;
                    }
                    else
                    {
                        direction = eDirection.left;
                    }
                }
            }

            return direction;
        }
Ejemplo n.º 3
0
 public static float GetAngleForVector(Vector2d v)
 {
     // (1, 0) -> 0 degrees
     // (0, 1) -> 90 degrees
     // (-1, 0) -> 180 degrees
     // (0, -1) -> 270 degrees
     return (((float)Math.Atan2(v.j, v.i) + MathConstants.TWO_PI) % MathConstants.TWO_PI) * RADIANS_TO_DEGREES;
 }
Ejemplo n.º 4
0
 public float Dot(Vector2d v)
 {
     return i * v.i + j * v.j;
 }
Ejemplo n.º 5
0
 public float Cross(Vector2d v)
 {
     return i * v.j - v.i * j;
 }
Ejemplo n.º 6
0
 public void Copy(Vector2d v)
 {
     this.i = v.i;
     this.j = v.j;
 }
Ejemplo n.º 7
0
 public Vector2d(Vector2d v)
 {
     this.i = v.i;
     this.j = v.j;
 }
Ejemplo n.º 8
0
        public float NormalizeWithDefault(Vector2d defaultVector)
        {
            float length = Magnitude();

            if (length > MathConstants.EPSILON)
            {
                ScaleBy(1.0f / length);
            }
            else
            {
                Copy(defaultVector);
            }

            return length;
        }
Ejemplo n.º 9
0
 public Point2d Offset(Vector2d v)
 {
     return new Point2d(x + v.i, y + v.j);
 }
Ejemplo n.º 10
0
 public void Move(Vector2d v)
 {
     m_pMin += v;
     m_pMax += v;
 }