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; }
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; }
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; }
public float Dot(Vector2d v) { return i * v.i + j * v.j; }
public float Cross(Vector2d v) { return i * v.j - v.i * j; }
public void Copy(Vector2d v) { this.i = v.i; this.j = v.j; }
public Vector2d(Vector2d v) { this.i = v.i; this.j = v.j; }
public float NormalizeWithDefault(Vector2d defaultVector) { float length = Magnitude(); if (length > MathConstants.EPSILON) { ScaleBy(1.0f / length); } else { Copy(defaultVector); } return length; }
public Point2d Offset(Vector2d v) { return new Point2d(x + v.i, y + v.j); }
public void Move(Vector2d v) { m_pMin += v; m_pMax += v; }