internal static void Draw(XNACS1Primitive p, XNACS1LibDrawHelper dh) { OBB2D a = new OBB2D(p); dh.DrawLineSegments(a.m_Corner[0], a.m_Corner[1]); dh.DrawLineSegments(a.m_Corner[1], a.m_Corner[2]); dh.DrawLineSegments(a.m_Corner[2], a.m_Corner[3]); dh.DrawLineSegments(a.m_Corner[3], a.m_Corner[0]); }
internal static bool OBB2DIntersect(XNACS1Primitive a, XNACS1Primitive b, Vector2 pos) { OBB2D aOBB = new OBB2D(a); OBB2D bOBB = new OBB2D(b); bool touched = aOBB.overlaps1Way(bOBB) && bOBB.overlaps1Way(aOBB); if (touched) { pos = a.Center + b.Center; pos /= 2.0f; } return touched; }
internal static bool OBB2DIntersect(XNACS1Primitive a, XNACS1Primitive b, out Vector2 pos) { pos = Vector2.Zero; OBB2D aOBB = new OBB2D(a); OBB2D bOBB = new OBB2D(b); bool touched = aOBB.overlaps1Way(bOBB) && bOBB.overlaps1Way(aOBB); if (touched) { pos = a.Center + b.Center; pos /= 2.0f; } return(touched); }
/// <summary> /// Determines if this primitive collides with the otherPrimitive. If true, pos is the colliding position. /// </summary> /// <param name="otherPrimitive">the other primitive.</param> /// <param name="pos">If two primitives collide, this is the "average" of all the possible colliding positions.</param> /// <returns>True: if the two primitives has collided. Pos is the approximated colliding position. /// False: if the two primitives do not collide, (Pos is undefined in this case). /// </returns> public bool Collided(XNACS1Primitive otherPrimitive, out Vector2 pos) { if ((m_Type == PrimitiveType.PrimitiveCircle) && (otherPrimitive.m_Type == PrimitiveType.PrimitiveCircle)) { return(CircleCircle(m_Center, SizeX * 0.5f, otherPrimitive.Center, otherPrimitive.SizeX * 0.5f, out pos)); } else { if ((m_Rotate == 0.0f) && (otherPrimitive.m_Rotate == 0.0f)) { return(RecRec(MinBound, MaxBound, otherPrimitive.MinBound, otherPrimitive.MaxBound, out pos)); } else { return(OBB2D.OBB2DIntersect(this, otherPrimitive, out pos)); } } }
private double[] m_Origin; // origin[a] = corner[0].dot(axis[a]); /** Returns true if other overlaps one dimension of this. */ private bool overlaps1Way(OBB2D other) { for (int a = 0; a < 2; ++a) { double t = Vector2.Dot(m_Axis[a], other.m_Corner[0]); // Find the extent of box 2 on axis a double tMin = t; double tMax = t; for (int c = 1; c < 4; ++c) { t = Vector2.Dot(m_Axis[a], other.m_Corner[c]); if (t < tMin) { tMin = t; } else if (t > tMax) { tMax = t; } } // We have to subtract off the origin // See if [tMin, tMax] intersects [0, 1] if ((tMin > 1.0 + m_Origin[a]) || (tMax < m_Origin[a])) { // There was no intersection along this dimension; // the boxes cannot possibly overlap. return(false); } } // There was no dimension along which there is no intersection. // Therefore the boxes overlap. return(true); }
/** Returns true if other overlaps one dimension of this. */ private bool overlaps1Way(OBB2D other) { for (int a = 0; a < 2; ++a) { double t = Vector2.Dot(m_Axis[a], other.m_Corner[0]); // Find the extent of box 2 on axis a double tMin = t; double tMax = t; for (int c = 1; c < 4; ++c) { t = Vector2.Dot(m_Axis[a], other.m_Corner[c]); if (t < tMin) { tMin = t; } else if (t > tMax) { tMax = t; } } // We have to subtract off the origin // See if [tMin, tMax] intersects [0, 1] if ((tMin > 1.0 + m_Origin[a]) || (tMax < m_Origin[a])) { // There was no intersection along this dimension; // the boxes cannot possibly overlap. return false; } } // There was no dimension along which there is no intersection. // Therefore the boxes overlap. return true; }