1 module PixelPerfectEngine.collision.pixelPreciseCollision; 2 3 /* 4 * Copyright (C) 2015-2018, by Laszlo Szeremi under the Boost license. 5 * 6 * Pixel Perfect Engine, collision.pixelPreciseCollision module. 7 */ 8 9 import PixelPerfectEngine.system.advBitArray; 10 11 import PixelPerfectEngine.graphics.common; 12 import PixelPerfectEngine.graphics.bitmap; 13 import PixelPerfectEngine.system.binarySearchTree; 14 import PixelPerfectEngine.collision; 15 import PixelPerfectEngine.collision.boxCollision; 16 17 public struct CollisionShape{ 18 Coordinate position; 19 AdvancedBitArray shape; /// 20 BitmapAttrib attributes; 21 } 22 /** 23 * Capable of detecting collision on a pixel basis. 24 */ 25 public class PixelPreciseCollisionDetector{ 26 CollisionShape[int] objects; 27 CollisionListener cl; 28 public void testAll(){ 29 foreach(objectA; objects.byKey){ 30 foreach(objectB; objects.byKey){ 31 if(objectA != objectB){ 32 if(areColliding(objects[objectA], objects[objectB])){ 33 cl.spriteCollision(new CollisionEvent(objectA,objectB)); 34 35 } 36 } 37 } 38 } 39 } 40 public void testSingle(int objectA){ 41 42 } 43 /** 44 * Tests two boxes together. Returns true on collision. 45 */ 46 protected bool areColliding(ref CollisionShape a, ref CollisionShape b){ 47 if (a.position.bottom < b.position.top) return false; 48 if (a.position.top > b.position.bottom) return false; 49 50 if (a.position.right < b.position.left) return false; 51 if (a.position.left > b.position.right) return false; 52 53 Coordinate cc; // test area coordinates 54 int cTPA, cTPB; // testpoints 55 56 // process the test area and calculate the test points 57 if(a.position.top >= b.position.top){ 58 cc.top = a.position.top; 59 cTPA = a.position.width() * (a.position.top - b.position.top); 60 }else{ 61 cc.top = b.position.top; 62 cTPB = b.position.width() * (b.position.top - a.position.top); 63 } 64 if(a.position.bottom >= b.position.bottom){ 65 cc.bottom = b.position.bottom; 66 }else{ 67 cc.bottom = a.position.bottom; 68 } 69 if(a.position.left >= b.position.left){ 70 cc.left = a.position.left; 71 cTPA += a.position.left - b.position.left; 72 }else{ 73 cc.left = b.position.left; 74 cTPB += b.position.left - a.position.left; 75 } 76 if(a.position.right >= b.position.right){ 77 cc.right = b.position.right; 78 }else{ 79 cc.right = a.position.right; 80 } 81 //writeln("A: x: ", ca.left," y: ", ca.top, "B: x: ", cb.left," y: ", cb.top, "C: x: ", cc.left," y: ", cc.top); 82 for(int y ; y < cc.height() ; y++){ 83 if(a.shape.test(cTPA, cc.width(), b.shape, cTPB)){ 84 return true; 85 } 86 cTPA += a.position.width(); 87 cTPB += b.position.width(); 88 } 89 90 return false; 91 } 92 }