Frictional Games Forum (read-only)
[SCRIPT] Focus Distance Debug Variable? - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: [SCRIPT] Focus Distance Debug Variable? (/thread-28110.html)

Pages: 1 2


RE: Focus Distance Debug Variable? - BonesTheRabbit - 12-27-2014

(12-26-2014, 12:26 PM)TheGreatCthulhu Wrote:
Quote:It's just not as elegant as calculating the value fluidly.
Well, maybe not - but you have to work with what you've got available
I understand that. Was just trying to see if there was an alternative method which I wasn't aware of. The game obviously calculates a relevant value. I just figured there might be a global variable that called it. There doesn't appear to be, and that's fine. I don't even know if I'll use any form of the idea yet, honestly.


RE: Focus Distance Debug Variable? - Daemian - 12-27-2014

I don't see how you're gonna detect when the players look at any of those area, cause they are kinda far and probably many objects will interfere, like the same bridge on his feet.

Apart from that, I made a function to get the distance between two entities the other day.
You maybe find it useful.

PHP Code:
float GetEntitiesDistance string &in fromEntitystring &in toEntity )
{
    
float x1 GetEntityPosXfromEntity );
    
float y1 GetEntityPosYfromEntity );
    
float z1 GetEntityPosZfromEntity );
    
    
float x2 GetEntityPosXtoEntity );
    
float y2 GetEntityPosYtoEntity );
    
float z2 GetEntityPosZtoEntity );
    
    
float distance1 MathSqrt(MathPow(MathAbs(x1),2) + MathPow(MathAbs(z1),2));
    
distance1 MathSqrt(MathPow(MathAbs(y1),2) + MathPow(MathAbs(distance1),2));
    
    
float distance2 MathSqrt(MathPow(MathAbs(x2),2) + MathPow(MathAbs(z2),2));
    
distance2 MathSqrt(MathPow(MathAbs(y2),2) + MathPow(MathAbs(distance2),2));
    
    
float totalDistance MathMax(distance1,distance2) - MathMin(distance1,distance2);
    
    return 
totalDistance;
    




RE: Focus Distance Debug Variable? - TheGreatCthulhu - 12-29-2014

@Daemian:
Let me offer some more insight and advice about how to calculate distances in 3D space:

You basically have two 3D vectors that tell you the location of each entity
locFrom = (x1, y1, z1)
locTo = (x2, y2, z2)


To get the vector that represents the distance between them, you simply need to calculate the (locTo - locFrom) vector, and get its length.
The subtraction part is easy:
locDelta = (locTo - locFrom) = (x2 - x1, y2 - y1, z2 - z1) = (x3, y3, z3)

(That corresponds the r-r' vector in the image below.)
[Image: central.png?w=300&h=&cache=cache]

Finally, you get the length by calculating
dist = Sqrt(x3^2 + y3^2 + z3^2)
Where ^2 stands for squared.

(See the image below to see why the length of a vector is calculated in this way - it's just the Pythagorean theorem.)
[Image: vect.h9.gif]

Note that there's no need for absolute value, since squared quantities are always positive.

What you're doing will generally not give you the correct distance - you also start with two vectors:
locFrom = (x1, y1, z1)
locTo = (x2, y2, z2)


But then you do this (I use || to denote abs value):
dist1 = Sqrt(|y1|^2 + |Sqrt(|x1|^2 + |z1|^2)|^2);

which is basically the same as this, since you take the square of the square root:
dist1 = Sqrt(x1^2 + y1^2 + z1^2);

Which is the distance from the origin to the first entity.

Then you calculate
dist2 = Sqrt(|y2|^2 + |Sqrt(|x2|^2 + |z2|^2)|^2);

which equivalent to:
dist2 = Sqrt(x2^2 + y2^2 + z2^2);

That's the distance from the origin to the second entity.
With that, you have two distances, without any information about the directions.

Then you do this:
dist = Max(dist1, dist2) - Min(dist1, dist2)

which is the same as
dist = |dist2 - dist1|

This will only give you the amount their distances differ from the origin, which will match the actual distance between the two objects only if they are on the same line (ray) from the origin. For example, if they are at different angles with respect to the origin, but at the same distance from the origin, the result would be 0, even though there's a non-zero distance between the objects themselves.

BTW, when programming/scripting, if speed is important, you want to do as few Sqrt operations as possible, because these are computationally pretty expensive; on the other hand, + and * are relatively cheep. Also, it's the game will take less time to calculate a*a then MathPow(a, 2), so if it's just the square, people often write a*a, and use the math function for higher powers.

So, with regard to all that, the code should look something like this:
PHP Code:
float GetEntitiesDistance string &in fromEntitystring &in toEntity )
{
    
float x1 GetEntityPosXfromEntity );
    
float y1 GetEntityPosYfromEntity );
    
float z1 GetEntityPosZfromEntity );
    
    
float x2 GetEntityPosXtoEntity );
    
float y2 GetEntityPosYtoEntity );
    
float z2 GetEntityPosZtoEntity );

    
float x3 x2 x1;
    
float y3 y2 y1;
    
float z3 z2 z1;
    
float distance MathSqrt(x3*x3 y3*y3 z3*z3);

    return 
distance;




RE: Focus Distance Debug Variable? - PutraenusAlivius - 12-29-2014

(12-29-2014, 01:41 PM)TheGreatCthulhu Wrote: @Daemian:
Let me offer some more insight and advice about how to calculate distances in 3D space:

You basically have two 3D vectors that tell you the location of each entity
locFrom = (x1, y1, z1)
locTo = (x2, y2, z2)


To get the vector that represents the distance between them, you simply need to calculate the (locTo - locFrom) vector, and get its length.
The subtraction part is easy:
locDelta = (locTo - locFrom) = (x2 - x1, y2 - y1, z2 - z1) = (x3, y3, z3)

(That corresponds the r-r' vector in the image below.)
[Image: central.png?w=300&h=&cache=cache]

Finally, you get the length by calculating
dist = Sqrt(x3^2 + y3^2 + z3^2)
Where ^2 stands for squared.

(See the image below to see why the length of a vector is calculated in this way - it's just the Pythagorean theorem.)
[Image: vect.h9.gif]

Note that there's no need for absolute value, since squared quantities are always positive.

What you're doing will generally not give you the correct distance - you also start with two vectors:
locFrom = (x1, y1, z1)
locTo = (x2, y2, z2)


But then you do this (I use || to denote abs value):
dist1 = Sqrt(|y1|^2 + |Sqrt(|x1|^2 + |z1|^2)|^2);

which is basically the same as this, since you take the square of the square root:
dist1 = Sqrt(x1^2 + y1^2 + z1^2);

Which is the distance from the origin to the first entity.

Then you calculate
dist2 = Sqrt(|y2|^2 + |Sqrt(|x2|^2 + |z2|^2)|^2);

which equivalent to:
dist2 = Sqrt(x2^2 + y2^2 + z2^2);

That's the distance from the origin to the second entity.
With that, you have two distances, without any information about the directions.

Then you do this:
dist = Max(dist1, dist2) - Min(dist1, dist2)

which is the same as
dist = |dist2 - dist1|

This will only give you the amount their distances differ from the origin, which will match the actual distance between the two objects only if they are on the same line (ray) from the origin. For example, if they are at different angles with respect to the origin, but at the same distance from the origin, the result would be 0, even though there's a non-zero distance between the objects themselves.

BTW, when programming/scripting, if speed is important, you want to do as few Sqrt operations as possible, because these are computationally pretty expensive; on the other hand, + and * are relatively cheep. Also, it's the game will take less time to calculate a*a then MathPow(a, 2), so if it's just the square, people often write a*a, and use the math function for higher powers.

So, with regard to all that, the code should look something like this:
PHP Code:
float GetEntitiesDistance string &in fromEntitystring &in toEntity )
{
    
float x1 GetEntityPosXfromEntity );
    
float y1 GetEntityPosYfromEntity );
    
float z1 GetEntityPosZfromEntity );
    
    
float x2 GetEntityPosXtoEntity );
    
float y2 GetEntityPosYtoEntity );
    
float z2 GetEntityPosZtoEntity );

    
float x3 x2 x1;
    
float y3 y2 y1;
    
float z3 z2 z1;
    
float distance MathSqrt(x3*x3 y3*y3 z3*z3);

    return 
distance;


http://www.reddit.com/r/theydidthemath/


RE: Focus Distance Debug Variable? - TheGreatCthulhu - 12-29-2014

(12-29-2014, 05:41 PM)Julius Caesar Wrote: http://www.reddit.com/r/theydidthemath/

^smartass Big Grin

Anyway, I should have placed the reason behind this post in a more obvious place (say, somewhere near the start, rather than in the middle): Daemian's code doesn't calculate the distance correctly, and I wanted to show him how to do it.


RE: Focus Distance Debug Variable? - Daemian - 12-29-2014

I expected someone to improve -or adapt- the code to their needs but you just solved it all. :0
Maybe you wanna make another function too to get the angle? In a regular 0 to 360 range. I was thinking about it but still I didn't make any tests. I know it should be simple.
Or maybe give us a hint on how would you approach to solve it :>

It is true my code would return a wrong value if you compare objects in opposite sides from the center, I just thought it would be improbable that someone would do that because of the dimensions of an Amnesia level and how it evolves when it grows.
I mean you find yourself always comparing similar numbers like x33780 with x33712. But yeah, mine works and it's very precise, but is not the correct way because of that. :]

rep++