private static Collission DetectCollission(List <vec4> rects) { for (var iCurrentRect = 0; iCurrentRect < rects.Count; ++iCurrentRect) { for (var iOtherRect = 0; iOtherRect < rects.Count; ++iOtherRect) { if (iCurrentRect == iOtherRect) { continue; } var currRect = rects[iCurrentRect]; var otherRect = rects[iOtherRect]; if (HitTests.HitTest(ref currRect, ref otherRect) != HitTestResult.Apart) { return new Collission { Happened = true, A = iCurrentRect, B = iOtherRect } } ; } } return(new Collission()); } }
private static void Validate(List <vec4> rects) { for (var iCurrentRect = 0; iCurrentRect < rects.Count; ++iCurrentRect) { for (var iOtherRect = 0; iOtherRect < rects.Count; ++iOtherRect) { if (iCurrentRect == iOtherRect) { continue; } var currRect = rects[iCurrentRect]; var otherRect = rects[iOtherRect]; if (HitTests.HitTest(ref currRect, ref otherRect) != HitTestResult.Apart) { throw new Exception("collision"); } } } }
public void HitTest() { vec4 rect0 = new vec4(0, 0, 1, 1); vec4 rect1 = new vec4(-1, 0, 1.5f, 1); var overlap0 = HitTests.HitTest(rect0, rect1) != HitTestResult.Apart; Assert.IsTrue(overlap0); rect0 = new vec4(5, 0, 1, 1); rect1 = new vec4(-1, 0, 1.5f, 1); var overlap1 = HitTests.HitTest(rect0, rect1) != HitTestResult.Apart; Assert.IsFalse(overlap1); rect0 = new vec4(0, 0, 1, 1); rect1 = new vec4(1, 0, 1, 1); var overlap2 = HitTests.HitTest(rect0, rect1) != HitTestResult.Apart; Assert.IsFalse(overlap2); }
protected virtual void OnRedraw(OpenGL gl) { gl.ClearColor(1, 1, 1, 1); gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); gl.MatrixMode(GL_PROJECTION); gl.LoadIdentity(); gl.Ortho(0, Width, Height, 0, -1, 1); gl.MatrixMode(GL_MODELVIEW); gl.LoadIdentity(); using (spriteBatch.BeginScope()) { float borderThickness = 1; var iLoCol = 0; var iHiCol = 0; var iLoRow = 0; var iHiRow = 0; //DrawColumns(borderThickness, ref iLoCol, ref iHiCol); //DrawRows(borderThickness, ref iLoRow, ref iHiRow); //for (var iCol = iLoCol; iCol < iHiCol; iCol++) // for (var iRow = iLoRow; iRow < iHiRow; iRow++) // { // var offsetX = Columns[iCol]._OffsetX; // var offsetY = Rows[iRow]._OffsetY; // spriteBatch.Text((float)Math.Round(offsetX - (float)HOffset + 15), (float)Math.Round(offsetY - (float)VOffset), names[(iCol + iRow) % names.Length], font, TextColor, .5f, .5f); // } //for (var iCol = iLoCol; iCol < iHiCol; iCol++) // for (var iRow = iLoRow; iRow < iHiRow; iRow++) // { // var offsetX = Columns[iCol]._OffsetX; // var offsetY = Rows[iRow]._OffsetY; // spriteBatch.Rect(offsetX - (float)HOffset + 3, offsetY - (float)VOffset + 3, 9, 9, colors[(iCol + iRow) % colors.Length]); // spriteBatch.Rect(offsetX - (float)HOffset + 2, offsetY - (float)VOffset + 2, 11, 11, BorderColor, -.1f); // } //spriteBatch.Triangle( // new vec2(-1.0f, -1.0f), // new vec2(1.0f, -1.0f), // new vec2(1.0f, 1.0f), // Color.Chocolate, 1.0f); var n = .39f; //var rects = new[] { new vec2(32, 146), new vec2(71, 24), new vec2(55, 132), new vec2(77, 38), new vec2(43, 79) }; // reverse, longer into y //for (var i = 0; i < rects.Length; ++i) //{ // ref var rect = ref rects[i]; // if (rect.x > rect.y) // { // var oldX = rect.x; // rect.x = rect.y; // rect.y = rect.x; // } //} var maxHeight = rectSizes.Max(z => z.y); var sumWidth = rectSizes.Sum(z => z.x); Array.Sort(rectSizes, new Comparison <vec2>((a, b) => (int)(b.x * b.y - a.x * a.y))); var spaces = new List <vec4>(); spaces.Add(new vec4(rectSizes[0].x, 0, 9999f, 9999f)); spaces.Add(new vec4(0, rectSizes[0].y, 9999f, 9999f)); var rects = new List <vec4> { new vec4(0, 0, rectSizes[0].x, rectSizes[0].y) }; var width = Math.Max(rectSizes[0].x, rectSizes[0].y); for (var i = 1; i < rectSizes.Length; ++i) { // go through all rectangles and place them somewhere var rectSize = rectSizes[i]; var spaceFoundIndex = -1; vec4 foundSpace = new vec4(0, 0, 9999f, 9999f); for (var iSpace = 0; iSpace < spaces.Count; ++iSpace) { var space = spaces[iSpace]; if (space.z >= rectSize.x && space.w >= rectSize.y) { // would fit if (spaceFoundIndex == -1) { spaceFoundIndex = iSpace; foundSpace = space; } else { if (space.x + rectSize.x < width && space.y + rectSize.y < width) { //if (space.x + rectSize.x + space.y + rectSize.y < foundSpace.x + rectSize.x + foundSpace.y + rectSize.y) //{ spaceFoundIndex = iSpace; foundSpace = space; //} } } } } if (spaceFoundIndex == -1) { throw new Exception("not found"); } var newSpace0 = new vec4(foundSpace.x + rectSize.x, foundSpace.y, 9999f, 9999f); spaces[spaceFoundIndex] = newSpace0; spaces.Add(new vec4(foundSpace.x, foundSpace.y + rectSize.y, 9999f, 9999f)); var newRect = new vec4(foundSpace.x, foundSpace.y, rectSize.x, rectSize.y); rects.Add(newRect); width = Math.Max(width, Math.Max(newRect.x + newRect.z, newRect.y + newRect.w)); for (var iSpace = 0; iSpace < spaces.Count; ++iSpace) { var space = spaces[iSpace]; var hitTestX = HitTests.HitTest(newRect.x, newRect.x + newRect.z, space.x, space.x + space.z); var hitTestY = HitTests.HitTest(newRect.y, newRect.y + newRect.w, space.y, space.y + space.w); if (hitTestX != HitTestResult.Apart && hitTestY != HitTestResult.Apart) { if (hitTestX == HitTestResult.B_Contains_A) { space.z = newRect.x - space.x; continue; } if (hitTestY == HitTestResult.B_Contains_A) { space.w = newRect.y - space.y; continue; } if (hitTestX == HitTestResult.A_Intersects_B) { space.z = space.z - (newRect.x + newRect.z) - space.x; space.x = newRect.x + newRect.z; continue; } if (hitTestY == HitTestResult.A_Intersects_B) { space.w = space.w - (newRect.y + newRect.w) - space.y; space.y = newRect.y + newRect.w; continue; } } } } Validate(rects); for (var i = 0; i < rects.Count; ++i) { var rect = rects[i]; spriteBatch.Rect(rect.x, rect.y, rect.z, rect.w, colors[i % colors.Length]); } //for(var i = 0; i < rectSizes.Length; ++i) //{ // var rectSize = rectSizes[i]; // spriteBatch.Rect( // x, 0f, rectSize.x, rectSize.y, colors[i % colors.Length]); // x += rectSize.x; //} //spriteBatch.Quad( // new vec2(-n, -n), // new vec2(n, -n), // new vec2(n, n), // new vec2(-n, n), // // _texture, // Color.Red); //spriteBatch.Rect( // 100, 105, // 1000.0f, 1200.0f, //Color.Black); //spriteBatch.Rect( // 100, 105, // 1300.0f, 1200.0f, // _texture, // Color.Gainsboro); //for (var x = 0f; x < 1000f; x += 10) // for (var y = 0f; y < 1000f; y += 10) // { // spriteBatch.Rect( // x + _rotation % 100, y, // 9, 9, // Color.PaleVioletRed); // } //spriteBatch.Text((float)HOffset, (float)VOffset, "Hello World!!!", font, TextColor, 1f, 1f); } }