PIXEL-PERFECT COLLISION AND DESTRUCTIBLE ENVIRONMENTS
ANSI/AIM Code 128 Printer In NoneUsing Barcode encoder for Font Control to generate, create Code-128 image in Font applications.
www.OnBarcode.com Print Barcode In NoneUsing Barcode drawer for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com //The distance vector between the lander and the asteroid _v0.update ( _lander.xPos + _lander.width * 0.5, _lander.yPos + _lander.height * 0.5, _asteroidBitmap.x + _asteroidBitmap.width * 0.5, _asteroidBitmap.y + _asteroidBitmap.height * 0.5 ); //Check for a collision var loopCounter:int = 0; while (loopCounter++ != 10) { if(landerBitmap.bitmapData.hitTest ( new Point(_lander.xPos, _lander.yPos), 255, _asteroidBitmap, new Point(_asteroidBitmap.x, _asteroidBitmap.y), 255 ) ) { //Switch off gravity _lander.gravity_Vy = 0; //Move the lander out of the collision in the direction //of the distance vector _lander.setX = _lander.xPos - _v0.dx; _lander.setY = _lander.yPos - _v0.dy; _lander.vx = 0; _lander.vy = 0; } else { break; } } For a precise-looking collision, the code switches off the lander s gravity and set its velocity to 0. If this isn t done, the lander will appear to wobble slightly when it s resting on the asteroid. This is because the forces that are pushing and pulling it are not quite equal. If we switch gravity off when the lander is on the asteroid, we need to switch it back on again when it leaves. But there s a problem: when the lander is resting on the asteroid, it s actually not touching the asteroid any more. It s just slightly above it. Remember that the while loop pushes the lander and asteroid apart. The loop quits when they re no longer touching each other. That means that you can t run the same collision test to find out if
ECC200 Creator In NoneUsing Barcode maker for Font Control to generate, create Data Matrix 2d barcode image in Font applications.
www.OnBarcode.com Paint Code 128 Code Set B In NoneUsing Barcode creator for Font Control to generate, create Code 128B image in Font applications.
www.OnBarcode.com the lander is actually on the asteroid, because it will always return false. The loop has done its job and separated the objects perfectly. The lander and asteroid will be directly adjacent to each other, but they won t overlap by even 1 pixel. The problem is that because they re completely separated, we have no way to detect when the lander is resting on the asteroid. To solve this, we need to run a second collision check that tests to see if there is any ground directly below the lander. It checks to see if 1 pixel below the lander is in contact with the asteroid. If it is, then we know the lander is still on the asteroid. If there s no ground directly below it, then we know the lander isn t in contact with the ground, and we can switch gravity back on again. This will be easier for you understand if you can visualize it before you look at the code. Figure 58 illustrates the logic of what we want to accomplish.
QR Code Generator In NoneUsing Barcode encoder for Font Control to generate, create QR Code ISO/IEC18004 image in Font applications.
www.OnBarcode.com Barcode Printer In NoneUsing Barcode maker for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com Do a second collison test where the lander s collision area is shifted down by 1-pixel. This 1-pixel margin will tell you if the lander is touching the surface of the asteroid.
PDF 417 Generator In NoneUsing Barcode drawer for Font Control to generate, create PDF417 image in Font applications.
www.OnBarcode.com USD8 Creation In NoneUsing Barcode creation for Font Control to generate, create USD8 image in Font applications.
www.OnBarcode.com Turn gravity off.
Encode USS Code 128 In JavaUsing Barcode generation for Java Control to generate, create Code 128B image in Java applications.
www.OnBarcode.com Code128 Generator In NoneUsing Barcode printer for Office Word Control to generate, create Code-128 image in Word applications.
www.OnBarcode.com Turn gravity on.
Code128 Reader In NoneUsing Barcode reader for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com Make Matrix Barcode In Visual C#Using Barcode maker for VS .NET Control to generate, create Matrix 2D Barcode image in VS .NET applications.
www.OnBarcode.com Figure 5-8. Check whether the lander is on the surface of the asteroid. The code for this second collision test is saying, If an area 1 pixel below the lander isn t touching the asteroid, turn gravity on. Here s what it looks like:
Decode QR Code In .NET FrameworkUsing Barcode scanner for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com Make UPC Symbol In NoneUsing Barcode generation for Software Control to generate, create UPC Code image in Software applications.
www.OnBarcode.com PIXEL-PERFECT COLLISION AND DESTRUCTIBLE ENVIRONMENTS
Encode UPC Symbol In NoneUsing Barcode generation for Microsoft Excel Control to generate, create UPC-A Supplement 5 image in Office Excel applications.
www.OnBarcode.com Generating Data Matrix ECC200 In NoneUsing Barcode maker for Online Control to generate, create Data Matrix image in Online applications.
www.OnBarcode.com if(!landerBitmap.bitmapData.hitTest ( new Point(_lander.xPos, _lander.yPos + 1), 255, _asteroidBitmap, new Point(_asteroidBitmap.x, _asteroidBitmap.y), 255 ) ) { _lander.gravity_Vy = 0.1; } Note that it checks whether a collision is not happening. if(!... The + 1 is highlighted in the code so that you can clearly see where it shifts the collision area down by a pixel. new Point(_lander.xPos, _lander.yPos + 1), In environments where game objects rest on steep surfaces, you will need a bit more clearance. You ll be able tell this if the object appears to wobble slightly when it should be resting. If that s the case, use +2, +3, +4 keep increasing that number until the wobble disappears.
Data Matrix Generation In NoneUsing Barcode drawer for Software Control to generate, create Data Matrix ECC200 image in Software applications.
www.OnBarcode.com GS1 128 Recognizer In VB.NETUsing Barcode decoder for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com Draw 2D In .NETUsing Barcode generation for ASP.NET Control to generate, create Matrix 2D Barcode image in ASP.NET applications.
www.OnBarcode.com Barcode Maker In NoneUsing Barcode creation for Online Control to generate, create Barcode image in Online applications.
www.OnBarcode.com