/// <summary> /// Stop pill from falling when it collides with an AABB /// </summary> /// <param name="other"></param> public void LifePillCollision(PetzakAABB other) { if (lifePill.CollidesWith(other)) { pill.isFalling = false; Vector3 fix = lifePill.FindFix(other); lifePill.BroadcastMessage("ApplyFix", fix); } }
/// <summary> /// This function returns how far to move this AABB so that it no longer overlaps /// another AABB. This function assumes that the two are overlapping. /// This function only solves for the X or Y axies. Z is ignored. /// </summary> /// <param name="other"></param> /// <returns>How far to move this box (in meters).</returns> public Vector3 FindFix(PetzakAABB other) { float moveRight = other.max.x - this.min.x; float moveLeft = other.min.x - this.max.x; float moveUp = other.max.y - this.min.y; float moveDown = other.min.y - this.max.y; Vector3 fix = new Vector3(); fix.x = (Mathf.Abs(moveLeft) < Mathf.Abs(moveRight)) ? moveLeft : moveRight; fix.y = (Mathf.Abs(moveUp) < Mathf.Abs(moveDown)) ? moveUp : moveDown; if (Mathf.Abs(fix.x) < Mathf.Abs(fix.y)) { fix.y = 0; } else { fix.x = 0; } return(fix); }
/// <summary> /// Determines if there are any gaps between one AABB and another. /// Only accounts for x and y axis. /// </summary> /// <param name="other"></param> /// <returns></returns> public bool CollidesWith(PetzakAABB other) { // LightPlatform collision against player if (other.name.StartsWith("Light") && !this.name.StartsWith("Life")) { Recalc(); // make sure both objects have the correct min/max other.Recalc(); PetzakPlayerMovement p = this.GetComponentInParent <PetzakPlayerMovement>(); bool closeEnough = Math.Abs(other.max.y - this.min.y) < .2; // objects are very close to one another bool withinHorizontalRange = other.min.x <= this.max.x && other.max.x >= this.min.x; bool isNotMovingUp = p.velocity.y <= 0; return(closeEnough && withinHorizontalRange && isNotMovingUp && !p.isJumping && !p.isGrounded); } if (other.max.x < this.min.x || // check for gap to left other.min.x > this.max.x || // check for gap to right other.min.y > this.max.y || // check for gap above other.max.y < this.min.y) // check for gap below { return(false); } return(true); // no gaps found }