/// <summary> /// Check if the bounding rectangle intersects this bounding rectangle. /// </summary> /// <param name="otherRect"></param> /// <returns></returns> public bool Intersects( BoundingRect otherRect ) { // Generate the potential seperating axis vectors between our bounding rect // and the provided rect. We avoid the use of arrays here so we can avoid // garbage collection Vector2 v0 = UpperRight - UpperLeft; Vector2 v1 = UpperRight - LowerRight; Vector2 v2 = otherRect.UpperLeft - otherRect.LowerLeft; Vector2 v3 = otherRect.UpperLeft - otherRect.UpperRight; return ( IsAxisCollision( otherRect, v0 ) && IsAxisCollision( otherRect, v1 ) && IsAxisCollision( otherRect, v2 ) && IsAxisCollision( otherRect, v3 ) ); }
private bool IsAxisCollision( BoundingRect otherRect, Vector2 axis ) { // Project the four corners of the bounding box we are checking onto the axis, // and get a scalar value of that projection for comparison. Vector2 axisNormalized = Vector2.Normalize( axis ); float a0 = Vector2.Dot( otherRect.UpperLeft, axisNormalized ); float a1 = Vector2.Dot( otherRect.UpperRight, axisNormalized ); float a2 = Vector2.Dot( otherRect.LowerLeft, axisNormalized ); float a3 = Vector2.Dot( otherRect.LowerRight, axisNormalized ); // Project the four corners of our bounding rect onto the requested axis, and // get scalar values for those projections float b0 = Vector2.Dot( UpperLeft, axisNormalized ); float b1 = Vector2.Dot( UpperRight, axisNormalized ); float b2 = Vector2.Dot( LowerLeft, axisNormalized ); float b3 = Vector2.Dot( LowerRight, axisNormalized ); // Get the maximum and minimum scalar values for each of the rectangles float aMin = Math.Min( Math.Min( a0, a1 ), Math.Min( a2, a3 ) ); float aMax = Math.Max( Math.Min( a0, a1 ), Math.Max( a2, a3 ) ); float bMin = Math.Min( Math.Min( b0, b1 ), Math.Min( b2, b3 ) ); float bMax = Math.Max( Math.Min( b0, b1 ), Math.Max( b2, b3 ) ); // Test if there is an overlap between the minimum of a and the maximum // values of the two rectangles return ( bMin <= aMax && bMax >= aMax ) || ( aMin <= bMax && aMax >= bMax ); }
/// <summary> /// Draws a hit box for the game /// </summary> private void DrawHitBox( GameTime gameTime ) { double currentTime = gameTime.TotalGameTime.TotalSeconds; double startedAt = mTimeStarted.TotalSeconds + WAIT_TIME; double finishedAt = startedAt + ACTION_TIME - WAIT_TIME; double weightedAmount = Utils.FindOneZeroWeighting( currentTime, startedAt, finishedAt ); float angleDeg = MathHelper.Lerp( 170.0f, 10.0f, (float) weightedAmount ); float radians = MathHelper.ToRadians( angleDeg ); Vector2 actorPosition = mGameObject.Position; Vector2 weaponOffset = new Vector2( 32, 32 ); Vector2 weaponSize = new Vector2( 55, 10 ); Vector2 weaponPivot = new Vector2( 0, 5 ); Vector2 weaponWorldPos = actorPosition + weaponOffset; Rectangle weaponRect = new Rectangle( (int) weaponWorldPos.X, (int) weaponWorldPos.Y, (int) weaponSize.X, (int) weaponSize.Y ); BoundingRect bounds = new BoundingRect( weaponRect, radians, weaponPivot + weaponWorldPos ); // new Vector2( 0.0f, 10.0f / 2.0f ) ); GameRoot.Debug.DrawBoundingBox( bounds, Color.HotPink ); // Test all the other skeletons out there List<GameObject> collisions = new List<GameObject>(); for ( int i = 0; i < GameRoot.Enemies.Count; ++i ) { GameObject obj = GameRoot.Enemies[i]; Vector2 pos = obj.Position; Rectangle staticInnerRect = new Rectangle( (int) pos.X, (int) pos.Y, 64, 64 ); BoundingRect staticRect = new BoundingRect( staticInnerRect ); GameRoot.Debug.DrawBoundingBox( staticRect, Color.PowderBlue ); // lets see what happens if ( bounds.Intersects( staticRect ) ) { collisions.Add( obj ); GameRoot.Debug.DrawFilledRect( staticInnerRect, Color.White ); } } // Delete the other colliding game objects for ( int i = 0; i < collisions.Count; ++i ) { GameRoot.Enemies.Remove( collisions[i] ); } }