actionscript 3 - Which one executes faster: hitTestObject or Point.distance? -


i trying optimize collision detection alghorithm written in as3.

i wondering if there improvements in performance if use

point.distance(pointobject1, pointobject2); 

between 2 objects instead of using

object1.hittestobject(object2); 

my objects more or less convex border doesn't matter.

point.distance more faster (4 , more times!) if test objects complicated containers several children inside. if use simple sprite's it'll near 25% difference in functions execution time.

it's so, because point.distance counting hypotenuse pythagoras' theorem. so, we've got 2 subtractions, 1 addition , 3 involutions count. many modern processors have involution instruction, it's fast. if use hittest, there more actions performed. , number of these actions grow complexity of hittest'ing sprite (because it's harder count it's bounds).

i've made tests. result confirmed right.

var ar:vector.<sprite> = vector.<sprite>([]); //sprites hittest for(var i:int = 0; < 100000; i++) {     var sp:sprite = new sprite(); //!the results other, case of use huge container come objects here!     sp.graphics.drawcircle(0, 0, randomintbetween(1, 200)); //add shapes     sp.graphics.drawrect(0, 0, randomintbetween(1, 200), randomintbetween(1, 200));     sp.x = randomintbetween(-800, 800);     sp.y = randomintbetween(-800, 600);     sp.rotation = randomintbetween(-360, 360); //rotate , scale in random way     sp.scalex = sp.scaley = math.random();     ar.push(sp); }  var tim:number = new date().time; each(var spr:sprite in ar) {    ar[0].hittestobject(spr); } tim = new date().time - tim; trace(tim);  var pn:vector.<point> = vector.<point>([]); //points point.distance for(i = 0; < 100000; i++) {     var point:point = new point(randomintbetween(-800, 800), randomintbetween(-800, 800));     pn.push(point); }  tim = new date().time; each(var pnt:point in pn) {     point.distance(pn[0], pnt); } tim = new date().time - tim; trace(tim); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -