Log in

View Full Version : dist calculations


Scorpious2k
11-22-2003, 01:15 PM
Took a little time to look at the dist calcs... in Mob::Dist it reads


float Mob::Dist(const Mob &other) {
double xDiff = other.x_pos - x_pos;
double yDiff = other.y_pos - y_pos;
double zDiff = other.z_pos - z_pos;
return sqrt( (xDiff * xDiff)
+ (yDiff * yDiff)
+ (zDiff * zDiff) );
}


Which looks about as efficient as you can get without going to assembler... but if you went to that level it might look like


float Mob::Dist(const Mob &other)
{
double xDiff = other.x_pos - x_pos;
double yDiff = other.y_pos - y_pos;
double zDiff = other.z_pos - z_pos;
double calctemp;
float calcdist;

__asm
{
fld xDiff
fmul xDiff
fst calctemp
fld yDiff
fmul yDiff
fadd calctemp
fst calctemp
fld zDiff
fmul zDiff
fadd calctemp
fsqrt
fst calcdist
}
return calcdist;
}


Which might run in under 500 cycles....

Haven't tested this mind you, just a thought :-)

DannMann99
11-22-2003, 01:49 PM
Not to get off topic but... i love the sig lol... kind of like the saying:
"I am a compulsive liar". Makes you wonder.

About the code, you should compile it and see what happens.
~Dan

Scorpious2k
11-22-2003, 02:05 PM
i love the sig lol...

Thanks, it fits if you know me. :-)

About the code, you should compile it and see what happens.

I compiled it to check and see that it was clean, but we aren't running 5.0 at the moment so I have no way to test it. I wrote it sort of off the top of my head, so I'm not positive its exactly right.

I know that Trumpcard is looking for something like this and thought I would toss it out. If it works well enough, I'll do the rest of the dist calcs if he wants.

Mongrel
11-23-2003, 07:56 PM
Why use doubles? I doubt we need 64 bit accuracy anyway, so unless someone is running a 64 bit server, floats are 2x faster and use half the memory space.

Trumpcard
11-23-2003, 11:59 PM
You're right, those should be floats...

The original programmer put them in as doubles, we've just never gone back around to fixing it...

Good catch on that, didnt even notice it.. I'll fix those in CVS tonight.

Scorpious2k
11-24-2003, 01:30 PM
At the assembly level, it doesn't seem to make a difference since they are all converted to 80 bit values on load. And I don't see a difference in the timing in it either.

I still agree the precision should be only what is needed because it creates overhead to convert in C++.

Eglin
02-06-2004, 01:42 AM
It is usually better to calculate distance squared, since you eliminate the root op.
Took a little time to look at the dist calcs... in Mob::Dist it reads

float Mob::Dist(const Mob &other) {
double xDiff = other.x_pos - x_pos;
double yDiff = other.y_pos - y_pos;
double zDiff = other.z_pos - z_pos;
return sqrt( (xDiff * xDiff)
+ (yDiff * yDiff)
+ (zDiff * zDiff) );
}

Which looks about as efficient as you can get without going to assembler... but if you went to that level it might look like
...<snip>...

kathgar
02-06-2004, 05:07 PM
Thats what DistNoRoot and DistNoRootNoZ are for