using System; using System.Diagnostics; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace BouncyCatIsBouncy.Debugging { static class OBBRenderer { static LineBrush s_brush; static bool s_initialized; static private void Init(GraphicsDevice graphics) { s_brush = new LineBrush(2, Color.Red); s_brush.Load(graphics); s_initialized = true; } [Conditional("DEBUG")] static public void Draw(SpriteBatch spriteBatch, OBB boundingBox) { Debug.Assert(spriteBatch != null); if (!s_initialized) Init(spriteBatch.GraphicsDevice); DrawCenter(spriteBatch, ref boundingBox); DrawSides(spriteBatch, ref boundingBox); } static private Vector2 PolarToXY(float rotation) { var sin = (float)Math.Sin(rotation); var cos = (float)Math.Cos(rotation); return new Vector2(sin, cos); } static private void DrawCenter(SpriteBatch spriteBatch, ref OBB boundingBox) { const float LENGTH = 10.0f; var xAxis = PolarToXY(boundingBox.Rotation) * LENGTH; var yAxis = PolarToXY(boundingBox.Rotation + (float)Math.PI / 2.0f) * LENGTH; s_brush.Draw(spriteBatch, boundingBox.Center + xAxis, boundingBox.Center - xAxis); s_brush.Draw(spriteBatch, boundingBox.Center + yAxis, boundingBox.Center - yAxis); } static private Vector2 RotateVector(Vector2 position, float radians) { var sin = (float)Math.Sin(-radians); var cos = (float)Math.Cos(-radians); return new Vector2(position.X * cos + position.Y * -sin , position.X * sin + position.Y * cos); } static private void DrawSides(SpriteBatch spriteBatch, ref OBB boundingBox) { var halfWidth = boundingBox.Width / 2.0f; var halfHeight = boundingBox.Height / 2.0f; var rot = boundingBox.Rotation; var upperRight = RotateVector(new Vector2(halfWidth, -halfHeight), rot); var upperLeft = RotateVector(new Vector2(-halfWidth, -halfHeight), rot); var lowerLeft = RotateVector(new Vector2(-halfWidth, halfHeight), rot); var lowerRight = RotateVector(new Vector2(halfWidth, halfHeight), rot); s_brush.Draw(spriteBatch, boundingBox.Center + upperRight, boundingBox.Center + upperLeft); s_brush.Draw(spriteBatch, boundingBox.Center + upperLeft, boundingBox.Center + lowerLeft); s_brush.Draw(spriteBatch, boundingBox.Center + lowerLeft, boundingBox.Center + lowerRight); s_brush.Draw(spriteBatch, boundingBox.Center + lowerRight, boundingBox.Center + upperRight); } } }