Distance3DSquared, LengthSquared

Remember, just because you request it, that doesn't mean you'll get it.

Moderator: GZDoom Developers

User avatar
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

Post by Matt »

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.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49252
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Distance3DSquared, LengthSquared

Post by Graf Zahl »

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.
User avatar
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

Post by Matt »

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()?
dpJudas
 
 
Posts: 3177
Joined: Sat May 28, 2016 1:01 pm

Re: Distance3DSquared, LengthSquared

Post by dpJudas »

If there is already a dot function you can do lengthSquared yourself by doing dot(pos, pos).
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49252
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Distance3DSquared, LengthSquared

Post by Graf Zahl »

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.
User avatar
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

Post by Matt »

dpJudas wrote:If there is already a dot function you can do lengthSquared yourself by doing dot(pos, pos).
Just tried it now on 3.1.0 and that syntax does not work ("unexpected 'dot'").
User avatar
Major Cooke
Posts: 8218
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

Post by Major Cooke »

Code: Select all

void DumbDot()
{
	Vector3 p;
	p = p.dot(pos, pos);
}
Confirmed Vaecrius' report. This doesn't work.

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
*/
Found this in the source code, is this supposed to work in zscript like the C++ side?
dpJudas
 
 
Posts: 3177
Joined: Sat May 28, 2016 1:01 pm

Re: Distance3DSquared, LengthSquared

Post by dpJudas »

Major Cooke wrote:Found this in the source code, is this supposed to work in zscript like the C++ side?
I don't know, but personally I greatly dislike those operators. Global functions like glsl does it is the way to go.
User avatar
Major Cooke
Posts: 8218
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

Post by Major Cooke »

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

Post by _mental_ »

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;
User avatar
Major Cooke
Posts: 8218
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

Post by Major Cooke »

Aaaah, so that's how it is. Documentation added.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49252
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Distance3DSquared, LengthSquared

Post by Graf Zahl »

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.
User avatar
Major Cooke
Posts: 8218
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

Post by Major Cooke »

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.
User avatar
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

Post by Matt »

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;
    }
} 
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49252
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: Distance3DSquared, LengthSquared

Post by Graf Zahl »

How does dot compare to length?

Return to “Feature Suggestions [GZDoom]”