BonesTheRabbit
Member
Posts: 68
Threads: 20
Joined: Feb 2012
Reputation:
2

RE: Focus Distance Debug Variable?
(12262014, 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.


12272014, 01:24 AM 

Daemian
Posting Freak
Posts: 1,129
Threads: 42
Joined: Dec 2012
Reputation:
49

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.
float GetEntitiesDistance ( string &in fromEntity, string &in toEntity ) { float x1 = GetEntityPosX( fromEntity ); float y1 = GetEntityPosY( fromEntity ); float z1 = GetEntityPosZ( fromEntity ); float x2 = GetEntityPosX( toEntity ); float y2 = GetEntityPosY( toEntity ); float z2 = GetEntityPosZ( toEntity ); 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; }


12272014, 04:22 PM 

TheGreatCthulhu
Member
Posts: 213
Threads: 10
Joined: Oct 2010
Reputation:
32

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 rr' vector in the image below.)
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.)
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 nonzero 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:
float GetEntitiesDistance ( string &in fromEntity, string &in toEntity ) { float x1 = GetEntityPosX( fromEntity ); float y1 = GetEntityPosY( fromEntity ); float z1 = GetEntityPosZ( fromEntity ); float x2 = GetEntityPosX( toEntity ); float y2 = GetEntityPosY( toEntity ); float z2 = GetEntityPosZ( toEntity );
float x3 = x2  x1; float y3 = y2  y1; float z3 = z2  z1; float distance = MathSqrt(x3*x3 + y3*y3 + z3*z3);
return distance; }


12292014, 01:41 PM 

PutraenusAlivius
Posting Freak
Posts: 4,713
Threads: 75
Joined: Dec 2012
Reputation:
119

RE: Focus Distance Debug Variable?
(12292014, 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 rr' vector in the image below.)
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.)
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 nonzero 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:
float GetEntitiesDistance ( string &in fromEntity, string &in toEntity ) { float x1 = GetEntityPosX( fromEntity ); float y1 = GetEntityPosY( fromEntity ); float z1 = GetEntityPosZ( fromEntity ); float x2 = GetEntityPosX( toEntity ); float y2 = GetEntityPosY( toEntity ); float z2 = GetEntityPosZ( toEntity );
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."


12292014, 05:41 PM 

TheGreatCthulhu
Member
Posts: 213
Threads: 10
Joined: Oct 2010
Reputation:
32

RE: Focus Distance Debug Variable?
(12292014, 05:41 PM)Julius Caesar Wrote: http://www.reddit.com/r/theydidthemath/
^smartass
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.


12292014, 07:18 PM 

Daemian
Posting Freak
Posts: 1,129
Threads: 42
Joined: Dec 2012
Reputation:
49

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: 12292014, 11:17 PM by Daemian.)


12292014, 11:12 PM 

