public void addReferenceTwiceTest() { //Preconfig int radius1 = 20; Vector center1 = new Vector(0f, 0f); //Creation Bumper parent = new Bumper(); BoundingContainer bCont = new BoundingContainer(parent); BoundingCircle bC1 = new BoundingCircle(radius1, center1); bCont.AddBoundingBox(bC1); BoundingField bf = new BoundingField(0, 0); //Operation bf.addReference(bC1); bf.addReference(bC1); int hit = 0; //Assertion foreach (IBoundingBox b in bf.getReferences()) { if (b.Equals(bC1)) { hit++; } } if (hit == 1) { Assert.AreEqual(1, hit); } }
protected override void Init() { base.Init(); int r1 = 70; int r2 = 16; Vector mitteKreis = new Vector(140, 295); Vector obenKreisLinie = new Vector(180, 241); Vector p3 = new Vector(678, 505); Vector p4 = new Vector(700, 534); Vector circle2 = new Vector(692,546); Vector p5 = new Vector(692, 561); Vector p6 = new Vector(645, 566); Vector p7 = new Vector(116, 355); Vector peak = new Vector(707,553); r1 = (int)(r1/factor); r2 = (int)(r2 / factor); mitteKreis /= factor; obenKreisLinie /= factor; p3 /= factor; p4 /= factor; circle2 /= factor; p5 /= factor; p6 /= factor; p7 /= factor; peak /= factor; BoundingCircle bC1 = new BoundingCircle(r1, mitteKreis- new Vector(r1,r1)); BoundingLine bL1 = new BoundingLine(obenKreisLinie, peak); BoundingLine bL2 = new BoundingLine(p3, p4); BoundingCircle bC2 = new BoundingCircle(r2, circle2 - new Vector(r2, r2)); BoundingLine bL3 = new BoundingLine(p5, p6); BoundingLine bL4 = new BoundingLine(peak, p7); //bL1.bounceFactor = 2; this.BoundingContainer.AddBoundingBox(bC1); this.BoundingContainer.AddBoundingBox(bL1); //this.BoundingContainer.AddBoundingBox(bL2); //this.BoundingContainer.AddBoundingBox(bC2); //this.BoundingContainer.AddBoundingBox(bL3); this.BoundingContainer.AddBoundingBox(bL4); /* Version with exact BB * BoundingCircle bC1 = new BoundingCircle(r1, mitteKreis- new Vector(r1,r1)); BoundingLine bL1 = new BoundingLine(obenKreisLinie, p3); BoundingLine bL2 = new BoundingLine(p3, p4); BoundingCircle bC2 = new BoundingCircle(r2, circle2 - new Vector(r2, r2)); BoundingLine bL3 = new BoundingLine(p5, p6); BoundingLine bL4 = new BoundingLine(p6, p7); //bL1.bounceFactor = 2; this.BoundingContainer.AddBoundingBox(bC1); this.BoundingContainer.AddBoundingBox(bL1); this.BoundingContainer.AddBoundingBox(bL2); this.BoundingContainer.AddBoundingBox(bC2); this.BoundingContainer.AddBoundingBox(bL3); this.BoundingContainer.AddBoundingBox(bL4); * */ }
public override bool CircleIntersect(BoundingCircle bC, out Vector hitPoint, Vector velocity) { Vector thisWorldTras = this.BoundingContainer.ParentElement.Location; Vector bCWorldTrans = bC.BoundingContainer.ParentElement.Location; if (VectorDistance(bC.Position + bCWorldTrans, this.Position + thisWorldTras) < (this.radius + bC.radius)) { Vector direction = (-(bC.Position + bCWorldTrans) + (this.Position + thisWorldTras)); ; if (velocity != new Vector(0, 0)) { if (direction.X == 0 && direction.Y == 0) { direction = -velocity; } else if (direction.X * velocity.X >= 0 && direction.Y* velocity.Y >= 0) { //point in the same direction => reverse direction direction = -direction; } } if (direction.X == 0 && direction.Y == 0) { direction.X = 0; direction.Y = -1; } direction = NormalizeVector(direction); //hitPoint = bC.position + bCWorldTrans + Vector.Normalize(-(bC.position + bCWorldTrans) + (this.position + thisWorldTras)) * bC.radius hitPoint = bC.Position + bCWorldTrans + direction * bC.radius; return true; } hitPoint = new Vector(0, 0); return false; }
public void TestIntersect1pxOverlapBot() { //Preconfig Vector position1 = new Vector(0f, 50f); Vector target1 = new Vector(50f, 50f); int radius2 = 20; Vector center2 = new Vector(20, 49); Vector ballSpeed = new Vector(0, -5); Vector hitPoint; bool isIntersec = false; //Creation Line parent = new Line(); Ball ball = new Ball(); BoundingContainer bCont = new BoundingContainer(parent); BoundingContainer bCont2 = new BoundingContainer(ball); BoundingLine bL1 = new BoundingLine(position1, target1); BoundingCircle bC2 = new BoundingCircle(radius2, center2); bCont.AddBoundingBox(bL1); bCont2.AddBoundingBox(bC2); ball.Velocity = ballSpeed; parent.Location = (new Vector(0, 0)); //Operation isIntersec = bC2.Intersect(bL1, out hitPoint); //Assertion Assert.IsTrue(isIntersec); Assert.AreEqual(new Vector(40,50), hitPoint); }
public void TestBoundingCirclePushBackLeft() { //Preconfig int radius = 20; Vector position = new Vector(100f, 100f); Vector ballPos = new Vector(90, 120); Vector hitPoint = new Vector(100, 120); Vector ballSpeed = hitPoint - ballPos; Vector expectedPushBack = (radius * 2 / 1.9f) * ((hitPoint - (position + new Vector(radius, radius)))).AsNormalized(); Vector pushBackVec; //Creation Bumper parent = new Bumper(); BoundingCircle bC2 = new BoundingCircle(radius, position); BoundingContainer bCont = new BoundingContainer(parent); bCont.AddBoundingBox(bC2); //Operation parent.Location = (new Vector(0, 0)); pushBackVec = bC2.GetOutOfAreaPush(radius * 2, hitPoint, ballSpeed, ballPos); //Assertion Assert.AreEqual(expectedPushBack, pushBackVec); }
public void TestReflection135Direction() { //Preconfig Vector position = new Vector(100f, 100f); Vector ballPos = new Vector(140f, 120f); int radius = 20; Vector ballSpeed = new Vector(-100, -100); Vector hitPoint = new Vector(120 + 14.1421f, 120 + 14.1421f); Vector expectedReflection = -ballSpeed; expectedReflection.Normalize(); Vector reflection; //Creation Bumper parent = new Bumper(); BoundingCircle bC2 = new BoundingCircle(radius, position); BoundingContainer bCont = new BoundingContainer(parent); bCont.AddBoundingBox(bC2); //Operation parent.Location = (new Vector(0, 0)); reflection = bC2.Reflect(ballSpeed, hitPoint, ballPos); reflection.Normalize(); //Assertion Assert.AreEqual(expectedReflection.X, reflection.X, 0.001f); Assert.AreEqual(expectedReflection.Y, reflection.Y, 0.001f); }
public void TestIntersectIntersect1pxLeftTooFar() { //Preconfig int radius1 = 20; Vector center1 = new Vector(0f, 0f); int radius2 = 20; Vector center2 = new Vector(-19, 0f); Vector ballSpeed = new Vector(-5, 0); Vector hitPoint; bool isIntersec = false; //Creation Bumper parent = new Bumper(); Ball ball = new Ball(); BoundingContainer bCont = new BoundingContainer(parent); BoundingContainer bCont2 = new BoundingContainer(ball); BoundingCircle bC1 = new BoundingCircle(radius1, center1); BoundingCircle bC2 = new BoundingCircle(radius2, center2); bCont.AddBoundingBox(bC1); bCont2.AddBoundingBox(bC2); ball.Velocity = ballSpeed; parent.Location = (new Vector(0, 0)); parent.Width = 2 * radius1; parent.Height = 2 * radius1; //Operation isIntersec = bC1.Intersect(bC2, out hitPoint, ballSpeed); //Assertion Assert.IsTrue(isIntersec); Assert.AreEqual(new Vector(40, 20), hitPoint); }
protected override void Init() { base.Init(); //set up of bounding box BoundingCircle bc = new BoundingCircle(this.radius, new Vector(0, 0)); this.BoundingContainer.AddBoundingBox(bc); }
protected override void Init() { int r = 16; BoundingCircle bC = new BoundingCircle(r, new Vector( (BaseWidth / 2) - r, (BaseHeight / 2) - r )); this.BoundingContainer.AddBoundingBox(bC); bC.AssignToContainer(this.BoundingContainer); this.pureIntersection = true; }
protected override void Init() { this.pureIntersection = true; BoundingCircle bC = new BoundingCircle(15, new Vector(0, 0)); this.BoundingContainer.AddBoundingBox(bC); bC.AssignToContainer(this.BoundingContainer); this.pureIntersection = true; }
public void TakeOverBoundingContainerWithCircleBigCenter() { //preconfig int cols = 10; int rows = 10; int width = 100; int height = 100; int expectedFieldHeight = height / rows; int expectedFieldWidth = width / cols; int radius1 = 50; Vector position1 = new Vector(0, 0); //creation BoundingRaster br = new BoundingRaster(cols, rows, width, height); Bumper parent1 = new Bumper(); BoundingCircle bC1 = new BoundingCircle(radius1, position1); BoundingContainer bCont1 = new BoundingContainer(parent1); bCont1.AddBoundingBox(bC1); parent1.Location = (new Vector(0, 0)); //operation br.TakeOverBoundingContainer(bCont1); //assertion for (int x = 0; x < cols; x++) { for (int y = 0; y < rows; y++) { if (x <= 9 && x >= 0 && y <= 9 && y >= 0) { bool found = false; foreach (IBoundingBox b in br.GetBoundingField(x, y).getReferences()) { Assert.AreEqual(bC1, b); found = true; } if (!found) { Assert.Fail(); } } else { foreach (IBoundingBox b in br.GetBoundingField(x, y).getReferences()) { if (bC1.Equals(b)) { Assert.Fail(); } } } } } }
public void TestCreation() { //Preconfig int radius = 20; Vector center = new Vector(0f, 0f); //Creation BoundingCircle bC = new BoundingCircle(radius, center); //Operation //Assertion Assert.IsNotNull(bC); Assert.AreEqual(bC.Position, center + new Vector(radius, radius)); Assert.AreEqual(bC.radius, radius); }
public override bool CircleIntersect(BoundingCircle bC, out Vector hitPoint, Vector velocity) { //strategy: connect center of ball with start of line. calc where the normal from center of ball on line hits (pointNormalDirectionPice). If len from center of ball to this point //is smaller then radius then it is a hit. Should pointNormalDirectionPice be smaller then start - radius of ball or bigger then end+ radius of ball => ignore hitPoint = new Vector(0, 0); Vector bLWorldPos = this.Position + this.BoundingContainer.ParentElement.Location; Vector bLWorldTar = this.target + this.BoundingContainer.ParentElement.Location; Vector thisWorldPos = bC.Position + bC.BoundingContainer.ParentElement.Location; Vector centerOfCircle = thisWorldPos; Vector directionLine = bLWorldTar - bLWorldPos; Vector normalLine = new Vector(-directionLine.Y, directionLine.X); double lenDirectionPiece = Vector.Multiply((centerOfCircle - bLWorldPos), NormalizeVector(directionLine)); if (lenDirectionPiece <= -bC.radius || lenDirectionPiece >= (directionLine.Length + bC.radius)) { return false; } Vector pointNormalDirectionPice = bLWorldPos + lenDirectionPiece * NormalizeVector(directionLine); Vector normalFromDirLineToCenter = centerOfCircle - pointNormalDirectionPice; double diff = normalFromDirLineToCenter.Length; if (diff < bC.radius) { if (lenDirectionPiece < 0) { hitPoint = bLWorldPos; return true; } if (lenDirectionPiece > (directionLine.Length)) { hitPoint = bLWorldTar; return true; } //in this case T lies in the circle hitPoint = pointNormalDirectionPice; return true; } return false; }
protected override void Init() { base.Init(); RotationRange = -RotationRange; Vector offset = new Vector(-10, -10); int r1 = 70; int r2 = 16; Vector mitteKreis = new Vector(-(140 - 400) + 400, 295) + offset; Vector obenKreisLinie = new Vector(-(180 - 400) + 400, 241) + offset; Vector p3 = new Vector(-(678 - 400) + 400, 505) + offset; Vector p4 = new Vector(-(700 - 400) + 400, 534) + offset; Vector circle2 = new Vector(-(692 - 400) + 400, 546) + offset; Vector p5 = new Vector(-(692 - 400) + 400, 561) + offset; Vector p6 = new Vector(-(645 - 400) + 400, 566) + offset; Vector p7 = new Vector(-(116 - 400) + 400, 355) + offset; Vector peak = new Vector(-(707 - 400) + 400, 553) + offset; r1 = (int)(r1 / factor); r2 = (int)(r2 / factor); mitteKreis /= factor; obenKreisLinie /= factor; p3 /= factor; p4 /= factor; circle2 /= factor; p5 /= factor; p6 /= factor; p7 /= factor; peak /= factor; BoundingCircle bC1 = new BoundingCircle(r1, mitteKreis - new Vector(r1, r1)); BoundingLine bL1 = new BoundingLine(obenKreisLinie, peak); BoundingLine bL2 = new BoundingLine(p3, p4); BoundingCircle bC2 = new BoundingCircle(r2, circle2 - new Vector(r2, r2)); BoundingLine bL3 = new BoundingLine(p5, p6); BoundingLine bL4 = new BoundingLine(peak, p7); //bL1.bounceFactor = 2; this.BoundingContainer.AddBoundingBox(bC1); this.BoundingContainer.AddBoundingBox(bL1); //this.BoundingContainer.AddBoundingBox(bL2); // this.BoundingContainer.AddBoundingBox(bC2); //this.BoundingContainer.AddBoundingBox(bL3); this.BoundingContainer.AddBoundingBox(bL4); }
public void TestIntersectIntersectleft1pxOverlap() { //Preconfig Vector target1 = new Vector(40, 50); Vector position1 = new Vector(0f, 50f); int radius2 = 20; Vector center2 = new Vector(-39, 30); Vector ballSpeed = new Vector(0, 5); Vector hitPoint; bool isIntersec = false; //Creation Line parent = new Line(); Ball ball = new Ball(); BoundingContainer bCont = new BoundingContainer(parent); BoundingContainer bCont2 = new BoundingContainer(ball); BoundingLine bL1 = new BoundingLine(position1, target1); BoundingCircle bC2 = new BoundingCircle(radius2, center2); bCont.AddBoundingBox(bL1); bCont2.AddBoundingBox(bC2); ball.Velocity = ballSpeed; ball.Location = (new Vector(0, 0)); ball.Width = 20; ball.Height = 20; parent.Location = (new Vector(0, 0)); parent.Width = 40; parent.Height = 0; //Operation isIntersec = bL1.Intersect(bC2, out hitPoint); //Assertion Assert.IsTrue(isIntersec); Assert.AreEqual(0, hitPoint.X, 2); Assert.AreEqual(50f, hitPoint.Y, 2); }
public override IBoundingBox Clone() { //do not forget to assinge BoundingContainer after clone BoundingCircle bL = new BoundingCircle(this.radius, new Vector(this.Position.X - this.radius, this.Position.Y - this.radius)); return bL; }
public void TestIntersectNoIntersectTouch() { //Preconfig int radius1 = 20; Vector center1 = new Vector(0f, 0f); int radius2 = 20; Vector center2 = new Vector(40f, 0f); Vector ballSpeed = new Vector(-5, 0); Vector hitPoint; bool isIntersec = false; //Creation Bumper parent = new Bumper(); Ball ball = new Ball(); BoundingContainer bCont = new BoundingContainer(parent); BoundingContainer bCont2 = new BoundingContainer(ball); BoundingCircle bC1 = new BoundingCircle(radius1, center1); BoundingCircle bC2 = new BoundingCircle(radius2, center2); bCont.AddBoundingBox(bC1); bCont2.AddBoundingBox(bC2); ball.Velocity = ballSpeed; //Operation isIntersec = bC1.Intersect(bC2, out hitPoint); //Assertion Assert.IsFalse(isIntersec); }
/// <summary> /// Submethod of intersect => checks for an intersection of this bounding box and a Bounding circle /// </summary> /// <param name="bC">Bounding circle which might intersect with this bounding box</param> /// <param name="hitPoint">Point where this bounding box intersects with bC</param> /// <param name="velocity">Speed of the object intersecting</param> /// <returns></returns> public abstract bool CircleIntersect(BoundingCircle bC, out Vector hitPoint, Vector velocity);
public void TestIntersectIntersectNone() { //Preconfig Vector target1 = new Vector(20, 50); Vector position1 = new Vector(0f, 50f); int radius2 = 20; Vector center2 = new Vector(10, 0f); Vector ballSpeed = new Vector(0, 5); Vector hitPoint; bool isIntersec = false; //Creation Line parent = new Line(); Ball ball = new Ball(); BoundingContainer bCont = new BoundingContainer(parent); BoundingContainer bCont2 = new BoundingContainer(ball); BoundingLine bL1 = new BoundingLine(position1, target1); BoundingCircle bC2 = new BoundingCircle(radius2, center2); bCont.AddBoundingBox(bL1); bCont2.AddBoundingBox(bC2); ball.Velocity = ballSpeed; parent.Location = new Vector(0, 0); parent.Width = 20; parent.Height = 0; //Operation isIntersec = bC2.Intersect(bL1, out hitPoint); //Assertion Assert.IsFalse(isIntersec); }