Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script Help Focus Distance Debug Variable?
BonesTheRabbit Offline
Member

Posts: 68
Threads: 20
Joined: Feb 2012
Reputation: 2
#11
RE: Focus Distance Debug Variable?

(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.
12-27-2014, 01:24 AM
Find Reply
Daemian Offline
Posting Freak

Posts: 1,129
Threads: 42
Joined: Dec 2012
Reputation: 49
#12
RE: Focus Distance Debug Variable?

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: (Select All)
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;
    


12-27-2014, 04:22 PM
Find Reply
TheGreatCthulhu Offline
Member

Posts: 213
Threads: 10
Joined: Oct 2010
Reputation: 32
#13
RE: Focus Distance Debug Variable?

@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: (Select All)
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;

(This post was last modified: 12-29-2014, 01:46 PM by TheGreatCthulhu.)
12-29-2014, 01:41 PM
Find Reply
PutraenusAlivius Offline
Posting Freak

Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation: 119
#14
RE: Focus Distance Debug Variable?

(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: (Select All)
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/

"Veni, vidi, vici."
"I came, I saw, I conquered."
12-29-2014, 05:41 PM
Find Reply
TheGreatCthulhu Offline
Member

Posts: 213
Threads: 10
Joined: Oct 2010
Reputation: 32
#15
RE: Focus Distance Debug Variable?

(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.
12-29-2014, 07:18 PM
Find Reply
Daemian Offline
Posting Freak

Posts: 1,129
Threads: 42
Joined: Dec 2012
Reputation: 49
#16
RE: Focus Distance Debug Variable?

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++

(This post was last modified: 12-29-2014, 11:17 PM by Daemian.)
12-29-2014, 11:12 PM
Find Reply




Users browsing this thread: 1 Guest(s)