VECTORS: GHOSTS IN THE MACHINE in Font
VECTORS: GHOSTS IN THE MACHINE Paint ANSI/AIM Code 128 In None Using Barcode creation for Font Control to generate, create Code128 image in Font applications. www.OnBarcode.comQuick Response Code Maker In None Using Barcode generator for Font Control to generate, create QR Code image in Font applications. www.OnBarcode.com//v1: the ship's motion vector _v1.update ( _shipModel.xPos, _shipModel.yPos, (_shipModel.xPos + _shipModel.vx), (_shipModel.yPos + _shipModel.vy) ); //v2: the vector between the drag handles _v2.update(_handle1.x, _handle1.y, _handle2.x, _handle2.y); //v3: the vector between v1 and v2 _v3.update(_shipModel.xPos, _shipModel.yPos, _handle1.x, _handle1.y); Next, we calculate the ratio of the perpdot product of v3 and v2, and of v1 and v2. var t:Number = VectorMath.perpProduct(_v3, _v2) / VectorMath.perpProduct(_v1, _v2); The VectorMath class has a perpProduct method that does this calculation for us. static public function perpProduct(v1:VectorModel, v2:VectorModel):Number { var perpProduct:Number = v1.ln.vx * v2.dx + v1.ln.vy * v2.dy; return perpProduct; } Next, we combine v1 s position and velocity, and multiply it by the ratio (t) to find the intersection point. var intersection_X:Number = _v1.a.x + _v1.vx * t; var intersection_Y:Number = _v1.a.y + _v1.vy * t; Finally, we position the intersection mark. _mark.x = intersection_X; _mark.y = intersection_Y; (The visible X that you see on the stage is created from the IntersectionMark class in the vectors package. It draws a simple crosshair.) There s one small problem with this example that I m sure you ve noticed. The intersection mark doesn t know that it should be limited to the magnitude of v2. It thinks that v2 goes on forever. As a result, it finds intersections beyond the limits of v2, as you can see in Figure 232. This isn t actually incorrect as it has been coded, but we would have a much more usable system if intersections were found only within the vector s point A and B. Making UPC Symbol In None Using Barcode encoder for Font Control to generate, create UPC Code image in Font applications. www.OnBarcode.comBarcode Generation In None Using Barcode creation for Font Control to generate, create Barcode image in Font applications. www.OnBarcode.comFigure 232. Intersections are found outside the limits of the vector. We need to conjure up a little more vector magic to help us solve this problem. In the chapter s source files, you ll find a folder called LimitingIntersection. Run the SWF, and you ll notice that the intersection point is now mapped only on the length of the vector, and not beyond it. This is very easy to implement. The source code is exactly the same as the previous example, except for these additions: PDF 417 Generation In None Using Barcode generator for Font Control to generate, create PDF417 image in Font applications. www.OnBarcode.comPainting Data Matrix In None Using Barcode printer for Font Control to generate, create Data Matrix 2d barcode image in Font applications. www.OnBarcode.com1. Extend a vector from v2 s start point to the intersection point.
Generate ANSI/AIM Code 128 In None Using Barcode printer for Font Control to generate, create Code128 image in Font applications. www.OnBarcode.comDrawing USS93 In None Using Barcode maker for Font Control to generate, create ANSI/AIM Code 93 image in Font applications. www.OnBarcode.comvar v4:VectorModel = new VectorModel (_v2.a.x, _v2.a.y, intersection_X, intersection_Y); Generating Code 128 In VB.NET Using Barcode maker for .NET framework Control to generate, create Code 128 Code Set C image in Visual Studio .NET applications. www.OnBarcode.comCode 128A Printer In Java Using Barcode creation for Android Control to generate, create Code 128 image in Android applications. www.OnBarcode.com2. Extend another vector running in the opposite direction from v2 s end point to the
EAN13 Decoder In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comPaint Matrix In .NET Framework Using Barcode generator for VS .NET Control to generate, create Matrix image in .NET applications. www.OnBarcode.comintersection point. var v5:VectorModel = new VectorModel (_v2.b.x, _v2.b.y, intersection_X, intersection_Y); Decoding Barcode In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comDraw UPC Symbol In None Using Barcode printer for Software Control to generate, create UPCA image in Software applications. www.OnBarcode.com3. If the magnitude of either of these vectors is larger than v2 s magnitude, then you know
Quick Response Code Encoder In Java Using Barcode maker for Android Control to generate, create QR image in Android applications. www.OnBarcode.comPDF417 2d Barcode Recognizer In None Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comthat the intersection point falls beyond v2 s bounds. if(v4.m > _v2.m  v5.m > _v2.m) { //The intersection point is outside the magnitude of v2 intersection_X = 0; intersection_Y = 0; } You can decide what actions to take on this. In this simple example, the intersection point is moved to position 0,0, which keeps it out of trouble. In a more complex game environment, you ll probably have many more variables to consider. Data Matrix Generator In ObjectiveC Using Barcode encoder for iPad Control to generate, create DataMatrix image in iPad applications. www.OnBarcode.comQR Code ISO/IEC18004 Creation In .NET Framework Using Barcode drawer for Reporting Service Control to generate, create Denso QR Bar Code image in Reporting Service applications. www.OnBarcode.comVECTORS: GHOSTS IN THE MACHINE
Code39 Creator In None Using Barcode maker for Microsoft Word Control to generate, create Code 39 image in Office Word applications. www.OnBarcode.comScan Barcode In Visual C# Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications. www.OnBarcode.comFigure 233 illustrates how this works. It s a very simple concept, and once you become more comfortable using vectors, it s the kind of thing you ll easily be able to figure out for yourself. Figure 233. If the magnitude of either v4 or v5 is greater than v2, then the intersection point is not within the bounds of v2. Collision and bounce
You can relax now! We ve covered almost everything you need to know about vectors for 2D game design. The theory class is over, and we can now put some of this new information to practical use. Now we re going to solve the classic problem of lineversuspoint collision. This is collision between a twodimensional line segment and a onedimensional point. Points are objects with no height or width, just a position. They re often called particles, so that s how I ll be referring to them from now on. Collisions between lines and particles are the most basic form of collision detection that you can do. Once you understand these fundamentals, you ll find shapeversusshape collision, which we ll look at in the next chapters, much easier to comprehend.

