 Home
 Products
 Integration
 Tutorial
 Barcode FAQ
 Purchase
 Company
COLLISIONS BETWEEN CIRCLES in Font
COLLISIONS BETWEEN CIRCLES Create USS Code 128 In None Using Barcode maker for Font Control to generate, create Code128 image in Font applications. www.OnBarcode.comCreate EAN13 In None Using Barcode maker for Font Control to generate, create EAN / UCC  13 image in Font applications. www.OnBarcode.com//xSide is 1 when _c1 is to the right of _c2, //and 1 when it's to the left of _c2. //ySide is 1 when _c1 is below _c2, //and 1 when it's above _c2 _c1.xPos > _c2.xPos xSide = 1 : xSide = 1; _c1.yPos > _c2.yPos ySide = 1 : ySide = 1; //Reposition _c1 out of the collision. //Use xSide and ySide to make the overlap vector either //negative or positive, depending on the side of the collision _c1.setX = _c1.xPos + (collision_Vx * xSide); _c1.setY = _c1.yPos + (collision_Vy * ySide); //Reposition _c2 _c2.setX = _c2.xPos + (collision_Vx * xSide); _c2.setY = _c2.yPos + (collision_Vy * ySide); //We ve moved the circles out of the collision. Now we //need to bounce them away at the correct angle //_c1's motion vector var v1:VectorModel = new VectorModel ( _c1.xPos, _c1.yPos, _c1.xPos + _c1.vx, _c1.yPos + _c1.vy ); //_c2's motion vector var v2:VectorModel = new VectorModel ( _c2.xPos, _c2.yPos, _c2.xPos + _c2.vx, _c2.yPos + _c2.vy ); //Project v1 onto v0 and v0.ln var p1a:VectorModel = VectorMath.project(v1, v0); var p1b:VectorModel = VectorMath.project(v1, v0.ln); //Project v2 onto v0 and v0.ln var p2a:VectorModel = VectorMath.project(v2, v0); var p2b:VectorModel = VectorMath.project(v2, v0.ln); Paint QR Code In None Using Barcode creator for Font Control to generate, create QRCode image in Font applications. www.OnBarcode.comPaint DataMatrix In None Using Barcode creation for Font Control to generate, create Data Matrix image in Font applications. www.OnBarcode.com//Mix and match the results of p1 and p2 to //find the correct bounce vectors //Bounce c1 //using p1b and p2a _c1.vx = p1b.vx + p2a.vx; _c1.vy = p1b.vy + p2a.vy; //Bounce c2 //using p1a and p2b _c2.vx = p1a.vx + p2b.vx; _c2.vy = p1a.vy + p2b.vy; } A collision is detected when the magnitude of the vector between the circles is less than their combined radii. This is exactly the same way a collision was detected in our first example. But because both circles are moving, we have a new problem. When only one circle was moving, it was easy to know which circle to reposition after the collision: the moving one. Now that both circles could be moving, which circle do we reposition, and by how much To figure that out, we need to do the following: Printing Code 128 Code Set A In None Using Barcode maker for Font Control to generate, create Code 128A image in Font applications. www.OnBarcode.comPrint Barcode In None Using Barcode generation for Font Control to generate, create Barcode image in Font applications. www.OnBarcode.com1. Find out the amount by which the circles overlap when they collide.
Barcode Encoder In None Using Barcode generation for Font Control to generate, create Barcode image in Font applications. www.OnBarcode.comDrawing Royal Mail Barcode In None Using Barcode generator for Font Control to generate, create Royal Mail Barcode image in Font applications. www.OnBarcode.comvar overlap:Number = totalRadii  v0.m; This is the same code that we used to find the overlap in the example with the stationary circle. Code 128 Code Set B Encoder In Visual Basic .NET Using Barcode encoder for .NET framework Control to generate, create Code128 image in .NET applications. www.OnBarcode.comMaking USS Code 128 In .NET Using Barcode drawer for ASP.NET Control to generate, create ANSI/AIM Code 128 image in ASP.NET applications. www.OnBarcode.com2. Create a collision vector. This will give us the vx and vy values we need to separate
Decode Code 128A In Visual Basic .NET Using Barcode recognizer for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comBarcode Encoder In ObjectiveC Using Barcode creator for iPad Control to generate, create Barcode image in iPad applications. www.OnBarcode.comthe circles. This vector must be divided in half so that we can share it between the two circles. One half will go to circle 1, and the other half will go to circle 2. var collision_Vx:Number = Math.abs(v0.dx * overlap * 0.5); var collision_Vy:Number = Math.abs(v0.dy * overlap * 0.5); The vector is found by scaling v0 s magnitude to the amount of overlap. By multiplying the overlap by v0 s dx and dy, we have a new vector inclined at the same angle as v0 but with the same magnitude as the overlap. Figure 322 shows how this vector is found. The vx and vy values need to be absolute (without a plus or minus sign). We need these values to be neutral because we want the flexibility to decide whether to add or subtract this vector to each circle s position (you ll see how in the next step). Paint Barcode In None Using Barcode printer for Software Control to generate, create Barcode image in Software applications. www.OnBarcode.comUCC  12 Reader In C#.NET Using Barcode reader for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comCOLLISIONS BETWEEN CIRCLES
Paint Barcode In None Using Barcode drawer for Office Excel Control to generate, create Barcode image in Office Excel applications. www.OnBarcode.comData Matrix ECC200 Recognizer In Java Using Barcode decoder for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comFigure 322. Find the collision vector, divide it by half, and make it absolute.
Recognizing DataMatrix In None Using Barcode scanner for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comRecognize Barcode In VB.NET Using Barcode recognizer for .NET framework Control to read, scan read, scan image in .NET framework applications. www.OnBarcode.com3. Is the first circle above, below, to the right of, or to the left of the second circle We need
Matrix 2D Barcode Encoder In Visual C#.NET Using Barcode drawer for VS .NET Control to generate, create Matrix 2D Barcode image in VS .NET applications. www.OnBarcode.comPaint Barcode In Java Using Barcode creator for Java Control to generate, create Barcode image in Java applications. www.OnBarcode.comto know this information so that we can correctly add or subtract the overlap vector to each circle s position. The easiest way to track this is by creating variables that are assigned 1 or 1, depending on where the circles are in relation to each other. Why we need to do this will become clear in the next step. var xSide:int; var ySide:int; _c1.xPos > _c2.xPos xSide = 1 : xSide = 1; _c1.yPos > _c2.yPos ySide = 1 : ySide = 1; This bit of code illustrates the use of the ternary operation. It s a shorthand style of writing if / else statements. This line of code _c1.xPos > _c2.xPos xSide = 1 : xSide = 1; is the same as writing this: if(_c1.xPos > _c2.xPos) { xSide = 1; } else { xSide = 1; } For short, simple conditional tests, the ternary operation is very helpful. It takes up much less space and will make your code more readable once your eyes get used to the syntax. Try it! 4. We need to push the circles apart to resolve the collision. For example, on the x axis, we ll need to push one circle to the left and the other to the right. The collision vector that we calculated in step 2 will be the correct direction for one of the circles, but not the other. However, we know that the directions the circles need to move in will be the polar opposites of each other. That means we can use the xSide and ySide variables (which will be 1 or 1) to correctly invert one of the vectors. //Move _c1 out of the collision _c1.setX = _c1.xPos + (collision_Vx * xSide); _c1.setY = _c1.yPos + (collision_Vy * ySide); //Move _c2 out of the collision _c2.setX = _c2.xPos + (collision_Vx * xSide); _c2.setY = _c2.yPos + (collision_Vy * ySide); The orientation of the circles will always be changing, so we can never know which circle s overlap vector to invert. Luckily, the xSide and ySide variables take care of keeping track of that for us automatically. Figure 323 shows how the circles new positions are found.

