If you look in my profie you see that I’ve written a .NET compiler for Scratch. What is Scratch? It’s a language to teach children the basics of programming (http://scratch.mit.edu) but it’s pretty addictive for older “children” too! I chose monogame as the basis for the compiler’s graphics side of things and have been very happy except for one set of Scratch blocks. These blocks return true or false if a sprite is touching another sprite or colour or one sprite’s colour is touching another colour. In other words they perform pixel perfect collision detection. The latest version of Scratch is written in Adobe ActionScript (Flash) and they have recently opened the source code for this https://github.com/LLK/scratch-flash/blob/master/src/primitives/SensingPrims.as. Having been intrigued I had a look at the “sensing” code. e.g.
if(true || !app.isIn3D) {
var sBM:BitmapData = s.bitmap();
for each (s2 in app.stagePane.spritesAndClonesNamed(arg))
if (s2.visible && sBM.hitTest(s.bounds().topLeft, 1, s2.bitmap(), s2.bounds().topLeft, 1))
return true;
}
and for colour sensing they can filter bitmaps by colour which is pretty neat e.g.
var outBM:BitmapData = new BitmapData(srcBM.width, srcBM.height, true, 0);
outBM.threshold(srcBM, srcBM.rect, srcBM.rect.topLeft, '==', c, 0xFF000000, 0xF0F8F8F0); // match only top five bits of each component
For my code I don’t have the those functions but use rectangular bounds for the initial hit test before using the texture map as an array and walking the pixels for a pixel perfect hit test. It works well but is no where near as quick as Scratch. Anyone any ideas how to speed things up or insight as to how Adobe get their bitmap speed?
Thanks in advance.