|
-
Software Developer
[disc] Distance Calculations
I have been doing some tests and was just wonder what is better in the long run.
1. Using flashes built in Point object.
Code:
function Util_Distance1(Object1 : Object, Object2 : Object) : Number {
import flash.geom.Point;
var p1 : Point = new Point(Object1._x, Object1._y);
var p2 : Point = new Point(Object2._x, Object2._y)
var result_distance : Number = Point.distance(p1,p2);
delete p1;
delete p2;
return result_distance;
}
2. Using Math
Code:
function Util_Distance2(Object1 : Object, Object2 : Object) : Number {
xdist = Object2._x - Object1._x;
ydist = Object2._y - Object1._y;
return Math.sqrt(xdist * xdist + ydist * ydist);
}
The test I have ran both return 0 as the timed result.
So my question is, which of the two functions would give any performance?
Full Code
Code:
function Util_Distance1(Object1 : Object, Object2 : Object) : Number {
import flash.geom.Point;
var p1 : Point = new Point(Object1._x, Object1._y);
var p2 : Point = new Point(Object2._x, Object2._y)
var result_distance : Number = Point.distance(p1,p2);
delete p1;
delete p2;
return result_distance;
}
function Util_Distance2(Object1 : Object, Object2 : Object) : Number {
xdist = Object2._x - Object1._x;
ydist = Object2._y - Object1._y;
return Math.sqrt(xdist * xdist + ydist * ydist);
}
var StartTime:Number = getTimer();
var resultDistance : Number = Util_Distance1(Player1, Player2);
var EndTime:Number = getTimer();
trace ("Using Geom")
trace ("Distance: " + resultDistance);
trace ("Duration: " + (EndTime - StartTime) + " (ms)");
StartTime = getTimer();
resultDistance = Util_Distance2(Player1, Player2);
EndTime = getTimer();
trace ("Using Math")
trace ("Distance: " + resultDistance);
trace ("Duration: " + (EndTime - StartTime) + " (ms)");
Post you game for free at Gamestack.org. We send the user straight to your website, no iFrames or other leeching methods.
Please check out Gamestack.org for more information.
-
Well, a single run of each is going to yield identical results, virtually 100% of the time. You really need to loop over the exact thing you're interested in many times (1000's at least). Try only to loop the calculation you're interested in (e.g. put initialisation of things outside the timers, and outside of the functions.)
Try it again in a loop. I'd be interested to know the results, I don't have MX 2004.
-
Software Developer
Well I took them both and ran them in a loop of 1000 times, and here are my results.
Using Math - Util_Distance2();
Distance: 313.143581285007
Duration: 17 (ms)
Using Geom - Util_Distance1();
Distance: 313.143581285007
Duration: 79 (ms)
So I guess the Math one is faster
Code:
function Util_Distance2(Object1 : Object, Object2 : Object) : Number {
xdist = Object2._x - Object1._x;
ydist = Object2._y - Object1._y;
return Math.sqrt(xdist * xdist + ydist * ydist);
}
Anyone have any thoughts on this?
Post you game for free at Gamestack.org. We send the user straight to your website, no iFrames or other leeching methods.
Please check out Gamestack.org for more information.
-
Senior Member
I remember doing speed tests with built-in point object some time ago and found it slower then doing same calculations without it. I believe the Point.distance method itself uses exactly same Math function as you did, but it has some internal checks (it checks if the object is point, if the values are numbers etc) so that makes it slower.
-
dunno what you need the distance for but if you for example would like to check if only 2 points are close to each other (e.g collsion, reaching goal,...) you could add a if(){ line to roughly check if the minimum/ maximum distance is already close
sample of what I mean
Code:
p1 = {x:40,y:90};
p2 = {x:10,y:160};
function getDis(a,b,dist){
var dax = Math.abs(b.x-a.x);
var day = Math.abs(b.y-a.y);
if(Math.max(dax,day)<dist){//if longest delta is already close enough
var d = Math.sqrt(dax*dax+day*day);//real distance
return(d);
}else{
return(false);
}
}
_root.onEnterFrame = function(){
var v =getDis(p1,p2,100);
if(v!=false){
trace("hit @"+v+" distance");
}
}
I dont know if this will be faster,- but from my expierence cascading code this way can increase lots of performance and equalize it.
Hope you get what I mean
-
ism
because most apps will compare the distance to a fixed value, people will sometimes multiply the fixed distance by itself (which generally only occurs once) and then omit the square root function (which generally uses a lot of CPU)
Graphics Attract, Motion Engages, Gameplay Addicts
XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|