/// <summary> /// Checks to see if an animation is on screen. O(1) complexity, though not very efficient: /// checks 9 points in and around the animation to see if any of them are on screen /// </summary> /// <param name="anim">The animation to check</param> /// <returns>whether the animation is on screen</returns> private bool checkOnScreen(Animation anim) { Vector2 bb = (anim.position - camera.getRefPoint()) * camera.getScale() + new Vector2(w / 2, h / 2); if (!checkNearby(bb)) return false; Vector2 aa = (anim.position - camera.getRefPoint() + new Vector2(anim.getWidth() / 2, anim.getHeight() / 2)) * camera.getScale() + new Vector2(w / 2, h / 2); Vector2 ab = (anim.position - camera.getRefPoint() + new Vector2(0, anim.getHeight() / 2)) * camera.getScale() + new Vector2(w / 2, h / 2); Vector2 ac = (anim.position - camera.getRefPoint() + new Vector2(-anim.getWidth() / 2, anim.getHeight())) * camera.getScale() + new Vector2(w / 2, h / 2); Vector2 ba = (anim.position - camera.getRefPoint() + new Vector2(anim.getWidth() / 2, 0)) * camera.getScale() + new Vector2(w / 2, h / 2); Vector2 bc = (anim.position - camera.getRefPoint() + new Vector2(-anim.getWidth() / 2, 0)) * camera.getScale() + new Vector2(w / 2, h / 2); Vector2 ca = (anim.position - camera.getRefPoint() + new Vector2(anim.getWidth() / 2, -anim.getHeight() / 2)) * camera.getScale() + new Vector2(w / 2, h / 2); Vector2 cb = (anim.position - camera.getRefPoint() + new Vector2(0, -anim.getHeight() / 2)) * camera.getScale() + new Vector2(w / 2, h / 2); Vector2 cc = (anim.position - camera.getRefPoint() + new Vector2(-anim.getWidth() / 2, -anim.getHeight() / 2)) * camera.getScale() + new Vector2(w / 2, h / 2); if (checkOnScreen(aa) || checkOnScreen(ab) || checkOnScreen(ac) || checkOnScreen(ba) || checkOnScreen(bb) || checkOnScreen(bc) || checkOnScreen(ca) || checkOnScreen(cb) || checkOnScreen(cc)) return true; return false; }
/// <summary> /// Rudimentary collision detection between two animation /// </summary> /// <param name="other">The animation to check this one against</param> /// <returns>whether the two animation hit</returns> public bool checkHit(Animation other) { if (other == null) return false; float top1 = position.Y - getHeight() / 2, top2 = other.position.Y - other.getHeight() / 2; float bot1 = position.Y + getHeight() / 2, bot2 = other.position.Y + other.getHeight() / 2; float l1 = position.X - getWidth() / 2, l2 = other.position.X - other.getWidth() / 2; float r1 = position.X + getWidth() / 2, r2 = other.position.X + other.getWidth() / 2; if (top1 < bot2 && bot1 > top2 && l1 < r2 && r1 > l2) return true; return false; }