Distance3DSquared, LengthSquared
Moderator: GZDoom Developers
-
Matt
- Posts: 9696
- Joined: Sun Jan 04, 2004 5:37 pm
- Preferred Pronouns: They/Them
- Operating System Version (Optional): Debian Bullseye
- Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Distance3DSquared, LengthSquared
Same thing as Distance3D and Length but without the square root - a lot of mods call for repeated distance checks for multiple actors and this can add up quite a bit.
I tried making my own but the virtual machine overhead far more than outweighed any performance advantage.
I tried making my own but the virtual machine overhead far more than outweighed any performance advantage.
-
Graf Zahl
- Lead GZDoom+Raze Developer

- Posts: 49252
- Joined: Sat Jul 19, 2003 10:19 am
- Location: Germany
Re: Distance3DSquared, LengthSquared
I didn't do these because calculating a square root is mostly irrelevant when considering the VM. Of course a benchmark that proves different might make me reconsider.
-
Matt
- Posts: 9696
- Joined: Sun Jan 04, 2004 5:37 pm
- Preferred Pronouns: They/Them
- Operating System Version (Optional): Debian Bullseye
- Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Re: Distance3DSquared, LengthSquared
Doing a bunch of pos.length() calls every tic in ZScript is definitely faster than pos.x*pos.x+pos.y*pos.y+pos.z*pos.z, though I don't even know what coding the native version that is actually equivalent to length() without the square root would look like (I'm assuming there's some kind of memory handling optimization involving arrows or something)...
EDIT: or is it literally just lengthsquared()?
EDIT: or is it literally just lengthsquared()?
-
dpJudas
-

- Posts: 3177
- Joined: Sat May 28, 2016 1:01 pm
Re: Distance3DSquared, LengthSquared
If there is already a dot function you can do lengthSquared yourself by doing dot(pos, pos).
-
Graf Zahl
- Lead GZDoom+Raze Developer

- Posts: 49252
- Joined: Sat Jul 19, 2003 10:19 am
- Location: Germany
Re: Distance3DSquared, LengthSquared
There's a dot product operator, called 'dot'. Whether it is faster remains to be seen, as it requires one more VM instruction than calling 'length' on a vector.
-
Matt
- Posts: 9696
- Joined: Sun Jan 04, 2004 5:37 pm
- Preferred Pronouns: They/Them
- Operating System Version (Optional): Debian Bullseye
- Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Re: Distance3DSquared, LengthSquared
Just tried it now on 3.1.0 and that syntax does not work ("unexpected 'dot'").dpJudas wrote:If there is already a dot function you can do lengthSquared yourself by doing dot(pos, pos).
-
Major Cooke
- Posts: 8215
- Joined: Sun Jan 28, 2007 3:55 pm
- Preferred Pronouns: He/Him
- Operating System Version (Optional): Windows 10
- Graphics Processor: nVidia with Vulkan support
- Location: GZBoomer Town
Re: Distance3DSquared, LengthSquared
Code: Select all
void DumbDot()
{
Vector3 p;
p = p.dot(pos, pos);
}
But, hey, hold on a second...
Code: Select all
/* Since C++ doesn't let me add completely new operators, the following two
** are overloaded for vectors:
**
** | dot product
** ^ cross product
*/
-
dpJudas
-

- Posts: 3177
- Joined: Sat May 28, 2016 1:01 pm
Re: Distance3DSquared, LengthSquared
I don't know, but personally I greatly dislike those operators. Global functions like glsl does it is the way to go.Major Cooke wrote:Found this in the source code, is this supposed to work in zscript like the C++ side?
-
Major Cooke
- Posts: 8215
- Joined: Sun Jan 28, 2007 3:55 pm
- Preferred Pronouns: He/Him
- Operating System Version (Optional): Windows 10
- Graphics Processor: nVidia with Vulkan support
- Location: GZBoomer Town
Re: Distance3DSquared, LengthSquared
Yeah I don't like it myself either. I think an actual Distance3DSquared() and LengthSquared() function could be implemented, including a Dot(v1, v2) and Cross(v1, v2) function in the event we want to use two different vectors.
-
_mental_
-

- Posts: 3820
- Joined: Sun Aug 07, 2011 4:32 am
Re: Distance3DSquared, LengthSquared
dot and cross are operators, not member functions:
Code: Select all
Vector3 a = (1, 2, 3);
Vector3 b = (3, 2, 1);
double dot_prod = a dot b;
Vector3 cross_prod = a cross b;-
Major Cooke
- Posts: 8215
- Joined: Sun Jan 28, 2007 3:55 pm
- Preferred Pronouns: He/Him
- Operating System Version (Optional): Windows 10
- Graphics Processor: nVidia with Vulkan support
- Location: GZBoomer Town
Re: Distance3DSquared, LengthSquared
Aaaah, so that's how it is. Documentation added.
-
Graf Zahl
- Lead GZDoom+Raze Developer

- Posts: 49252
- Joined: Sat Jul 19, 2003 10:19 am
- Location: Germany
Re: Distance3DSquared, LengthSquared
You have to ask Randi why it was done this way. Back when I implemented them ZDoom hadn't been abandoned yet so I tried to avoid removing features from the language, even though I thought they were not so great.
-
Major Cooke
- Posts: 8215
- Joined: Sun Jan 28, 2007 3:55 pm
- Preferred Pronouns: He/Him
- Operating System Version (Optional): Windows 10
- Graphics Processor: nVidia with Vulkan support
- Location: GZBoomer Town
Re: Distance3DSquared, LengthSquared
I doubt Randi's logged in any time lately.
Anyway, I'd try making one of these functions myself to test stuff with if I had any idea on how to do so for vectors, but after taking a look at how Length() and Unit() are done... Yeah I have no idea how.
Anyway, I'd try making one of these functions myself to test stuff with if I had any idea on how to do so for vectors, but after taking a look at how Length() and Unit() are done... Yeah I have no idea how.
-
Matt
- Posts: 9696
- Joined: Sun Jan 04, 2004 5:37 pm
- Preferred Pronouns: They/Them
- Operating System Version (Optional): Debian Bullseye
- Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Re: Distance3DSquared, LengthSquared
Just tried with this (with and without the commented-out if lines). Dot is way faster - like 46-48 fps versus 1-2 fps without the commented-out lines.
Code: Select all
/*
vid_fps 1;summon cru;
vid_fps 1;summon fin;
*/
version "3.1.0"
class Cru:Actor{
states{
spawn:
BAL1 A 1 nodelay{
for(int i=0;i<1000000;i++){
double bleh=pos dot pos;
// if(bleh>pos.x*pos.x)bleh=12345;
}
}wait;
}
}
class Fin:Actor{
states{
spawn:
BAL1 A 1 nodelay{
for(int i=0;i<1000000;i++){
double bleh=pos.length();
// if(bleh>pos.x)bleh=12345;
}
}wait;
}
} -
Graf Zahl
- Lead GZDoom+Raze Developer

- Posts: 49252
- Joined: Sat Jul 19, 2003 10:19 am
- Location: Germany
Re: Distance3DSquared, LengthSquared
How does dot compare to length?