Finding distance

Archive of the old editing forum
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. This forum is archived - please use this set of forums to ask new questions.
Locked
User avatar
LilWhiteMouse
Posts: 2270
Joined: Tue Jul 15, 2003 7:00 pm
Location: Maine, US
Contact:

Finding distance

Post by LilWhiteMouse »

I'm trying to setup a script that monitors a minotaurfriend actor, and execute a script if he get's to far from the player. It doesn't work though, I tend to end up with negative distances. So what do I need to change so it does? FYI, I don't think I ever learned how to determine distance in three dimensions, I had to make a guess. Is that my error?

Code: Select all

script 52 (void) {
int PX, PY, PZ;
int MX, MY, MZ;
int Dist, Roam;
Roam = 1024;
while (1)
	{
//Get player location
Thing_ChangeTID(0, 255);
PX = GetActorX(255)/65536;
PY = GetActorY(255)/65536;
PZ = GetActorZ(255)/65536;
//Get monoceros location
MX = GetActorX(304)/65536;
MY = GetActorY(304)/65536;
MZ = GetActorZ(304)/65536;
Dist = ((PX - MX)^2 + (PY - MY)^2 + (PZ - MZ)^2)^(1/2);
print (d:Dist);//display distance test
if (Dist > Roam){ACS_Execute(53, 0);}//Respawns Monoceros near player
delay (const:35);
	}
}
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49234
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Post by Graf Zahl »

The problem is that the '^' operator is not doing what you think. In C and ACS it's an exclusive or. And don't forget that (1/2) is 0 in integer arithmetics.

To get the square you have to actually write (PX - MX)*(PX - MX).
An operator that calculates a square root doesn't exist.
It's actually not necessary to calculate the square root. You can as well calculate the square of the distance and then compare that to the square of Roam:

Code: Select all

Dist = (PX - MX)*(PX - MX) + (PY - MY)*(PY - MY) + (PZ - MZ)*(PZ - MZ); 
if (Dist > Roam*Roam)
This is most likely the only efficient way to do this in ACS.
User avatar
LilWhiteMouse
Posts: 2270
Joined: Tue Jul 15, 2003 7:00 pm
Location: Maine, US
Contact:

Post by LilWhiteMouse »

Thanks, it's working now.
User avatar
Eevee
Posts: 592
Joined: Wed Jul 16, 2003 5:26 am
Contact:

Post by Eevee »

Doesn't ACS have **? I'm.. pretty sure C does, but I can never keep the power operators straight between languages. o.-
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49234
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Post by Graf Zahl »

C doesn't and ACS does neither. Honestly, in a simple scripting language a power operator is rarely needed. When do you need a power greater than 2?
User avatar
Eevee
Posts: 592
Joined: Wed Jul 16, 2003 5:26 am
Contact:

Post by Eevee »

oh, right, C has pow(). PERL has **. Dammit.

I assumed ACS would because I keep thinking anything in Perl will work in C for some odd reason :P nevermind.
User avatar
cccp_leha
Posts: 1816
Joined: Wed Jul 16, 2003 7:21 am
Location: NJ, USA
Contact:

Post by cccp_leha »

Well, I guess that when "C" was stripped to "ACS", they tok out pow() as well because Hexen didn't have the 1337 functions to get actorX/Y/Z.
Locked

Return to “Editing (Archive)”